2016-10-24 49 views
0

我試圖創建一個重複結構,當我在代碼末尾鍵入'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

那麼對於初學者,你需要一個循環...

在這個例子中,可能是一個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; 
} 
+0

我討厭成爲一個煩惱,但我似乎無法讓你的方法工作。我的代碼現在有什麼問題? – Krilla13

+0

https://gyazo.com/9b286dff70380a8a67b1211c01a88bf7 - 這是我的代碼的圖像。它可以工作,但是當我在最後輸入「Y」以「確保另一個動物」時,它不會循環。另一個成員似乎工作的goto循環,但我猶豫使用它,因爲我的教授從來沒有教過我們這種方法。 – Krilla13

+0

比較值時,您需要2個等號,而不只是一個。你在代碼的第14行中做了這個。 – Matt

0

B.沃德是正確的,你必須使用「嵌套」做-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; 
} 
相關問題