# Basic C Programming Help! Cash Register

1. Feb 6, 2013

### Saterial

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 PrintFooter();

{
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;

//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. Feb 6, 2013

### 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.

3. Feb 6, 2013

### Saterial

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. Feb 6, 2013

### CompuChip

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. Feb 6, 2013

### Saterial

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. Feb 6, 2013

### Staff: Mentor

add in another condition that says if (validCode==1 && ...) to the subsequent ifs

7. Feb 6, 2013

### 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

.

8. Feb 6, 2013

### Saterial

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
}

9. Feb 6, 2013

### 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.

10. Feb 6, 2013

### Saterial

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. Feb 6, 2013

### Staff: Mentor

Read your input as a string and then check if all characters are digits before converting it.

12. Feb 6, 2013

### Saterial

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?

13. Feb 6, 2013

### Staff: Mentor

how about %-15.2f ?

14. Feb 6, 2013

### Saterial

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. 15. Feb 6, 2013 ### jedishrfu ### Staff: Mentor I think its%-15.2f not %15.2f (see the minus sign?) 16. Feb 6, 2013 ### Saterial That will cause my print to not align to the right. 17. Feb 6, 2013 ### Saterial 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. 18. Feb 6, 2013 ### 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 ...

19. Feb 6, 2013

### chiro

Hey Saterial.

What is your code to print the string?

20. Feb 7, 2013

### 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.