• C

end of program, else statement

I have gotten the menu of my program to work and validated the selections. However, if i do not select a valid selection from the menu it randomly displays one of the if statements. The program is not following the if, else flow. I have returns after the if, a,b, or c statements. SHould the order be different?


#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;
       
//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 0;
      }
}
pgmtklAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

imladrisCommented:
I think the critical problem is that the second and third if condition:

if (cstore = 'B')  

if (cstore = 'C')

are wrong. With a single '=' those are assignments and will evaluate to true, and so the associated code will be executed.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
deepu chandranCommented:
hi friend,

   The problem with the following scanf i think

scanf("%c",&cstore);   =>    scanf(" %c",&cstore);//give a space to control string,it ia known bug in turbo c++

At the end  u hav to give one closing brace

here is the program i hav currected,

#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;

//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)  // here no need for semi colon
{

/*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 0;
      }
}  // end if (fpurchase > 0.0)
}  // end main



i think this will help you

cheers
deepu
0
pgmtklAuthor Commented:
Thanks. That did it. I was wondering why it was skipping my statements. Thanks again.
0
pgmtklAuthor Commented:
I meant to grade excellent and hit the wrong button.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.

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.