Basic C Programming Help Cash Register

In summary, the conversation is about a person working on a basic cash register c program and encountering errors with the validcode flag not working. They are limited to using certain functions and are unsure of how to fix the issue with the program always seeing the entered product code as valid. They have provided a section of their code for reference.
  • #1
Saterial
54
0
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:
            //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:
#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 statements 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;
}
 
Technology news on Phys.org
  • #2
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.
 
  • #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?
 
  • #4
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?
 
  • #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?
 
  • #6
add in another condition that says if (validCode==1 && ...) to the subsequent ifs
 
  • #7
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

.
 
  • #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:
        if ((validCode == 1 && code != 0) || code != 00){
        printf("Enter Quantity: ");                                     // prompt quantity
        scanf("%f", &quantity);                                         // retrieve from keyboard
        }
 
  • #9
Saterial said:
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?

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.
 
  • #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?
 
  • #11
Saterial said:
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?

Read your input as a string and then check if all characters are digits before converting it.
 
  • #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
Err it didn't show that I mean I think

Code:
: $             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.
 
  • #15
Saterial said:
Err it didn't show that I mean I think

Code:
: $             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.

I think its $%-15.2f not $%15.2f (see the minus sign?)
 
  • #16
That will cause my print to not align to the right.
 
  • #17
Code:
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.
 
  • #18
Saterial said:
That will cause my print to not align to the right.

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 ...
 
  • #19
Hey Saterial.

What is your code to print the string?
 
  • #20
berkeman said:
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...
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.
berkeman said:
 
  • #21
Saterial said:
Code:
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.

Minor quibble...

Your use of @ in your output is the reverse of how I usually see it, which would be
2.0 @ $1.99/lb = $3.98
or
2.0 lb @ $1.99/lb = $3.98

The number after @ is the unit price.
 
  • #22
Mark44 said:
Minor quibble...

Your use of @ in your output is the reverse of how I usually see it, which would be
2.0 @ $1.99/lb = $3.98
or
2.0 lb @ $1.99/lb = $3.98

The number after @ is the unit price.

This may be a cultural difference and not reversed output.
 

1. What is a cash register in C programming?

A cash register in C programming is a basic program that simulates the functions of a real-life cash register. It allows users to enter the price of items, calculate the total cost, and provide change.

2. How do I create a cash register program in C?

To create a cash register program in C, you will need to use basic programming concepts such as variables, loops, and conditional statements. You will also need to use the scanf() and printf() functions to take input and display output.

3. What are the essential features of a cash register program in C?

The essential features of a cash register program in C include the ability to calculate the total cost of items, handle different payment methods, provide change, and keep track of the transaction history.

4. How can I improve the functionality of my cash register program in C?

To improve the functionality of your cash register program in C, you can add features such as discounts, tax calculation, multiple payment options, and the ability to store and retrieve transaction data.

5. Are there any resources or tutorials available for learning how to create a cash register program in C?

Yes, there are many online resources and tutorials available for learning how to create a cash register program in C. You can find step-by-step guides, video tutorials, and practice exercises on various websites and programming forums.

Similar threads

  • Programming and Computer Science
Replies
9
Views
1K
  • Programming and Computer Science
Replies
5
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
12
Views
1K
  • Programming and Computer Science
Replies
1
Views
944
  • Programming and Computer Science
2
Replies
47
Views
4K
  • Introductory Physics Homework Help
Replies
11
Views
767
  • Programming and Computer Science
Replies
5
Views
3K
  • Programming and Computer Science
Replies
22
Views
2K
Replies
10
Views
960
  • Programming and Computer Science
Replies
3
Views
1K
Back
Top