如何根据用户输入使我的程序循环回到开始
How to make my program loop back to the beginning based on user input?
当我在代码末尾键入"Y"以再次运行"保险价格检查"时,我正在尝试创建一个重复结构。
#include <iostream>
using namespace std;
int main() {
// Declaration of variables
char animal, status, continue_;
int i=0;
//Begin Loop
cout<<"Welcome to the Animal Insurance Company! What type of animal would you like to insure today: Enter D for Dog, C for Cat, B for Bird or R for Reptile: "<<endl;
cin>>animal;
if(animal=='D' || animal=='d') {
cout<<"You have selected a dog, has your dog been neutered? Enter Y for Yes or N for NO."<<endl;
cin>>status;
if(status=='Y' || status=='y')
cout<<"The insurance for your dog cost is $50."<<endl;
else if(status =='N' || status=='n')
cout<<"The insurance for your dog cost is $80."<<endl;
else
cout<<"Invalid Input, please type Y or N"<<endl;
}
else if (animal=='C' || animal=='c') {
cout<<"You have selected a cat, has your cat been neutered? Enter Y for Yes or N for NO."<<endl;
cin>>status;
if(status=='Y' || status=='y')
cout<<"The insurance for your cat cost is $40."<<endl;
else if(status =='N' || status=='n')
cout<<"The insurance for your cat cost is $60."<<endl;
else
cout<<"Invalid Input, please type Y or N"<<endl;
}
else if (animal=='B' || animal=='b' || animal=='R' || animal=='r')
cout<<"The insurance cost will be $10"<<endl;
else
cout<<"Invalid Input"<<endl;
cout<<"Do you want to insure another animal? Enter Y for Yes or N for NO."<<endl;
cin>>continue_;
if(continue_=='n' || continue_=='N')
cout<<"Thank you for using Animal Insurance Company"<<endl;
return 0;
}
如何使代码循环回到开头?
我建议您使用do-while循环。https://www.tutorialspoint.com/cplusplus/cpp_do_while_loop.htm
do
{
// begin loop
...
}while(continue_!='n' && continue_!='N');
对于初学者来说,您需要一个循环。。。
在这个例子中,可能是一个while循环(如果你有兴趣查找,请进行预测试(
为了实现您想要的,您需要一个布尔标志,并且只要该标志设置为true,就会运行循环。
(假设您的其余代码运行良好(
// Declaration of variables
char animal, status, continue_;
int i=0;
bool running = true;
//Begin Loop
while (running == true) {
// Rest of program
cout<<"Do you want to insure another animal? Enter Y for Yes or N for NO."<<endl;
cin>>continue_;
if(continue_=='n' || continue_=='N') {
cout<<"Thank you for using Animal Insurance Company"<<endl;
running = false;
}
}
return 0;
}
B。Ward是对的,你必须使用"嵌套的"do-while循环来完全解决你的问题。因为在程序继续运行之前,代码中还有其他条件需要满足,而且这些条件还需要do-while循环的服务。像这样;
#include <iostream>
using namespace std;
int main() {
// Declaration of variables
char animal, status, continue_;
int i=0;
//Begin Loop
do {
cout<<"Welcome to the Animal Insurance Company! What type of animal would you like to insure today: Enter D for Dog, C for Cat, B for Bird or R for Reptile: "<<endl;
cin >> animal;
if(animal=='D' || animal=='d') {
cout<<"You have selected a dog, has your dog been neutered? Enter Y for Yes or N for NO."<<endl;
//until the required input is entered, program will keep asking for it
do {
cin>>status;
if(status=='Y' || status=='y') {
cout<<"The insurance for your dog cost is $50."<<endl;
break;
}
else if(status =='N' || status=='n') {
cout<<"The insurance for your dog cost is $80."<<endl;
break;
}
else {
cout<<"Invalid Input, please type Y or N"<<endl;
}
}while(status != 'y' || status != 'Y' || status != 'n' || status != 'N');
}
else if (animal=='C' || animal=='c') {
cout<<"You have selected a cat, has your cat been neutered? Enter Y for Yes or N for NO."<<endl;
//until the required input is entered, program will keep asking for it
do {
cin>>status;
if(status=='Y' || status=='y') {
cout<<"The insurance for your dog cost is $40."<<endl;
break;
}
else if(status =='N' || status=='n') {
cout<<"The insurance for your dog cost is $60."<<endl;
break;
}
else {
cout<<"Invalid Input, please type Y or N"<<endl;
}
}while(status != 'y' || status != 'Y' || status != 'n' || status != 'N');
}
else if (animal=='B' || animal=='b' || animal=='R' || animal=='r')
cout<<"The insurance cost will be $10"<<endl;
else {
cout<<"Invalid Input"<<endl;
break;
}
cout<<"Do you want to insure another animal? Enter Y for Yes or N for NO."<<endl;
cin>>continue_;
if(continue_=='n' || continue_=='N')
cout<<"Thank you for using Animal Insurance Company"<<endl;
}while(continue_ == 'y' || continue_ == 'Y');
return 0;
}
相关文章:
- C++程序在循环后给出奇怪的int值
- 我试图制作一个程序,要求用户输入问题和答案,但程序循环不正确
- 为什么我的程序在for循环中k=0时返回垃圾值
- 密码登录程序将永远循环并显示不正确的结果
- 程序在尝试猜测它选择的随机数时进入无限循环?
- 程序以使用 C++ 中的 while 循环查找一组数字的最小值
- 而循环:简单的除法程序输出零,不明白为什么
- 在这个无限循环中,当输入 0 时终止,当输入 int 范围之外的任何内容时,程序行为不正常
- 虽然循环在使用回车键时不退出程序
- 如何调试我的 while 循环C++程序?
- if/else 循环:C++程序:不会显示最终提示/最终循环
- 简单的C 嵌套以用于循环程序,以打印带有星星错误的正方形
- 带有空间的C 星号循环程序
- 循环程序的行为是不可预测的
- 如何解决嵌套循环程序的问题
- 对我的做边循环程序感到困惑
- gcc和clang都无法编译循环程序
- 需要帮助do-while循环程序?(c++)
- 一个用c++编写的简单游戏循环程序
- 如何循环程序,使其继续执行