2016-10-24 2 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 loop가 있지만, 교수님이 그 방법을 가르쳐주지 않으므로 사용하기를 주저합니다. – Krilla13

+0

값을 비교할 때 하나만이 아니라 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; 
} 
관련 문제