我試圖創建一個重複結構,當我在代碼末尾鍵入'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;
}
我該如何讓代碼循環回到開頭?
我討厭成爲一個煩惱,但我似乎無法讓你的方法工作。我的代碼現在有什麼問題? – Krilla13
https://gyazo.com/9b286dff70380a8a67b1211c01a88bf7 - 這是我的代碼的圖像。它可以工作,但是當我在最後輸入「Y」以「確保另一個動物」時,它不會循環。另一個成員似乎工作的goto循環,但我猶豫使用它,因爲我的教授從來沒有教過我們這種方法。 – Krilla13
比較值時,您需要2個等號,而不只是一個。你在代碼的第14行中做了這個。 – Matt