megasxrl
megasxrl

Reputation: 25

Loop in C program repeating data from previous input

I'm creating a program that requires the user to choose which brand of water they want and the amount of bottles they want delivered to their house. The program uses a while loop to ask the user if the number of bottles they want is correct and if not the loop will allow to re-enter the amount they want.

The program works up until the point each time I have to choose a new water brand; it sticks with the previous details of the water brand before. I'm unable to see the problem with the while loop in my program. Here's the code:

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

int main()
{
    int valuechange = 0;
    int correct = 0;
    int choice = 0;     
    int numofbottles;


    while (choice != 6)
    {
        printf("1. Buxton\n");
        printf("2. Evian\n");
        printf("3. Harrogate\n");
        printf("4. Power life\n");
        printf("5. Smart water\n");
        printf("6. Exit\n\n");
        printf("Enter your choice here: ");
        scanf("%d", &choice);
        /*each if statement has a while loop for if the user wants to re-enter a value of water bottles*/
        if (choice == 1)
        {
            while (correct != 1)
            {
                printf("Please choose how many bottles of Buxton you want to be delivered to your house:\n\n");
                scanf("%d", &numofbottles);

                printf("you have chosen %d bottles, is this the correct amount?\n(enter 1 for yes or 2 for no):\n\n", numofbottles);
                scanf("%d", &correct);
            }
            printf("Thank you, your order for %d Buxton bottles will be delivered to you\nwithin 3 working days.\n\n", numofbottles);
        }


        if (choice == 2)
        {
            while (correct != 1)
            {
                printf("Please choose how many Evian bottles  you want to be delivered to your house:\n\n");
                scanf("%d", &numofbottles);

                printf("you have chosen %d bottles, is this the correct amount?\n(enter 1 for yes or 2 for no):\n\n", numofbottles);
                scanf("%d", &correct);
            }
            printf("Thank you, your order for %d Evian bottles will be delivered to you\nwithin 3 working days.\n\n", numofbottles);
        }

        if (choice == 3)
        {
            while (correct != 1)
            {
                printf("Please choose how many Harrogate bottles  you want to be delivered to your house:\n");
                scanf("%d", &numofbottles);

                printf("you have chosen %d bottles, is this the correct amount?\n(enter 1 for yes or 2 for no):\n\n", numofbottles);
                scanf("%d", &correct);
            }
            printf("Thankyou, your order for %d Harrogate bottles will be deliver to you\nwithin 3 working days.\n\n", numofbottles);
        }


        if (choice == 4)
        {
            while (correct != 1)
            {
                printf("Please choose how many Powerlife bottles you want to be delivered to your house:\n\n");
                scanf("%d", &numofbottles);

                printf("you have chosen %d bottles, is this the correct amount?\n(enter 1 for yes of 2 for no):\n\n", numofbottles);
                scanf("%d", &correct);
            }
            printf("Thankyou, your order for %d Powerlife bottles will be deliver to you\nwithin 3 working days.\n\n", numofbottles);
        }

        if (choice == 5)
        {
            while (correct != 1)
            {
                printf("Please choose how many Smart water bottles you want to be delivered to your house:\n\n");
                scanf("%d", &numofbottles);

                printf("you have chosen %d bottles, is this the correct amount?\n(enter 1 for yes or 2 for no):\n\n", numofbottles);
                scanf("%d", &correct);
            }
            printf("Thankyou, your order for %d Smart water bottles will be deliver to you\nwithin 3 working days.\n\n", numofbottles);
        }
    }
    return 0;
}

Upvotes: 0

Views: 124

Answers (1)

P.P
P.P

Reputation: 121407

You don't reset correct after one iteration. Set it to 0 each time:

while (choice != 6)
{
correct = 0;
...
/*rest of the code */
}

By the way, you have a lot of code duplication. You can simply use an array to store the bottle types and use the array elements in the loop.

Upvotes: 2

Related Questions