• C

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)*fpurchase);
   fDelMarTotal=(fpurchase+fDelMarSalesTax);
   printf("\nDel Mar  \t%.2f\t%.2f\t%.2f\n",fDelMarTaxRate,fDelMarSalesTax,fDelMarTotal);
   return 0;
}
   
if (cstore == B)  
  { fEncinitasSalesTax = ((fEncinitasTaxRate/100)*fpurchase);
    fEncinitasTotal=(fpurchase+fEncinitasSalesTax);
    printf("\nEncinitas\t%4.2f\t%4.2f\t%4.2f\n",fEncinitasTaxRate,fEncinitasSalesTax,fEncinitasTotal);
    return 0;
  }

if (cstore == C)
   {  fLaJollaSalesTax = ((fLaJollaTaxRate/100)*fpurchase);
      fLaJollaTotal=(fpurchase+fLaJollaSalesTax);
      printf("\nLaJolla  \t%4.2f\t%4.2f\t%4.2f\n",fLaJollaTaxRate,fLaJollaSalesTax,fLaJollaTotal);  
      return 0;  
   }

      else {
      printf("Invalid Entry. Please enter A, B, or C\n");
      }
 }
   
/*//     Return zero to confirm that the program ran*/
     return 0;
}





pgmtklAsked:
Who is Participating?
 
sunnycoderCommented:
1.
       float f1=fDelMarSalesTax;
       float f2=fEncinitasSalesTax;
       float f3=fLaJollaSalesTax;

All are uninitialized .. dangerous

2.
scanf("%c",cstore);
should be
scanf("%c",&cstore);

3.
if (cstore == A)
if (cstore == B)
if (cstore == C)

should be
if (cstore == 'A')
if (cstore == 'B')
if (cstore == 'C')

Cheers!
sunnycoder
0
 
ikeworkCommented:
what you do here:

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
0
 
x4uCommented:
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;
}
0
Prepare for an Exciting Career in Cybersecurity

Help prevent cyber-threats and provide solutions to safeguard our global digital economy. Earn your MS in Cybersecurity. WGU’s MSCSIA degree program curriculum features two internationally recognized certifications from the EC-Council at no additional time or cost.

 
sunnycoderCommented:
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
0
 
pgmtklAuthor Commented:
Thanks for all of the help! It works. I will look into the additional thoughts to code it as well. Thanks again
0
 
sunnycoderCommented:
Glad to be of assistance :)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.