如何根据用户输入使我的程序循环回到开始

How to make my program loop back to the beginning based on user input?

本文关键字:循环 程序 开始 我的 何根 用户 输入      更新时间:2023-10-16

当我在代码末尾键入"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;
}