Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

menu error

Posted on 2006-03-28
6
Medium Priority
?
223 Views
Last Modified: 2010-04-15
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;
}





0
Comment
Question by:pgmtkl
6 Comments
 
LVL 45

Accepted Solution

by:
sunnycoder earned 2000 total points
ID: 16316997
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
 
LVL 20

Expert Comment

by:ikework
ID: 16317009
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
 
LVL 11

Expert Comment

by:x4u
ID: 16317067
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 45

Expert Comment

by:sunnycoder
ID: 16317094
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
 

Author Comment

by:pgmtkl
ID: 16317551
Thanks for all of the help! It works. I will look into the additional thoughts to code it as well. Thanks again
0
 
LVL 45

Expert Comment

by:sunnycoder
ID: 16317843
Glad to be of assistance :)
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question