pgmtkl
asked on
menu error
I am prompting users to pick a choice from a menu which will then calculate the tax. I was able to get the menu to appear, however it defaults to option A always. I have if statements. Should i have something else?
#include <stdio.h>
int main()
{
/* // declare variables here*/
//The first variables are each stores tax rate, Del Mar, Encinitas, La jolla
//The second variable is the purchase amount
//The third variable is the calculated tax rate
//The fourth variable is for user input of purchase amount
float fDelMarTaxRate = 7.25;
float fEncinitasTaxRate = 7.5;
float fLaJollaTaxRate = 7.75;
float fPurchaseAmount;
float fDelMarSalesTax, fEncinitasSalesTax, fLaJollaSalesTax;
float fpurchase;
float fDelMarTotal;
float fEncinitasTotal;
float fLaJollaTotal;
char cstore;
float f1=fDelMarSalesTax;
float f2=fEncinitasSalesTax;
float f3=fLaJollaSalesTax;
char A,B,C;
//print header and message asking what the purchase amount is
printf("What is the purchase amount?");
/*imput the answer into purchase*/
scanf("%f",&fpurchase);
if (fpurchase > 0.0);
{
/*select the store the purchase is made at*/
printf("A Del Mar\n");
printf("B Encinictas\n");
printf("C La Jolla\n");
printf("Select the store i.e A,B,or C: ");
scanf("%c",cstore);
printf("%c\n",cstore);
if (cstore == A)
{
/*calculate the tax amount*/
fDelMarSalesTax =((fDelMarTaxRate/100)*fpu rchase);
fDelMarTotal=(fpurchase+fD elMarSales Tax);
printf("\nDel Mar \t%.2f\t%.2f\t%.2f\n",fDel MarTaxRate ,fDelMarSa lesTax,fDe lMarTotal) ;
return 0;
}
if (cstore == B)
{ fEncinitasSalesTax = ((fEncinitasTaxRate/100)*f purchase);
fEncinitasTotal=(fpurchase +fEncinita sSalesTax) ;
printf("\nEncinitas\t%4.2f \t%4.2f\t% 4.2f\n",fE ncinitasTa xRate,fEnc initasSale sTax,fEnci nitasTotal );
return 0;
}
if (cstore == C)
{ fLaJollaSalesTax = ((fLaJollaTaxRate/100)*fpu rchase);
fLaJollaTotal=(fpurchase+f LaJollaSal esTax);
printf("\nLaJolla \t%4.2f\t%4.2f\t%4.2f\n",f LaJollaTax Rate,fLaJo llaSalesTa x,fLaJolla Total);
return 0;
}
else {
printf("Invalid Entry. Please enter A, B, or C\n");
}
}
/*// Return zero to confirm that the program ran*/
return 0;
}
#include <stdio.h>
int main()
{
/* // declare variables here*/
//The first variables are each stores tax rate, Del Mar, Encinitas, La jolla
//The second variable is the purchase amount
//The third variable is the calculated tax rate
//The fourth variable is for user input of purchase amount
float fDelMarTaxRate = 7.25;
float fEncinitasTaxRate = 7.5;
float fLaJollaTaxRate = 7.75;
float fPurchaseAmount;
float fDelMarSalesTax, fEncinitasSalesTax, fLaJollaSalesTax;
float fpurchase;
float fDelMarTotal;
float fEncinitasTotal;
float fLaJollaTotal;
char cstore;
float f1=fDelMarSalesTax;
float f2=fEncinitasSalesTax;
float f3=fLaJollaSalesTax;
char A,B,C;
//print header and message asking what the purchase amount is
printf("What is the purchase amount?");
/*imput the answer into purchase*/
scanf("%f",&fpurchase);
if (fpurchase > 0.0);
{
/*select the store the purchase is made at*/
printf("A Del Mar\n");
printf("B Encinictas\n");
printf("C La Jolla\n");
printf("Select the store i.e A,B,or C: ");
scanf("%c",cstore);
printf("%c\n",cstore);
if (cstore == A)
{
/*calculate the tax amount*/
fDelMarSalesTax =((fDelMarTaxRate/100)*fpu
fDelMarTotal=(fpurchase+fD
printf("\nDel Mar \t%.2f\t%.2f\t%.2f\n",fDel
return 0;
}
if (cstore == B)
{ fEncinitasSalesTax = ((fEncinitasTaxRate/100)*f
fEncinitasTotal=(fpurchase
printf("\nEncinitas\t%4.2f
return 0;
}
if (cstore == C)
{ fLaJollaSalesTax = ((fLaJollaTaxRate/100)*fpu
fLaJollaTotal=(fpurchase+f
printf("\nLaJolla \t%4.2f\t%4.2f\t%4.2f\n",f
return 0;
}
else {
printf("Invalid Entry. Please enter A, B, or C\n");
}
}
/*// Return zero to confirm that the program ran*/
return 0;
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
You should look at your compiler warnings, they point you to problems in your code
1. delete these declarations. You don't use f1, f2, f3 and A, B, C are not needed, see below.
float f1=fDelMarSalesTax;
float f2=fEncinitasSalesTax;
float f3=fLaJollaSalesTax;
char A,B,C;
2. scanf("%c",cstore);
should be
scanf("%c",&cstore);
You need to provide a pointer to the variable if you want scanf to store the result in it.
3. if (cstore == A) compares two variables (cstore and A) but you obviously wnat to compare cstore with the character A which would be 'A'. So this becomes:
if (cstore == 'A')
4. You need to use chain your if statements with else as you want to check for a invalid input at the end.
if (cstore == 'A')
{
...
} else if( cstore == 'B' )
{
...
}
else if( cstore == 'C' )
{
...
}
else
{
...
}
You could also use a switch statement instead of the if-else statements which would look like this:
switch( cstore )
{
case 'A':
{
...
}
break;
case 'B':
{
...
}
break;
case 'C':
{
...
}
break;
default:
{
...
}
break;
}
1. delete these declarations. You don't use f1, f2, f3 and A, B, C are not needed, see below.
float f1=fDelMarSalesTax;
float f2=fEncinitasSalesTax;
float f3=fLaJollaSalesTax;
char A,B,C;
2. scanf("%c",cstore);
should be
scanf("%c",&cstore);
You need to provide a pointer to the variable if you want scanf to store the result in it.
3. if (cstore == A) compares two variables (cstore and A) but you obviously wnat to compare cstore with the character A which would be 'A'. So this becomes:
if (cstore == 'A')
4. You need to use chain your if statements with else as you want to check for a invalid input at the end.
if (cstore == 'A')
{
...
} else if( cstore == 'B' )
{
...
}
else if( cstore == 'C' )
{
...
}
else
{
...
}
You could also use a switch statement instead of the if-else statements which would look like this:
switch( cstore )
{
case 'A':
{
...
}
break;
case 'B':
{
...
}
break;
case 'C':
{
...
}
break;
default:
{
...
}
break;
}
Since you are using scanf to read inputs, you are likely to face a few problems
1. After you enter the purchase amount, scanf will read the float but the enter key you pressed (newline) would still be present in the input stream. Your next read request will fetch this newline and nor new input!! To get rid of this, read another character to consume the \n e.g
scanf("%f%c",&fpurchase,&A )
Similarly with cstore
scanf("%c%c",&cstore,&B);
Still this depend on user to input the values correctly, e.g. "1.2\n" .. if user enters extra whitespace, program will not be able to handle it
2. There is no way to validate your input. e.g. if user enter ABC for purchase amount, scanf will still read something and pass it on and ofcourse your calculations would go awry ... If you wish to make it more robust, read input as a string and then convert it to required format after validation, e.g. to read in a double/float, you would
fgets (buffer, BUF_LEN,stdin);
//strtod or strtof or custome validation function call here
Cheers!
sunnycoder
1. After you enter the purchase amount, scanf will read the float but the enter key you pressed (newline) would still be present in the input stream. Your next read request will fetch this newline and nor new input!! To get rid of this, read another character to consume the \n e.g
scanf("%f%c",&fpurchase,&A
Similarly with cstore
scanf("%c%c",&cstore,&B);
Still this depend on user to input the values correctly, e.g. "1.2\n" .. if user enters extra whitespace, program will not be able to handle it
2. There is no way to validate your input. e.g. if user enter ABC for purchase amount, scanf will still read something and pass it on and ofcourse your calculations would go awry ... If you wish to make it more robust, read input as a string and then convert it to required format after validation, e.g. to read in a double/float, you would
fgets (buffer, BUF_LEN,stdin);
//strtod or strtof or custome validation function call here
Cheers!
sunnycoder
ASKER
Thanks for all of the help! It works. I will look into the additional thoughts to code it as well. Thanks again
Glad to be of assistance :)
if (cstore == A)
is comparing the values of two variables. cstore's value is the user-input. the value in variable A is undefined, since it was never set to any value.
i guess, what you wanted to do here, is comparing the variable cstrore with the char 'A'. so what you have to do is this:
if (cstore == 'A')
the same for the next comparisons, replace them
if (cstore == 'B')
if (cstore == 'C')
and remove the line:
char A,B,C;
hope it helps :)
ike