Thread: Basic C Help - Cash Register

  1. #1
    Registered User
    Join Date
    Feb 2013
    Posts
    19

    Basic C Help - Cash Register

    As you can see, I test if there is a valid code that is : 5 digits, first digit must be between 1-6, 2nd digit is between 1-4 depending on the first digit, and 00 computes total.

    I haven't finished coding the portion of what to do if a valid code is entered yet but, when I test it with an invalid code, it keeps looping "Enter Item Code:" or previously it would say "<INVALID CODE, PLEASE TRY AGAIN" and then it would say "Enter Quantity:" instead of going back to "Enter Item Code:" can anyone help?

    Code:
    SAMPLE OUTPUT
    ----------------------------------------------------------------------
    _-=== Windsor Market Farm Fresh ===-_
    Welcome!
    Enter Item code: 12199
    Enter Quantity: 2.5
    > Produce Bulk, local fruits, $1.99/lb @ 2.5 = $4.98
    Enter Item code: 44099
    Enter Quantity: 3
    > Dairy, yogurt, $0.99 each @ 3 = $2.97
    Enter Item code: 4444
    <INVALID CODE, PLEASE TRY AGAIN>
    Enter Item code: 00
    ----------------------------------------------------------------------
    > SUB-TOTAL $7.95
    > TAX@7% $0.56
    ----------------------------------------------------------------------
    > TOTAL $4.45
    ----------------------------------------------------------------------
    Enter CASH AMOUNT: 20
    >CHANGE TO CUSTOMER DUE: $15.55
    ----------------------------------------------------------------------
    THANK YOU FOR SHOPPING AT WINDSOR MARKET FARM FRESH, PLEASE COME AGAIN
    ----------------------------------------------------------------------
    Code:
    #include <stdio.h>
    
    
    //function prototypes
    void PrintHeader();
    void PrintFooter();
    
    
    void PrintHeader()
    {
        printf("---------------------------------------------------------------------\n\n");
        printf("                _-=== Windsor Market Farm Fresh ===-_\n\n");
        printf("Welcome!\n");
    }
    
    
    
    
    void PrintFooter()
    {
        printf("---------------------------------------------------------------------\n\n");
        printf("THANK YOU FOR SHOPPING AT WINDSOR MARKET FARM FRESH, PLEASE COME AGAIN\n\n");
        printf("---------------------------------------------------------------------\n");
    }
    
    
    int main()
    {
        int code;     //use this variable to enter the product codes
        float quantity;
        float subTotal;
        float paidCash;
        //float price;
        int validCode;
        
        //Display Heading
        PrintHeader();
        
        //Main Program loop
        do
        {
            do
            {
            printf("Enter Item Code: ");                //prompt for item code
            scanf("%d", &code);                         //retrieve input from keyboard
            
            
            //Check if 5 digits
            if (code < 10000 && code > 99999)
                validCode = 0;
            
            //Check 1st digit
            else if (code / 10000 == 7)
                validCode = 0;
            else if (code / 10000 == 8)
                validCode = 0;
            else if (code / 10000 == 9)
                validCode = 0;
            
            //Check 2nd digit
            else if (code / 1000 % 10 > 4)
                validCode = 0;
            else if (code / 10000 == 6 && code / 1000 % 10 > 0)
                validCode = 0;
            
            if (validCode == 0)                                            //if code is invalid, it will loop back to the beginning and ask for item code again
                printf("<INVALID CODE, PLEASE TRY AGAIN>\n");
            
            }while (validCode == 1);                                            // if code is valid, prompt for quantity
                printf("Enter Quantity: ");                                     // prompt quantity
                scanf("%f", &quantity);                                         // retrieve from keyboard
            
            
                }while(code !=0 || code != 00);                                 //loop will be repeated until item code 0 is entered
        
        //print and calculation statments to determine subtotal, total, tax and change
        printf("---------------------------------------------------------------------\n");
        printf("\t> SUB-TOTAL\t %.2f\n", subTotal);
        printf("\t> TAX@7%%\t %.2f\n", subTotal * 0.07);
        printf("---------------------------------------------------------------------\n");
        printf("\t> TOTAL\t %.2f\n", subTotal * 1.07);
        printf("---------------------------------------------------------------------\n");
        
        printf("Enter Cash Amount: ");
        scanf("%f", &paidCash);
        
        printf("\t> CHANGE TO CUSTOMER DUE: $%.2f\n", paidCash - subTotal * 1.07);
        
        //Display Footer
        PrintFooter();
    
    
        return 0;
    }

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Compile with warnings turned all the way up:
    Code:
    $ make foo
    gcc -Wall -Wunreachable-code -ggdb3 -std=c99 -pedantic  -lm -lpthread  foo.c   -o foo
    foo.c: In function ‘main’:
    foo.c:78: warning: ‘subTotal’ is used uninitialized in this function
    Note, that is not the cause of the problem you mentioned, but it is a problem.

    Your problem is that you use scanf, but don't check to see if it succeeded before checking for valid input. If you enter some letters, like "asdf", then scanf does not see a number, and thus can't store anything in your code variable. It is left with the same value it previously had, the loop body executes, and you're back to scanf, which again finds letters instead of a number, and the cycle repeats. One thing that would help would be to check the return value of scanf before you proceed, make sure it successfully found a number:
    Code:
    if (scanf("%d", &code) == 1)  // it returns the number of items successfully scanned
    But personally, I think it would be easier to read a line of text as a string from the user with fgets. Then, you can check the first digit with something like
    Code:
    char code[BUFSIZ];
    if (fgets(code, sizeof(code), stdin) == NULL)
        // error or EOF
    if (code[0] == '7' || code[0] == '8' || code[0] == '9')
        // invalid
    If you need to, at some point, turn that code into an integer, to do some math on it, you can use the strtol function, which has good error checking.

  3. #3
    Registered User
    Join Date
    Feb 2013
    Posts
    19
    As this is an intro to C Programming class, I'm strictly limited to using the stdio library, with printf/scanf/if/elseif,dowhile; nothing else as it is what we have spoke about in class so far.

  4. #4
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Quote Originally Posted by Bernard Wu View Post
    As this is an intro to C Programming class, I'm strictly limited to using the stdio library, with printf/scanf/if/elseif,dowhile; nothing else as it is what we have spoke about in class so far.
    Well, fgets is part of the stdio library, just like printf and scanf, and while strtol is not (it is part of stdlib), you probably don't really need it. Also, as a matter of correctness, if, else, do...while are not part of the stdio library, they are part of the C language itself, special keywords for controlling program flow.

    If you really must use only scanf for input, then I gave you one suggestion that will help avoid an infinite loop, check the return value of scanf. That still has problems though, you need to find a way of getting rid of the non-numeric data, so that subsequent calls to scanf might succeed. One way to do that might be to flush the input buffer, check out this link. Note, you will have to modify it to use scanf, if that is the only input function you are allowed to use.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ Cash Register Help
    By nealw in forum C++ Programming
    Replies: 5
    Last Post: 10-11-2011, 08:46 PM
  2. help me fix one problem with my cash register
    By lil_rom in forum C Programming
    Replies: 3
    Last Post: 04-14-2008, 12:03 AM
  3. help me fix one problem with my cash register
    By lil_rom in forum C++ Programming
    Replies: 4
    Last Post: 04-13-2008, 02:15 PM
  4. Cash register program help :(
    By lil_rom in forum C Programming
    Replies: 2
    Last Post: 04-11-2008, 12:35 AM
  5. Cash Register
    By Hursh in forum C Programming
    Replies: 5
    Last Post: 01-22-2002, 03:36 AM