gpt4 book ai didi

c - 在 C 程序中循环重复来自先前输入的数据

转载 作者:太空宇宙 更新时间:2023-11-04 08:18:15 25 4
gpt4 key购买 nike

我正在创建一个程序,要求用户选择他们想要的水品牌以及他们想要运送到他们家的瓶子数量。该程序使用 while 循环询问用户他们想要的瓶子数量是否正确,如果不正确,循环将允许重新输入他们想要的数量。

每次我必须选择一个新的水品牌时,该程序都会运行;它坚持以前水品牌的细节。我无法在我的程序中看到 while 循环的问题。这是代码:

#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;
}

最佳答案

您不会在一次迭代后重置 correct。每次都设置为0:

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

顺便说一句,你有很多代码重复。您可以简单地使用数组来存储瓶子类型并在循环中使用数组元素。

关于c - 在 C 程序中循环重复来自先前输入的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34560158/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com