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;
}
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 (orxyz— but that's where checking the return value fromscanf()is important), or entering such values for the number of bottles, or the value ofcorrect.