• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 224
  • Last Modified:

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;
      }
}
0
pgmtkl
Asked:
pgmtkl
  • 2
1 Solution
 
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
 
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

Featured Post

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.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now