Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Basic C Programming Help! Cash Register

  1. Feb 6, 2013 #1
    Hello, I am working on a basic cash register c program and I've run into a few errors/logical errors I don't know where to begin to fix.

    I am limited to knowing only how to use, scant, printf, if/elseif/else, do/dowhile. The problem is if you compile it and test it, my validcode flag is not working. It seems that the entered product code is always seen as valid. For example if I enter either 9999 or 999999, the program still sees it as valid and prompts for quantity.

    Why is this? I have
    Code (Text):
                //Check if 5 digits
                if (code < 10000 && code > 99999){
                    validCode = 0;
                }
                else
                {
                    validCode = 1;
                }
    So why is it seeing it as valid all the time? If I enter an invalid code it keeps asking for quantity and entering item code in a loop all over again.

    How can I go about fixing this?

    Code (Text):

    #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 = 0;
        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;
                }
                else
                {
                    validCode = 1;
                }
                //Check 1st digit
                if (code / 10000 == 7)
                {
                    validCode = 0;
                }
                else
                {
                    validCode = 1;
                }
                if (code / 10000 == 8)
                {
                    validCode = 0;
                }
                else
                {
                    validCode = 1;
                }
                if (code / 10000 == 9)
                {
                    validCode = 0;
                }
                else
                {
                    validCode = 1;
                }
               
                //Check 2nd digit
                if (code / 1000 % 10 > 4)
                {
                    validCode = 0;
                }
                else
                {
                    validCode = 1;
                }
                if (code / 10000 == 6 && code / 1000 % 10 > 0)
                {
                    validCode = 0;
                }
                else
                {
                    validCode = 1;
                }
               
                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
           
            if ((validCode == 1 && code != 0) || code != 00){
            printf("Enter Quantity: ");                                     // prompt quantity
            scanf("%f", &quantity);                                         // retrieve from keyboard
            }
            if (code / 10000 == 1 && code / 1000 % 10 == 1){
                price = (float)(code % 1000) / 100;
                printf("\t > Produce Bulk, local vegetables, $%.2f/lb @ %.1f = %15.2f\n", price, quantity, price * quantity );
                subTotal = subTotal + price * quantity;
            }
            else if (code / 10000 == 1 && code / 1000 % 10 == 2){
                price = (float)(code % 1000) / 100;
                printf("\t > Produce Bulk, local fruits, $%.2f/lb @ %.1f = %19.2f\n", price, quantity, price * quantity );
                subTotal = subTotal + price * quantity;
            }
            else if (code / 10000 == 1 && code / 1000 % 10 == 3){
                price = (float)(code % 1000) / 100;
                printf("\t > Produce Bulk, imports, $%.2f/lb @ %.1f = %24.2f\n", price, quantity, price * quantity );
                subTotal = subTotal + price * quantity;
            }
            else if (code / 10000 == 2 && code / 1000 % 10 == 1){
                price = (float)(code % 1000) / 100;
                printf("\t > Produce Bulk, local vegetables, $%.2f each @ %.1f = %13.2f\n", price, quantity, price * quantity );
                subTotal = subTotal + price * quantity;
            }
            else if (code / 10000 == 2 && code / 1000 % 10 == 2){
                price = (float)(code % 1000) / 100;
                printf("\t > Produce Bulk, local fruits, $%.2f each @ %.1f = %17.2f\n", price, quantity, price * quantity );
                subTotal = subTotal + price * quantity;
            }
            else if (code / 10000 == 2 && code / 1000 % 10 == 3){
                price = (float)(code % 1000) / 100;
                printf("\t > Produce Bulk, imports, $%.2f each @ %.1f = %22.2f\n", price, quantity, price * quantity );
                subTotal = subTotal + price * quantity;
            }
            else if (code / 10000 == 3 && code / 1000 % 10 == 1){
                price = (float)(code % 1000) / 100;
                printf("\t > Floral, roses, $%.2f each @ %.1f = %30.2f\n", price, quantity, price * quantity );
                subTotal = subTotal + price * quantity;
            }
            else if (code / 10000 == 3 && code / 1000 % 10 == 2){
                price = (float)(code % 1000) / 100;
                printf("\t > Floral, bouquet, $%.2f each @ %.1f = %28.2f\n", price, quantity, price * quantity );
                subTotal = subTotal + price * quantity;
            }
            else if (code / 10000 == 3 && code / 1000 % 10 == 3){
                price = (float)(code % 1000) / 100;
                printf("\t > Floral, house plant, $%.2f each @ %.1f = %24.2f\n", price, quantity, price * quantity );
                subTotal = subTotal + price * quantity;
            }
            else if (code / 10000 == 4 && code / 1000 % 10 == 1){
                price = (float)(code % 1000) / 100;
                printf("\t > Dairy, milk, $%.2f each @ %.1f = %32.2f\n", price, quantity, price * quantity );
                subTotal = subTotal + price * quantity;
            }
            else if (code / 10000 == 4 && code / 1000 % 10 == 2){
                price = (float)(code % 1000) / 100;
                printf("\t > Dairy, eggs, $%.2f each @ %.1f = %32.2f\n", price, quantity, price * quantity );
                subTotal = subTotal + price * quantity;
            }
            else if (code / 10000 == 4 && code / 1000 % 10 == 3){
                price = (float)(code % 1000) / 100;
                printf("\t > Dairy, butter, $%.2f each @ %.1f = %30.2f\n", price, quantity, price * quantity );
                subTotal = subTotal + price * quantity;
            }
            else if (code / 10000 == 4 && code / 1000 % 10 == 4){
                price = (float)(code % 1000) / 100;
                printf("\t > Dairy, yogurt, $%.2f each @ %.1f = %30.2f\n", price, quantity, price * quantity );
                subTotal = subTotal + price * quantity;
            }
            else if (code / 10000 == 5 && code / 1000 % 10 == 1){
                price = (float)(code % 1000) / 100;
                printf("\t > Meats, beef, $%.2f/lb @ %.1f = %34.2f\n", price, quantity, price * quantity );
                subTotal = subTotal + price * quantity;
            }
            else if (code / 10000 == 5 && code / 1000 % 10 == 2){
                price = (float)(code % 1000) / 100;
                printf("\t > Meats, chicken, $%.2f/lb @ %.1f = %31.2f\n", price, quantity, price * quantity );
                subTotal = subTotal + price * quantity;
            }
            else if (code / 10000 == 5 && code / 1000 % 10 == 3){
                price = (float)(code % 1000) / 100;
                printf("\t > Meats, turkey, $%.2f/lb @ %.1f = %32.2f\n", price, quantity, price * quantity );
                subTotal = subTotal + price * quantity;
            }
            else if (code / 10000 == 5 && code / 1000 % 10 == 4){
                price = (float)(code % 1000) / 100;
                printf("\t > Meats, pork, $%.2f/lb @ %.1f = %34.2f\n", price, quantity, price * quantity );
                subTotal = subTotal + price * quantity;
            }
            else if (code / 10000 == 5 && code / 1000 % 10 == 5){
                price = (float)(code % 1000) / 100;
                printf("\t > Meats, rabbit, $%.2f/lb @ %.1f = %32.2f\n", price, quantity, price * quantity );
                subTotal = subTotal + price * quantity;
            }
            else if (code / 10000 == 6 && code / 1000 % 10 == 0){
                price = (float)(code % 1000) / 100;
                printf("\t > Other, other item, $%.2f each @ %.1f = %26.2f\n", price, quantity, price * quantity );
                subTotal = subTotal + price * quantity;
            }
        }while(code !=0);                                 //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\n");
       
        printf("Enter Cash Amount: ");
        scanf("%f", &paidCash);
       
        printf("\t> CHANGE TO CUSTOMER DUE: $%.2f\n\n", paidCash - subTotal * 1.07);
       
        //Display Footer
        PrintFooter();
       
        return 0;
    }
     
     
  2. jcsd
  3. Feb 6, 2013 #2

    jedishrfu

    Staff: Mentor

    okay after your scanf (...code...) line why not print the code value to see if it got what you expect. I suspect its not.

    between each if statement I'd add prints to print the validCode boolean value to see if its getting reset to zero.
     
  4. Feb 6, 2013 #3
    I actually just tried that and after each item code, it was coming out as "validCode = 1" every single time regardless of if it was a invalid code or not.
    I put "printf("The validCode is %d", validCode); and it always returned The validCode is 1.
    I can't figure out why it would not set validCode = 0?
     
  5. Feb 6, 2013 #4

    CompuChip

    User Avatar
    Science Advisor
    Homework Helper

    It will be hard for any number to be < 10000 and > 99999 at the same time.
    Are you sure you did not mean logical or?
     
  6. Feb 6, 2013 #5
    Yeah I caught that earlier, I changed it to || instead.

    Also I now added a printf like after EACH check of validation.

    It showed up as : The validCode is 0. The validCode is 1. The validCode is 1. The validCode is 1. The validCode is 1.

    So now it seems to me that although it does change the bool flag, how can I make it so that as SOON as validCode is set to 0, it displays invalid code and prompts for another? Instead of going through each condition and changing the flag?
     
  7. Feb 6, 2013 #6

    jedishrfu

    Staff: Mentor

    add in another condition that says if (validCode==1 && ...) to the subsequent ifs
     
  8. Feb 6, 2013 #7

    berkeman

    User Avatar

    Staff: Mentor

    One possible source of error is the order of precedence in this line:

    if (code < 10000 && code > 99999)

    I'd try adding more parens to that line to ensure that it is not evaluated strictly left-to-right...

    http://www.swansontec.com/sopc.html

    .
     
  9. Feb 6, 2013 #8
    Oh wow, I'm an idiot! I figured it out. It was definitely a logical error.

    I had to removed all of the else statements because I basically made it, else the invalid code is STILL valid. Stupid of me.

    But now, I have a second issue. My exit code is 00 or 0, and now it's saying that 0 is an invalid code even though I have
    Code (Text):

            if ((validCode == 1 && code != 0) || code != 00){
            printf("Enter Quantity: ");                                     // prompt quantity
            scanf("%f", &quantity);                                         // retrieve from keyboard
            }
     
  10. Feb 6, 2013 #9

    Borek

    User Avatar

    Staff: Mentor

    Check the "code" value, not "validCode" value.

    In general what jedishrfu suggested is a simple, but effective approach: check values of the variables involved during execution of your program and see if the program does what you think it does.

    Instead of using printf you can as well use a debugger.
     
  11. Feb 6, 2013 #10
    Ahh thanks for all the input!

    I have everything fixed and running smoothly now. The last thing that I want to attempt to do, but is probably out of the scope of what I've learned so far is.

    When I use scanf, it waits for an integer. How can I make it so that if I type a character instead of a number accidentally it will not be stuck in an infinitely nonexitable loop?

    My approach would be to maybe return it as an invalid code if the returned scanned value is not an integer? But how can I do that?
     
  12. Feb 6, 2013 #11

    jedishrfu

    Staff: Mentor

    Read your input as a string and then check if all characters are digits before converting it.
     
  13. Feb 6, 2013 #12
    Aah, okay.

    Last thing that I wanted to ask was, how can I fix my alignment at the end?

    "%15.2f" would show a value aligned to the right of 15 spaces, how can I make it have a $ sign?

    I tried putting a $ sign after the conversion specifier but obviously that's an error. Right now it just shows as :
    : $ 8.99
    instead of
    : $8.99
    How can I go about fixing this in my "%15.2f" portion?
     
  14. Feb 6, 2013 #13

    jedishrfu

    Staff: Mentor

    how about %-15.2f ?
     
  15. Feb 6, 2013 #14
    Err it didn't show that I mean I think

    Code (Text):

    : $             8.99
    :              $8.99
     
    How can I make it like the bottom one?
    %$15.2f obviously doesn't work, and $%15.2f doesn't include the $ sign in the alignment, which is what the top one is.
     
  16. Feb 6, 2013 #15

    jedishrfu

    Staff: Mentor

    I think its $%-15.2f not $%15.2f (see the minus sign?)
     
  17. Feb 6, 2013 #16
    That will cause my print to not align to the right.
     
  18. Feb 6, 2013 #17
    Code (Text):
    Enter Item Code: 54199
    Enter Quantity: 2
             > Meats, pork, $1.99/lb @ 2.0 = $                              3.98
    Enter Item Code: 55199
    Enter Quantity: 2
             > Meats, rabbit, $1.99/lb @ 2.0 = $                            3.98
    Enter Item Code: 56199
    <INVALID CODE, PLEASE TRY AGAIN>
    Enter Item Code: 0
    That is my output, see how the $ signs do not align next to the 3.98? I want to figure out how to move my $'s out there.
     
  19. Feb 6, 2013 #18

    jedishrfu

    Staff: Mentor

    Okay then try strfmon() that is a locale specific way of handling monetary values:

    http://linux.die.net/man/3/strfmon

    by locale I mean if you're in the US the $999,999,999.99 vs Europe $999.999.999,99 or something to that effect.

    Another way is to use sprintf and then take the string trim spaces and prepend a $ to it then print with %15s ...
     
  20. Feb 6, 2013 #19

    chiro

    User Avatar
    Science Advisor

    Hey Saterial.

    What is your code to print the string?
     
  21. Feb 7, 2013 #20

    Mark44

    Staff: Mentor

    Although more parentheses won't hurt, they're not needed. The comparison operators < and > are higher precedence than the logical and operator &&, so the effect is as if it were written "if ( (code < 10000) && (code > 99999))".

    Also, the && operator is guaranteed to be evaluated left to right, which supports a sort of "lazy" evaluation. If it is determined that code < 10000, then the overall expression can't possibly be true, so the other comparison isn't performed.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: Basic C Programming Help! Cash Register
  1. C program help (Replies: 31)

  2. C program help (Replies: 3)

  3. Help in C programming (Replies: 1)

Loading...