Solved

# end of program, else statement

Posted on 2006-03-29
215 Views
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;

printf("What is the purchase amount?");

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;
}
}
0
Question by:pgmtkl

LVL 16

Accepted Solution

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

LVL 8

Expert Comment

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;

printf("What is the purchase amount?");

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

cheers
deepu
0

Author Comment

Thanks. That did it. I was wondering why it was skipping my statements. Thanks again.
0

Author Comment

I meant to grade excellent and hit the wrong button.
0

## Featured Post

### Suggested Solutions

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.