how to add loop in c++ -
could me add loop program?
here's code:
cout << "what do?" "\n"; cout << "\n"; cout << "(press enter...)"; cin.ignore(); cout << "\n"; cout << "stay in bed?" "\n"; cout << "go bathroom?" "\n"; cout << "go downstairs?" "\n"; cout << "\n"; string answer; getline(cin, answer); if (answer == "stay in bed", "stay in bed") { cout << "you lay there, motionless. silent."; } else if (answer == "go bathroom", "go bathroom") { cout << "you , walk across hall bathroom"; } else if (answer == "go downstairs", "go downstairs") { cout << "you , walk downstairs kitchen."; } else { cout << "that not valid answer..."; } cin.ignore();
how should add loop when user enters falling under condition of "else", loop returns ask "what do?"
there's more 1 way want (or think want do). 1 way put whole thing inside endless loop , use break
out:
while(1) { cout << "what do?\n"; getline(cin, answer); if (answer == "stay in bed") { cout << "you lay there, motionless. silent."; break; } else if (answer == "go bathroom") { cout << "you , walk across hall bathroom"; break; } }
Comments
Post a Comment