0

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;
}
1
  • 1
    You will need to look at how you can avoid repeating almost the same block of code 5 times (an array containing the names of the types of water would help, for example), after you've got the immediate problem resolved. You should also be checking the return value from scanf() each time — so having fewer of those calls will help. Your loop should deal with the possibility of the user entering a choice of 0, or 7, or 99, or -12345678 (or xyz — but that's where checking the return value from scanf() is important), or entering such values for the number of bottles, or the value of correct. Commented Jan 1, 2016 at 21:35

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.