Sam.W Sam.W - 20 days ago 4
C Question

C program cashier system

Well I have only learnt C programming for 2 months. The code I got here is to solve a cashier system. I got some bar code number, item name and price. After cashier types in a numbers of bar codes and finishes with F, a receipt with item number, code and price should be shown.
These are the codes I compiled. However, I can only type one bar code and the program crashed.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main()
{
typedef struct goods
{
char goods_code[6];
char goods_descrip[20];
float price;
}goodtype;

goodtype goods[13];
strcpy(goods[0].goods_descrip, "Chicken");
strcpy(goods[0].goods_code, "00310");
goods[0].price = 35.00;

strcpy(goods[1].goods_descrip, "Pork");
strcpy(goods[1].goods_code, "00311");
goods[1].price = 20.50;

strcpy(goods[2].goods_descrip, "Beef");
strcpy(goods[2].goods_code, "00322");
goods[2].price = 45.00;

strcpy(goods[3].goods_descrip, "Fish");
strcpy(goods[3].goods_code, "00323");
goods[3].price = 40.00;

strcpy(goods[4].goods_descrip, "Walmart T Shirt");
strcpy(goods[4].goods_code, "00510");
goods[4].price = 75.00;

strcpy(goods[5].goods_descrip, "Walmart Trousers");
strcpy(goods[5].goods_code, "00511");
goods[5].price = 120.00;

strcpy(goods[6].goods_descrip, "Walmart Coat");
strcpy(goods[6].goods_code, "00512");
goods[6].price = 100.00;

strcpy(goods[7].goods_descrip, "Walmart Jumper");
strcpy(goods[7].goods_code, "00513");
goods[7].price = 85.00;

strcpy(goods[8].goods_descrip, "Mug");
strcpy(goods[8].goods_code, "00710");
goods[8].price = 15.50;

strcpy(goods[9].goods_descrip, "Fry Pan");
strcpy(goods[9].goods_code, "00711");
goods[9].price = 200.00;

strcpy(goods[10].goods_descrip, "Bowl");
strcpy(goods[10].goods_code, "00712");
goods[10].price = 25.00;

strcpy(goods[11].goods_descrip, "Dish");
strcpy(goods[11].goods_code, "00713");
goods[11].price = 25.00;

char tempCode[6];
char receiptNM[20], receiptCD[6];
char stop[2] = {"F"};
float receiptPC, ttlcost = 0;
unsigned int i;

printf("Please enter the item code. Type F to finish");
scanf("%s", &tempCode);

while ( strcmp(tempCode, stop) ){
for (i = 0; i <= 12; ++i){
if (strcmp(tempCode, goods[i].goods_code) == 0){
strcpy(receiptNM, goods[i].goods_descrip);
strcpy(receiptCD, goods[i].goods_code);
receiptPC = goods[i].price;
ttlcost += goods[i].price;
}
else{
printf("This item code does not exist! Try again!\n");
}
printf("Please enter the item code. Type F to finish");
scanf("%s", &tempCode);
}
printf("_____________________________________\n\n");
printf(" THANK YOU FOR VISITING US! \n");
printf("_____________________________________\n");
printf(" Here is your receipt: \n\n");
printf("%10s%20s%10s", "Item", "Code", "Price");
printf("%10s%20s%10.2f\n", receiptNM, receiptCD, receiptPC);
printf("\n_____________________________________\n");
printf(" TOTAL COST:%.2f \n", ttlcost);
}


I'm struggling with this for hours and cannot fix it out.
An example output should beSample receipt

When I type 00310, the program cannot recognize it. While as I type 310 it can.

Also, when the code typed does not exist, the error message should only display once. But in my programm, it does 12 times.

Answer

You have made couple of mistakes. Let me point out them:- First one pointed by Keine Lust: http://stackoverflow.com/a/40690102/3959321, where you're accessing memory out of bounds of array by iterating to 12 (inclusive). If you've declared an array of 12 elements, the indices range from 0 to 11 and not 0 to 12

for (i = 0; i <= 12; ++i){  //wrong iterations (< 12 should be here)
    if (strcmp(tempCode, goods[i].goods_code) == 0) {
        strcpy(receiptNM, goods[i].goods_descrip);
        strcpy(receiptCD, goods[i].goods_code);
        receiptPC = goods[i].price;
        ttlcost += goods[i].price;
    }
    else {
        printf("This item code does not exist! Try again!\n");
    }
    printf("Please enter the item code. Type F to finish");
    scanf("%s", &tempCode);
}

Your program definitely will print "This item code doesn't exist!..." 12 times, as the else part is associated with an if and nested inside a for loop. So, it will be called each time when item is not found for 12 iterations of the loop.

The correct program is:-

----   //Rest above is same
unsigned int i;
printf("Please enter the item code. Type F to finish");
scanf("%s", tempCode);

while ( strcmp(tempCode, stop) ) {
    for (i = 0; i < 12; ++i){
        if (strcmp(tempCode, goods[i].goods_code) == 0){
            strcpy(receiptNM, goods[i].goods_descrip);
            strcpy(receiptCD, goods[i].goods_code);
            receiptPC = goods[i].price;
            ttlcost += goods[i].price;
            break;
        }
    }
    if (i==12)   //The loop iteration is complete, and i becomes 12 only when the for above hasn't been breaked (same as item found)
        printf("This item code does not exist! Try again!\n");
    printf("Please enter the item code. Type F to finish");
    memset(tempCode,'\0',sizeof(tempCode));
    scanf("%s", tempCode);
}

You are also making a mistake in the final reciept part:-

printf("%10s%20s%10s", "Item", "Code", "Price");
printf("%10s%20s%10.2f\n", receiptNM, receiptCD, receiptPC); //receiptNM, receiptCD and receiptPC are only single character arrays. 

They will store only one string unlike your expectation. In order to store all of them you make an array of a structure (that contains item code, name of item, and price). Follow GumBoy's answer:- http://stackoverflow.com/a/40690211/3959321