我正在尝试创建一个重复结构,以便在代码末尾键入“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;
}
如何让代码循环回到开头?
答案 0 :(得分:1)
我建议您使用do while循环。 https://www.tutorialspoint.com/cplusplus/cpp_do_while_loop.htm
do
{
// begin loop
...
}while(continue_!='n' && continue_!='N');
答案 1 :(得分:0)
对于初学者来说,你需要一个循环......
在这个例子中,可能是一个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;
}
答案 2 :(得分:0)
B中。沃德是对的,你必须使用&#34;嵌套&#34; 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;
}