in my code i am supposed to ask the user to enter total sales, there county sales tax, there state sales tax, then return the total tax collected and also return what the state and county sales tax is.
i have no errors but my output is coming back as:
Your county sales tax is 0.00
Your state sales tax is 0.00
your total tax collected is 0.00
here is my code:
#include <stdio.h>
void inputTaxData(int *, float *, float *);
float calculateTaxes(int, float, float);
void displayTaxData(float, float, float);
int main()
{
int totalSales;
float ctax, stax;
float totalTax;
inputTaxData(&totalSales, &ctax, &stax);
calculateTaxes(totalSales, ctax, stax);
displayTaxData(ctax, stax, totalTax);
return 0;
}
void inputTaxData(int *totalSalesPtr,float *ctaxPtr, float *staxPtr)
{
int totalSales;
float ctax, stax;
printf("\nWhat is your total sales for the month?");
scanf("%d", &totalSales);
*totalSalesPtr = totalSales;
printf("\nWhat is your county sales tax?");
scanf("%f", &ctax);
*ctaxPtr = ctax;
printf("\nWhat is your state sales tax?");
scanf("%f", &stax);
*staxPtr = stax;
}
float calculateTaxes(int totalSales, float ctax, float stax)
{
float totalTax = totalSales * ctax + stax;
return totalTax;
}
void displayTaxData(float ctax, float stax, float totalTax)
{
printf("\nYour County Sales tax is %.2f", &ctax);
printf("\nYour State Sales tax is %.2f", &stax);
printf("\nYour total tax collected is %.2f", totalTax);
}
Here are the portions of the code which need correction.
Issue 1:
In the function void inputTaxData(int *totalSalesPtr,float *ctaxPtr, float *staxPtr), you already are passing the addresses(pointers) of the variables.Hence you don't have to re-declare local variables in the function.
Fix:
All you gotta do is to assign the values obtained by scanf to these locations.
Issue 2:
The function float calculateTaxes(int totalSales, float ctax, float stax) returns the totalTax but this returned value is not used in main.
Fix:
Assign the returned value to totalTax in main.
Issue 3:In the function void displayTaxData(float ctax, float stax, float totalTax) you are passing a copy of the literal values of the arguments but are trying to print the addresses of ctax and stax.
Fix:Print the literal values of the arguments passed to this function.
Applying the fixes, your code would look like this.....
#include <stdio.h>
void inputTaxData(int *, float *, float *);
float calculateTaxes(int, float, float);
void displayTaxData(float, float, float);
int main()
{
int totalSales;
float ctax, stax;
float totalTax;
inputTaxData(&totalSales, &ctax, &stax);
totalTax = calculateTaxes(totalSales, ctax, stax);//assign the returned value
displayTaxData(ctax, stax, totalTax);
return 0;
}
void inputTaxData(int *totalSales,float *ctax, float *stax)
{
printf("\nWhat is your total sales for the month?");
scanf("%d", totalSales);//assign the scanned value to the variable
printf("\nWhat is your county sales tax?");
scanf("%f", ctax);//assign the scanned value to the variable
printf("\nWhat is your state sales tax?");
scanf("%f", stax);//assign the scanned value to the variable
}
float calculateTaxes(int totalSales, float ctax, float stax)
{
float totalTax = totalSales * ctax + stax;
return totalTax;
}
void displayTaxData(float ctax, float stax, float totalTax)
{
printf("\nYour County Sales tax is %.2f", ctax);//print value
printf("\nYour State Sales tax is %.2f", stax);//print value
printf("\nYour total tax collected is %.2f", totalTax);
}
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed last month.
Improve this question
I'm struggling to solve this problem from CS50's introduction to computer science. It might seem simple but I still couldn't get it why it's not working properly.
So, I have to prompt a bill amount (type float) before tax and tip, calcule tax and tip using its percentage and then add them all and split it in two. The user prompt is a bill amount (type float) before taxes, tip and tax percentages. I have to complete a given function as shown below.
#include <cs50.h>
#include <stdio.h>
float half(float bill, float tax, int tip);
int main(void)
{
float bill_amount = get_float("Bill before tax and tip: ");
float tax_percent = get_float("Sale Tax Percent: ");
int tip_percent = get_int("Tip percent: ");
printf("You will owe $%.2f each!\n", half(bill_amount, tax_percent, tip_percent));
}
// TODO: Complete the function
float half(float bill, float tax, int tip)
{
return 0.0;
}
And we should have
Bill before tax and tip: 12.50
Sale Tax Percent: 8.875
Tip percent: 20
You will owe $8.17 each!
https://cs50.harvard.edu/x/2023/problems/1/half/
What I've been trying
#include <cs50.h>
#include <stdio.h>
float half(float bill, float tax, int tip);
int main(void)
{
float bill_amount = get_float("Bill before tax and tip: ");
float tax_percent = get_float("Sale Tax Percent: ");
int tip_percent = get_int("Tip percent: ");
float bill = bill_amount;
float tax = tax_percent / 100;
int tip = tip_percent / 100;
printf("You will owe $%.2f each!\n", half(bill, tax, tip));
}
// TODO: Complete the function
float half(float bill, float tax, int tip)
{
float bill_after_tax = bill * tax + bill;
float bill_after_tax_tip = bill_after_tax * tip + bill_after_tax;
float split = bill_after_tax_tip / 2;
return split;
}
Your task is to complete the function half, not to make changes in the function main. Therefore, the changes you made to the function main violate the constraints of the task.
For this reason, you should divide tax and tip by 100 in the function half, not in the function main:
#include <cs50.h>
#include <stdio.h>
float half(float bill, float tax, int tip);
int main(void)
{
float bill_amount = get_float("Bill before tax and tip: ");
float tax_percent = get_float("Sale Tax Percent: ");
int tip_percent = get_int("Tip percent: ");
printf("You will owe $%.2f each!\n", half(bill_amount, tax_percent, tip_percent));
}
float half(float bill, float tax, int tip)
{
float bill_after_tax = bill * ( tax / 100.0 ) + bill;
float bill_after_tax_tip = bill_after_tax * ( tip / 100.0 ) + bill_after_tax;
float split = bill_after_tax_tip / 2;
return split;
}
Note that it is important to write ( tip / 100.0 ) instead of ( tip / 100 ), because the former is a floating-point division, whereas the latter is an integer division, which always has an integer result. This is because tip and 100 are both of type int. For example, ( 20 / 100.0 ) evaluates to the floating-point number 0.2, whereas ( 20 / 100 ) evaluates to the integer 0. You want the result to be 0.2, not 0.
The type of tip should be float since you are dividing it by 100 which results in a decimal number.
#include <cs50.h>
#include <stdio.h>
float half(float bill, float tax, int tip);
int main(void)
{
float bill_amount = get_float("Bill before tax and tip: ");
float tax_percent = get_float("Sale Tax Percent: ");
int tip_percent = get_int("Tip percent: ");
float bill = bill_amount;
float tax = tax_percent / 100.0;
float tip = tip_percent / 100.0;
printf("You will owe $%.2f each!\n", half(bill, tax, tip));
}
// TODO: Complete the function
float half(float bill, float tax, float tip)
{
float bill_after_tax = bill \* tax + bill;
float bill_after_tax_tip = bill_after_tax \* tip + bill_after_tax;
float split = bill_after_tax_tip / 2;
return split;
}
When I run this code I expect the return give me (100-percent)/100 - price
#include<stdio.h>
#include<cs50.h>
float discount(int prec , float pri);
int main (void){
int percent = get_float("enter the present here ");
float price = get_float("enter the price ");
float function1 = discount(percent , price);
printf("the new number is %.2f\n", function1);
}
float discount(int prec , float pri){
return (100 - prec)/100 * pri ;
}
but I got this return and I don't know what the problem is
$ make lesson
$ ./lesson
enter the present here 15
enter the price 100
the new number is 0.00
when I use this code
#include<stdio.h>
#include<cs50.h>
float discount(int prec , float pri);
int main (void){
int percent = get_float("enter the present here ");
float price = get_float("enter the price ");
float function1 = discount(percent , price);
printf("the new number is %.2f\n", function1);
}
float discount(int prec , float pri){
return pri * (100 - prec)/100 ;
}
I got the correct value but I want to know why the first example didn't work
$ make lesson
$ ./lesson
enter the present here 14
enter the price 100
the new number is 86.00
here in my program i must ask the user for his total sales for the month, then ask the user for the county tax rate(6%) and the state sales tax(6%) then calculate the total sales tax based of what they entered. i also must utilize pointers.
i keep getting the following error:[Error] called object 'calculateTaxes' is not a function or function pointer
i cant seem to figure it out. here is the code i have so far
#include <stdio.h>
int inputTaxData(int* totalSales);
float calculateTaxes;
int main()
{
int* totalSales;
inputTaxData(&totalSales);
calculateTaxes();
float c;
float s;
float t;
return 0;
}
int inputTaxData(int* totalSales)
{
printf("What is your total sales for the month?");
scanf("%d", totalSales);
}
float calculateTaxes(float c, float s, float t)
{
float c = 1.06;
float s = 1.06;
float t = c + s;
printf ("Your total state and county sales tax is %f", totalSales);
}
You declare float calculateTaxes;, i.e. as a float variable.
Then you try to call it as a function.
calculateTaxes();
That is what the compiler tries to tell you.
You probably wanted to instead provide a prototype for the later defined function.
float calculateTaxes(float c, float s, float t);
I keep getting the same error message not sure how to correct it
/*This program will prospective borrowers calculate monthly payment on a loan.
The program also prints the amortization table to show the balance of the loan after each monthly payment.
By: Kenneth Moore
Date: 3/13/2016
*/
#include <stdio.h>
#include <stdlib.h>
#include <math.h>>
const float monthInYear = 12; //This is a constant for the number of months in a year
const float percentTotal = 100; //This is a constant for calculating percentages
void getData (float* prPtr, float* nyPtr, float* iyPtr);
//this function will calculate the data for monthly payments based off APR and return the data to be printed on a table
float calculateMonthlyPayment (float* nmPtr, float* imPtr, float* pPtr, float* qPtr, float* mpPtr, float* nyPtr, float* iyPtr, float* prPtr);
//This function will print the data
void printInformation ();
//This function will display information in a table format
void printAmortizationTable ();
int main()
{
float nm; //scheduled number of months for the loan
float ny; //scheduled number of years to amortize the loan
float iy; //interest rate per year as a percentage
float im; //interest rate/month decimal
float pr; //principal the amount of the loan
float p; //the value of (1+IM)
float q; //the value of P/(P-1)
float mp; //Monthly payment
printf("Welcome the following program will help you decide how much loan you can afford.");
printf("You will be prompted to enter the principle amount of the loan, the interest");
printf("rate, and the length of the loan in years. It will then show you on a table");
printf("how much your monthly payment will be and how much of the loan remains after");
printf("each payment.");
getData (&pr, &ny, &iy);
calculateMonthlyPayment(&nm, &im, &p, &q, &mp, &ny, &iy, &pr);
return 0;
}
/*This function is a data collecting function the will collect user imputed data about the loan and calculate the interest and
payments to be printed in later functions*/
//Statement
void getData (float* prPtr, float* nyPtr, float* iyPtr)
{
printf("\nEnter the amount of money you wish to borrow.");
scanf("%f", prPtr);
printf("Enter the length in years for the loan.");
scanf("%f", nyPtr);
printf("Enter your expected interest rate.");
scanf("%f", iyPtr);
return;
}
//This function will now process the data to return the payment amount to be used in the table
float calculateMonthlyPayment (float* nmPtr, float* imPtr, float* pPtr, float* qPtr, float* mpPtr, float* nyPtr, float* iyPtr, float* prPtr)
{
nmPtr =(nyPtr*12); //how many months the loan will be in effect
imPtr =(iyPtr/monthInYear)/percentTotal; //how much interest is paid per month
//this is my error location how can i reslove it
pPtr = pow(1+imPtr,nmPtr);
qPtr = pPtr/(pPtr-1);
mpPtr = (prPtr*imPtr*qPtr);
}
You are multiplier a pointer with a constant. I reckon you want the value of where the pointer is pointing at. Use pointer-dereference operator * for that (same goes for nmPtr, and by the way the other pointers in that function):
*nmPtr =(*nyPtr*12);
The guys just helped me with a problem, got solved 100%, but now I have another one.
I compile my files and everything without problems, but when i want to run the program nothing happens.
enter.c : http://pastebin.com/GGzVeAhw
simple_interest.c: http://pastebin.com/XdESrxSk
This is how my header file looks like:
int enter(void);
double Calc_Interest(double principal, double rate, int term);
double Calc_Amount(double principal, double interest);
double Calc_Payments(double total, int term);
void Display_Results(double principal, double interest, double total, double mPay);
PS This forum is really helpful, especially for a beginner like me.
You have a bunch of prototypes in main(), but no function call.
It should be:
int main(void)
{
enter();
return 0;
}
You seem really confused about how to declare and use functions, perhaps you need some tutorial?
simple_interest.h
int enter(void);
double Calc_Interest(double principal, double rate, int term);
double Calc_Amount(double principal, double interest);
double Calc_Payments(double total, int term);
void Display_Results(double principal, double interest, double total, double mPay);
simple_interest.c
//Implement all functions in this file
#include <stdio.h>
double Calc_Interest(double principal, double rate, int term)
{
double interest;
interest = principal * (rate/100) *term;
return interest;
}
double Calc_Amount(double principal, double interest)
{
double total;
total = principal + interest;
return total;
}
double Calc_Payments(double total, int term)
{
double mPay;
mPay= total / (12 * term);
return mPay;
}
void Display_Results(double principal, double interest, double total, double mPay)
{
printf("\n");
printf("Personal Loan Statement\n");
printf("\n");
printf("Amount Borrowed: %.2f\n" , principal);
printf("Interest on Loan: %.2f\n" , interest);
printf("--------------------------------------------------------------------------------\n");
printf("Amount of Loan: %.2f\n" , total);
printf("Monthly Payment: %.2f\n" , mPay);
}
sample.c
#include <stdio.h>
#include "simple_interest.h"
int enter(void)
{
double principal, rate, interest, total, mPay;
int term;
printf("--------------------------------------------------------------------------------\n");
printf("This program calculates the monthly payments due from a personal loan\n");
printf("--------------------------------------------------------------------------------\n");
printf("Enter amount of the loan: ");
scanf("%lf", &principal);
printf("Enter current annual interest rate in \n");
scanf("%lf", &rate);
printf("Enter the number of years of the loan: ");
scanf("%d", &term);
interest = Calc_Interest(principal, rate, term);
total = Calc_Amount(principal, interest);
mPay = Calc_Payments(total, term);
Display_Results(principal, interest, total, mPay);
return 0;
}
int main(void)
{
enter();
return 0;
}
compile like this way
gcc sample.c simple_interest.c
./a.out