Thread: Loop issue in tax program

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    7

    Loop issue in tax program

    Below is my code. The problem is the program does compile and run, but it is not right. When u enter a numeric value it gives u the tax for all 3 locations; however, when you enter a letter or letters it lets u loop back to re enter a correct number and it will loop back several times if u continue to enter letters instead of numbers. The problem is if after u enter letters and it loops back, if you enter a number the program still thinks its an invalid entry. So what am i doing wrong. Please exlain to me what i am doing wrong so I can fix this issue, thanks.

    Code:
    #include <stdio.h> // Include standard I/O libraries
    #include <stdlib.h> // Include standard libraries
    #include <math.h> // Include Math libraries
    
    /* Defining tax rates for Kudler's three store */
        float DelMarRate = 7.25;    /* Del Mar tax rate */
        float EncinitasRate = 7.5;    /* Encintas tax rate */
        float LaJollaRate = 7.75;    /* LaJolla tax rate */
        
    /* Variables for Kudler's three store's tax calculation */   
        float DelMarTax = 0.0;        /* Calculation variable for Del Mar taxes */
        float EncinitasTax = 0.0;      /* Calculation variable for Encintas taxes */
        float LaJollaTax = 0.0;       /* Calculation variable for LaJolla taxes */
        float Amount = 0.0;
    
    int main(void)
    {
    char Purchase[32]; /* Declare variables */
    
    int i,Return=0; 
    
        /* Prompt user for Purchase Amount */
        printf("\n*********************************");
        printf("\n* Welcome to Kudler Fine foods! *");
        printf("\n*********************************");
        printf("\n\n");
        printf("\n\nPlease Enter the customer's purchase amount:$");
        scanf("%s", Purchase);
    
    do{
            /* Loop - Test the numeric values*/ 
            for(i=0; Purchase[i]; i++) 
            {
                if(Purchase[i] < '0' || Purchase[i] > '9') /* Test for a numeric value (0-9) */
                {
                if (Purchase[i] == '.') /* If character is a decimal continue */
                continue;
                    else
                    Return=1; /* Set Non-Numerical Value flag */
                    break; 
                } 
            }
    
                if (Return == 1) /* If Non-Numerical Value print Error Message and allow user to re-enter purchase price */
                {
                    printf("\n\nError! Invalid Purchase Amount [ %s ]\n",Purchase);
                    fflush(stdin);
                    printf("\n\nPlease Re-enter Purchase Amount:$"); 
                    scanf("%s", Purchase); 
                    
                } 
                                 
    }while (Return == 1);
    
    Amount=atof(Purchase); /* Convert to Float */ 
    DelMarTax=(Amount*DelMarRate)/100; /* Calculate Tax Amount for Del Mar */
    EncinitasTax=(Amount*EncinitasRate)/100; /* Calculate Tax Amount for Encinitas */
    LaJollaTax=(Amount*LaJollaRate)/100; /* Calculate Tax Amount for La Jolla */
    
        /* Display Tax Calculation output to the screen */
        printf("\n\n\n");
        printf("Del Mar\t\tEncinitas\tLa Jolla\t\n");
        printf("*******************************************\n");
        printf("$%.2f\t\t$%.2f\t\t$%.2f\n", DelMarTax, EncinitasTax, LaJollaTax);
        printf("\n\nPlease Press The Enter Key To Exit Program!\n\n");
        fflush(stdin);
        
        /*Keeping DOS Screen open so user can view output*/
            scanf("%c\n");    
        
            return 0;
        
    }

  2. #2
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    The issue is with scanf(). Clear the input buffer when a wrong value is entered. Search the FAQ.
    Mainframe assembler programmer by trade. C coder when I can.

  3. #3
    Registered User
    Join Date
    Oct 2009
    Posts
    7

    which scan f

    Okay, which scanf(). The one under the re-enter purchase amount? and how do i clear the input buffer when an incorrect value is entered? Thanks.

  4. #4
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    I would do both so that it doesn't matter where the user screws up.

    And the FAQ will tell you the rest; look at the top of our page.

  5. #5
    Registered User
    Join Date
    Oct 2009
    Posts
    7
    Thanks the program works beautifully i forgot to add a return = 0; after the do statement. I figured it out on my own.

    ONE NOTE: IF ANYONE READS THIS FORUM AND IS IN CSS 561 PLZ DO NOT COPY MY CODE AS THE POSTED CODE IS MY ORIGINAL WORK. IF THE CODE IS COPIED AND I AM CONTACTED BY THE INSTRUCTOR I WILL SHOW THIS THREAD TO MY INSTRUCTOR.

    Amanda Smith

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 18
    Last Post: 07-06-2009, 07:12 AM
  2. Mutiliplcation Program that uses a (do, while, and for loop)?
    By Debbie Bremer in forum C++ Programming
    Replies: 4
    Last Post: 10-11-2008, 06:04 PM
  3. Using While Loop to write a program
    By Cyberman86 in forum C++ Programming
    Replies: 2
    Last Post: 09-16-2008, 12:52 PM
  4. Help with Tax calculator program
    By Mshock in forum C++ Programming
    Replies: 1
    Last Post: 06-28-2006, 03:28 PM
  5. Help with sample tax program
    By DaMonsta in forum C Programming
    Replies: 1
    Last Post: 05-15-2003, 03:45 AM