For the last else statement... how can I make the program run again after it asks to enter 1 or 2 ... | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

For the last else statement... how can I make the program run again after it asks to enter 1 or 2 ...

{ if (number == 1) { cout << "Now is the time for all good men to come to the aid of their party." << endl; } else { if (number == 2) { cout << "Now is the time for all good men to come to the aid of their country." << endl; } else { while (number != 1 && number != 2) { cout << "I'm sorry but that is an incorrect choice; Please input a 1 or 2" << endl; cin >> number; } } }

14th Apr 2017, 6:28 PM
Luke Mai
Luke Mai - avatar
3 Answers
+ 18
Try this: #include<iostream> using namespace std; int main(){ int number; do{ cin>>number; if (number == 1){ cout << "Now is the time for all good men to come to the aid of their party." << endl; } else{ if (number == 2){ cout << "Now is the time for all good men to come to the aid of their country." << endl; } else{ cout << "I'm sorry but that is an incorrect choice; Please input a 1 or 2" << endl; } } }while(number!=1 && number!=2); } /* Explanation: do-while loop will keep taking input until the number is 1 or 2. */
14th Apr 2017, 7:08 PM
Shamima Yasmin
Shamima Yasmin - avatar
+ 2
please what's the best coding program for c++?
17th Apr 2017, 4:45 PM
ALI Mamdouh Ahmad
ALI Mamdouh Ahmad - avatar
+ 1
I think,it will be more easy to use SWITCH. int c=0; cin>>c; switch ( c ) { case 1: cout << "Now is the time for all good men to come to the aid of their party." << endl; break; case 2: cout << "Now is the time for all good men to come to the aid of their country." << endl; break; default: cout << "I'm sorry but that is an incorrect choice; Please input a 1 or 2" << endl; cin >> number; }
14th Apr 2017, 6:43 PM
Nikita Galchenko
Nikita Galchenko - avatar