Link to home
Create AccountLog in
Avatar of pgmtkl
pgmtkl

asked on

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;
      }
}
ASKER CERTIFIED SOLUTION
Avatar of imladris
imladris
Flag of Canada image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
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
Avatar of pgmtkl
pgmtkl

ASKER

Thanks. That did it. I was wondering why it was skipping my statements. Thanks again.
Avatar of pgmtkl

ASKER

I meant to grade excellent and hit the wrong button.