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; } } }
3 Respostas
+ 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.
*/
+ 2
please what's the best coding program for c++?
+ 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;
}