2016-10-24 9 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

値を比較するときは、1つではなく、2つの等号が必要です。これはコードの14行目で行いました。 – Matt

0

B.ウォードは、あなたが「ネストされた」やる-しばらくは完全にあなたの問題を解決するためにループを使用する必要があり、権利です。プログラム内で処理を進めるには、コード内に他の条件が満たされている必要があり、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; 
} 
関連する問題