0

I'm making this Pizza order system code where the Customer chooses the type and the size of the pizza and also can order up to 10 pizza's. The problem is that the program is skipping the last printf() statement the one inside the if statement within the while loop which asks the customer whether he wants more than one pizza.I didn't copy all the functions because it will be really long code but if the problem could be there then I paste them directly to see.

I tried to write just printf() statement without the if statement and still being skipped.

int main()
{

    printf("Hello! Welcome to UASKPS!\n");
    printf("You can order up to 10 pizzas.\n\n");
    printf("\nPress any key to start your order.\n");
    _getch();

    // let user order a single pizza in each loop run
    char c;
    nr_pizzas_ordered = 0;
    do
    {
        system("cls");

        // show pizza number
        printf("\nPlease choose your pizza #%d\n", nr_pizzas_ordered + 1);

        //  let user choose pizza type
        enum pizza_types desired_type = choose_pizza_type();

        //  let user choose pizza size
        enum pizza_sizes desired_size = choose_pizza_size();

        //  store the order
        all_pizza_orders[nr_pizzas_ordered].which_pizza = desired_type;
        all_pizza_orders[nr_pizzas_ordered].which_size = desired_size;

        //  one pizza has been ordered more
        nr_pizzas_ordered++;

        // ask user whether he wants to order another pizza

        if (nr_pizzas_ordered < 10)
        {
            printf("Do you want to order another pizza? (y/n)\n");
            c = _getch();
        }
        else
            c = 'n';

    } while (c == 'y');


    show_order_list();

    printf("\n\nThank you for your order! Press any key to exit.\n");
    _getch();

} 
bruceg
  • 2,433
  • 1
  • 22
  • 29
Dr.Noob
  • 319
  • 6
  • 17
  • 1
    Please fix your code format. – miike3459 Jan 04 '19 at 22:16
  • 2
    How are reading the number of pizzas from the user? And what value is being printed out? – John Szakmeister Jan 04 '19 at 22:16
  • 1
    Might be buffering. See [why does “printf” not work?](https://stackoverflow.com/q/39180642/10077). – Fred Larson Jan 04 '19 at 22:21
  • is nr_pizzas_ordered touched in another function? – Peter L. Jan 04 '19 at 22:21
  • 1
    How is `all_pizza_orders` defined? Is it an array of structs that's big enough? – bruceg Jan 04 '19 at 22:23
  • 6
    My guess is the printf is really working, but you immediately call `system(cls)` at the top of the loop after the printf as the next statement to be executed. Pull the `system(cls)` out of the loop, and I bet it shows up. – bruceg Jan 04 '19 at 22:26
  • the values are the type of pizza and the size of it like Margerita(small) but it works only for one pizza like it doesn't check the if statement at all, how to read the number of pizzas is by increasing the variable( nr_pizzas_ordered) with everytime the user clicks (y) so the programm keeps going in the loop and increases the nr_pizzas_ordered by 1. @JohnSzakmeister – Dr.Noob Jan 04 '19 at 22:33
  • @PeterL. no it doesn't – Dr.Noob Jan 04 '19 at 22:36
  • How is nr_pizzas_ordered declared? – Peter L. Jan 04 '19 at 22:38
  • @bruceg yes it is an array of structs that accepts 10 elements (ten pizza's), how do I know if its big enough or not?could that be the problem somehow? – Dr.Noob Jan 04 '19 at 22:40
  • 1
    @bruceg yesss you are right, system(cls) was the problem, thanks man :) – Dr.Noob Jan 04 '19 at 22:42
  • 1
    I think @bruceg has got it with the cls call. You should definitely try his suggestion. – John Szakmeister Jan 04 '19 at 22:42
  • 1
    Thanks guys system(cls) was the problem, appreciate your help – Dr.Noob Jan 04 '19 at 22:43
  • For future reference: [**How to debug small programs**](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) and talk to the duck... Really, it helps `:)` – David C. Rankin Jan 05 '19 at 01:25
  • @DavidC.Rankin I just looked it up its really helpful, tahnks man – Dr.Noob Jan 07 '19 at 23:12
  • [why does "printf" not work?](https://stackoverflow.com/q/39180642/995714) – phuclv Mar 22 '19 at 15:25
  • Possible duplicate of [Why does printf not flush after the call unless a newline is in the format string?](https://stackoverflow.com/questions/1716296/why-does-printf-not-flush-after-the-call-unless-a-newline-is-in-the-format-strin) – phuclv Mar 22 '19 at 15:25

0 Answers0