Thread: Please help- trying to have the program produce sales tax and total sale amount when

  1. #1
    Registered User
    Join Date
    Apr 2010
    Posts
    17

    Please help- trying to have the program produce sales tax and total sale amount when

    Code:
    /*Purpose: Check user's input for validity.
     Have the user select from the menu which store to use for tax calculation.
     Calculate and display the tax amount for the store selected,
      and the total sale amount for that store. */
    
    /********************************************************/
    #include <stdio.h>                           /* Header file */
    
    int main (void)                             /* Main function for the program */
    {
    int Store_No;
      /*Initialize Variables*/
      float fAmt = 0.00;                         /*Amount the user will input*/
      float fDelMar = 0.00;                      /*DelMar Tax result based on user input*/
      float fEncinitas = 0.00;                   /*Encinitas Tax result based on user input*/
      float fLaJolla = 0.00;                     /*LaJolla Tax result based on user input*/
      float fDelMarResult = 0.00;                /*Total amount plus tax result*/
      float fEncinitasResult = 0.00;             /*Total amount plus tax result*/
      float fLaJollaResult = 0.00;               /*Total amount plus tax result*/
      
      printf("Welcome to Kudler Fine Foods!!\n");
      printf("Make Your Store Selection from the list Below:\n\n");
      printf("  Store No.\tStore\n\n");
      printf("\t1.\tDel Mar\n");
      printf("\t2.\tEncinitas\n");
      printf("\t3.\tLa Jolla\n");
      printf("\n\t4.\tExit\n");
      printf("\nPress 4 to exit.\n");
      
      /*while (Store_No !=4)*/
      printf("Please enter a Store No. : ");
      while (scanf("%f", &Store_No) != 1)
      {
       while (getchar() != '\n');
       printf("Your input is not valid. Please enter a store No.: ");
      }
      /*if (Store_No != 4)*/
      {
      printf("Please enter the purchase amount:");     /* Prompt the user for the amount */
      while (scanf("%f", &fAmt) != 1)
      {
       while (getchar() != '\n');                      /*Validate the input*/
       printf("Your input is not Valid. Please, try again: "); /* Notify the user if the input is valid or not */
      }
      
      printf ("You entered %.2f\n", fAmt);
      
      
      switch (Store_No)
      {
      case 1:
      fDelMar=fAmt*.0725;
      fDelMarResult=fDelMar + fAmt;
      printf("\nThe tax for $%.2f in DelMar is $%.2f, with total of $%.2f\n", fAmt, fDelMar, fDelMarResult);  /*Display results*/
      break;
      
      case 2:
      fEncinitas=fAmt*.075;
      fEncinitasResult=fEncinitas + fAmt;
      printf("\nThe tax for $%.2f in Encinitas is $%.2f, with total of $%.2f\n", fAmt, fEncinitas, fEncinitasResult); /*Display results*/               
      break;
      
      case 3:
      fLaJolla=fAmt*.0775;
      fLaJollaResult=fLaJolla + fAmt;
      printf("\nThe tax for $%.2f in LaJolla is $%.2f, with total of $%.2f\n", fAmt, fLaJolla, fLaJollaResult);   /*Display results*/
      break;
      
      case 4:
      printf("Quitting program!\n");
      exit(0);                             
      return 0;
      
      default:
      printf("\nSorry, the Store No. you entered was not on the menu. Please try again! ");
     }
     }
     }
    
    /**************************************************************/

  2. #2
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    What exactly is the problem? Please explain.

  3. #3
    Registered User
    Join Date
    Apr 2010
    Posts
    17
    When I input the purchase amount, is not displaying the sales tax, and the total amount for the store No. selected. Thanks for your time.

  4. #4
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Well first of all you want to use %d to scanf store_no.

  5. #5
    Registered User
    Join Date
    Apr 2010
    Posts
    17
    Thanks. It works now when I use %d to scanf store_no, and I add a "while(1) {} before the menu selection. I'm just trying to figure out when I press 4, it will exit instead of asking for store no. or purchase amount. Thanks a lot.
    Code:
    /********************************
    printf("  Store No.\tStore\n\n"); while(1){
      printf("\t1.\tDel Mar\n");
      printf("\t2.\tEncinitas\n");
      printf("\t3.\tLa Jolla\n");
      printf("\n\t4.\tExit\n");
      printf("\nPress 4 to exit.\n");
      
      printf("Please enter a Store No. : ");
      while (scanf("%d", &Store_No) != 1)
    /*******************************/

  6. #6
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Ok, well instead of exiting the program in the switch, you can have an if clause right after you exit the while(scanf()) loop in which you read the option.

    Code:
    if(store_no == 4) exit(0);
    Then, if you hit 4 on input you will exit immediately after loop instead of going through the other scanfs and the switch.

  7. #7
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    First off, indent your code so we can see (1) what you think you are doing and (2) what you _ARE_ doing.

  8. #8
    Registered User
    Join Date
    Apr 2010
    Posts
    17
    When I add "if" code, it still won't exit the program immediately when I hit 4. I add it in the switch also, is the same. Here is the code:



    Code:
    #include <stdio.h>                           /* Header file */
    
    int main (void)                             /* Main function for the program */
    {
    int Store_No;
      /*Initialize Variables*/
      float fAmt = 0.00;                         /*Amount the user will input*/
      float fDelMar = 0.00;                      /*DelMar Tax result based on user input*/
      float fEncinitas = 0.00;                   /*Encinitas Tax result based on user input*/
      float fLaJolla = 0.00;                     /*LaJolla Tax result based on user input*/
      float fDelMarResult = 0.00;                /*Total amount plus tax result*/
      float fEncinitasResult = 0.00;             /*Total amount plus tax result*/
      float fLaJollaResult = 0.00;               /*Total amount plus tax result*/
      
      printf("Welcome to Kudler Fine Foods!!\n");
      printf("Make Your Store Selection from the list Below:\n\n");
      printf("  Store No.\tStore\n\n"); while(1){
      printf("\t1.\tDel Mar\n");
      printf("\t2.\tEncinitas\n");
      printf("\t3.\tLa Jolla\n");
      printf("\n\t4.\tExit\n");
      printf("\nPress 4 to exit.\n");
      
      printf("Please enter a Store No. : ");
      while (scanf("%d", &Store_No) != 1)
      {
       while (getchar() != '\n');
       printf("Your input is not valid. Please enter a store No.: ");
      }
      {
      printf("Please enter the purchase amount:");     /* Prompt the user for the amount */
      while (scanf("%f", &fAmt) != 1)
      {
       while (getchar() != '\n');                      /*Validate the input*/
       printf("Your input is not Valid. Please, try again: "); /* Notify the user if the input is valid or not */
      }
      
      printf ("You entered %.2f\n", fAmt);
      
      
      switch (Store_No)
      {
      case 1:
      fDelMar=fAmt*.0725;
      fDelMarResult=fDelMar + fAmt;
      printf("\nThe tax for $%.2f in DelMar is $%.2f, with total of $%.2f\n", fAmt, fDelMar, fDelMarResult);  /*Display results*/
      break;
      
      case 2:
      fEncinitas=fAmt*.075;
      fEncinitasResult=fEncinitas + fAmt;
      printf("\nThe tax for $%.2f in Encinitas is $%.2f, with total of $%.2f\n", fAmt, fEncinitas, fEncinitasResult); /*Display results*/               
      break;
      
      case 3:
      fLaJolla=fAmt*.0775;
      fLaJollaResult=fLaJolla + fAmt;
      printf("\nThe tax for $%.2f in LaJolla is $%.2f, with total of $%.2f\n", fAmt, fLaJolla, fLaJollaResult);   /*Display results*/
      break;
      
      case 4:
      if (Store_No == 4) exit(0);
      /*printf("Quitting program!\n");
      exit(0);*/                             
      return 0;
      
      default:
      printf("\nSorry, the Store No. you entered was not on the menu. Please try again!\n ");
     }
     }
     }
     }

  9. #9
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Quote Originally Posted by BigO View Post
    When I add "if" code, it still won't exit the program immediately when I hit 4. I add it in the switch also, is the same. Here is the code:



    Code:
    #include <stdio.h>                           /* Header file */
    
    int main (void)                             /* Main function for the program */
    {
    int Store_No;
      /*Initialize Variables*/
      float fAmt = 0.00;                         /*Amount the user will input*/
      float fDelMar = 0.00;                      /*DelMar Tax result based on user input*/
      float fEncinitas = 0.00;                   /*Encinitas Tax result based on user input*/
      float fLaJolla = 0.00;                     /*LaJolla Tax result based on user input*/
      float fDelMarResult = 0.00;                /*Total amount plus tax result*/
      float fEncinitasResult = 0.00;             /*Total amount plus tax result*/
      float fLaJollaResult = 0.00;               /*Total amount plus tax result*/
      
      printf("Welcome to Kudler Fine Foods!!\n");
      printf("Make Your Store Selection from the list Below:\n\n");
      printf("  Store No.\tStore\n\n"); while(1){
      printf("\t1.\tDel Mar\n");
      printf("\t2.\tEncinitas\n");
      printf("\t3.\tLa Jolla\n");
      printf("\n\t4.\tExit\n");
      printf("\nPress 4 to exit.\n");
      
      printf("Please enter a Store No. : ");
      while (scanf("%d", &Store_No) != 1)
      {
       while (getchar() != '\n');
       printf("Your input is not valid. Please enter a store No.: ");
      }
      {* <--------remove this and move the if here */
      printf("Please enter the purchase amount:");     /* Prompt the user for the amount */
      while (scanf("%f", &fAmt) != 1)
      {
       while (getchar() != '\n');                      /*Validate the input*/
       printf("Your input is not Valid. Please, try again: "); /* Notify the user if the input is valid or not */
      }
      
      printf ("You entered %.2f\n", fAmt);
      
      
      switch (Store_No)
      {
      case 1:
      fDelMar=fAmt*.0725;
      fDelMarResult=fDelMar + fAmt;
      printf("\nThe tax for $%.2f in DelMar is $%.2f, with total of $%.2f\n", fAmt, fDelMar, fDelMarResult);  /*Display results*/
      break;
      
      case 2:
      fEncinitas=fAmt*.075;
      fEncinitasResult=fEncinitas + fAmt;
      printf("\nThe tax for $%.2f in Encinitas is $%.2f, with total of $%.2f\n", fAmt, fEncinitas, fEncinitasResult); /*Display results*/               
      break;
      
      case 3:
      fLaJolla=fAmt*.0775;
      fLaJollaResult=fLaJolla + fAmt;
      printf("\nThe tax for $%.2f in LaJolla is $%.2f, with total of $%.2f\n", fAmt, fLaJolla, fLaJollaResult);   /*Display results*/
      break;
      
      case 4:
      if (Store_No == 4) exit(0);
      /*printf("Quitting program!\n");
      exit(0);*/                             
      return 0;
      
      default:
      printf("\nSorry, the Store No. you entered was not on the menu. Please try again!\n ");
     }
     }
     }
     }
    You should move the if from the switch to where I have indicated. Not sure but it looks like you have an extra '{' on that line.

  10. #10
    Registered User
    Join Date
    Apr 2010
    Posts
    17
    You are awesome. Thank you so much. It works well. I just need to figure out the code for, when the user input combination of numeric and non-numeric instead of just numeric. That way it will give an invalid when input both together. Thanks a lot. Huge relieve! I have been working on this for like 2 days now.

  11. #11
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Good job! Glad to know you got it to work. Now that you have a stable version it should be easier to add little enhancements here and there.

  12. #12
    Registered User
    Join Date
    Apr 2010
    Posts
    17
    Thank you! I'm so happy. May the Lord continue to bless you, and give you more wisdom and knowledge. You make my day. Now I can go and take care of some other things. Thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. c program help :>
    By n2134 in forum C Programming
    Replies: 9
    Last Post: 02-06-2010, 12:12 PM
  2. End Program
    By jhwebster1 in forum C Programming
    Replies: 7
    Last Post: 02-24-2006, 09:30 AM
  3. Help me please!!!
    By dantestwin in forum C++ Programming
    Replies: 10
    Last Post: 07-21-2004, 11:36 PM
  4. multi file project error part 2
    By dantestwin in forum C++ Programming
    Replies: 6
    Last Post: 07-21-2004, 11:35 PM
  5. can someone check this code
    By xpflyer2002 in forum C Programming
    Replies: 5
    Last Post: 09-16-2002, 03:22 AM