-3

I hope this question makes sense. I am a beginner on C, and am attempting to make a food ordering system for university. I need to make it so with each order a receipt is generated from B001 to B0025, but so far the receipt number will not count up from B001 with any new orders. Any help would be greatly appreciated!!

My ideas was By converting r_num = 1 in to a string and then by using strcat to concatenate the newly made string to "B" which equalled B00, this would create the receipt number of B001. I then use strcpy to copy this string into the array orders[i] which is attached to my structure burger, and as i = 0 it would go into the first element of the array. Down the bottom of the code I attempted to use r_num +1 to then to make r_num = 2 so when the while loop returns to the start the process would begin again but once concatenated B002 would be created. Sorry if this makes no sense Im new at this

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

struct burger
{
    char receipt_no[4];
    int quantity;
    float total_price;
};

int main()
{
    struct burger orders[25];
    char receipt[2], B[3] = "B00";
    int option, price = 12, i=0, exit_switch = 0, r_num = 1;
    float bill, percent;
    while(exit_switch==0)
    {
        sprintf(receipt, "%d", r_num);
        strcat(B, receipt);
        strcpy(orders[i].receipt_no, B);
        printf("Welcome to Tasty Burger\nPlace your order here...\n1. Order Burger\n2. Cancel\n3. Exit\n");
        scanf("%d", &option);
        switch (option)
        {
            case 1:
        {
            printf("How many burgers do you wish to order?\n");
            scanf("%d", &orders[i].quantity);
            if (orders[i].quantity >= 5){
                bill = orders[i].quantity * price;
                percent = bill / 10;
                orders[i].total_price = bill - percent;
                printf("Your total bill value is $%.2f\nDiscount of 10%% is $%.2f\nFinal bill value is $%.2f\n", bill, percent, orders[i].total_price);
                printf("Your receipt number is %s\n", orders[i].receipt_no);
                printf("Please go to a register and make the payment by quoting the Receipt Number – %s\n", orders[i].receipt_no);
                }
        else {
            orders[i].total_price = orders[i].quantity * price;
            printf("Your total bill value is $%.2f\n", orders[i].total_price);
            printf("Your receipt number is %s\n", orders[i].receipt_no);
            printf("Please go to a register and make the payment by quoting the Receipt Number – %s\n", orders[i].receipt_no);
        }
        }
        
    }
    r_num = r_num + 1;
    i++;
    }

    return 0;
}
  • It would help to understand how and why you expected this to work. For example, how are you expecting `printf` to know how many characters to print for the receipt nujmber in this code? `printf("Your receipt number is %s\n", orders[i].receipt_no);` – David Schwartz May 11 '22 at 02:37
  • 1
    Have you tried running your code line by line in a debugger while monitoring the values of all variables, in order to determine at which point your program stops behaving as intended? If you did not try this, then you may want to read this: [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/q/25385173/12149471) You may also want to read this: [How to debug small programs?](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) – Andreas Wenzel May 11 '22 at 02:38
  • @DavidSchwartz By converting r_num = 1 in to a string and then by using strcat to concatenate the newly made string to "B" which equalled B00, this would create the receipt number of B001. I then use strcpy to copy this string into the array orders[i] which is attached to my structure burger, and as i = 0 it would go into the first element of the array. Down the bottom of the code I attempted to use r_num +1 to then to make r_num = 2 so when the while loop returns to the start the process would begin again but once concatenated B002 would be created. Sorry if this makes no sense Im new at this – Spindaddy 3000 May 11 '22 at 03:33
  • @Spindaddy3000: Might be easier to just use a normal integer in the structure for the receipt number, and then convert it to a string immediately before printing (or maybe while printing, like `printf( "B%03d", orders[i].receipt_no)`). – Brendan May 11 '22 at 05:50
  • Most of your string operations will result in a buffer overflow. You need to read the chapter dealing with strings in your C text book. E.g if you declare `char B[3];`, `B` can contain strings of at most length 2. `B` is nothing more than a fixed size array, you cannot extend it. – Jabberwocky May 11 '22 at 07:04

0 Answers0