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)*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 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;
//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 0;
}
}
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
ASKER
Thanks. That did it. I was wondering why it was skipping my statements. Thanks again.
ASKER
I meant to grade excellent and hit the wrong button.
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)*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 0;
}
} // end if (fpurchase > 0.0)
} // end main
i think this will help you
cheers
deepu