So I'm supposed to write a code that prompts a user to enter a number less than 100, until the user finally puts a number that is less than 100.
Here is my code:
#include <iostream>
using namespace std;
int main() {
int userInput = 0;
do{
cout << "Enter a number (<100) : " << userInput << endl;
cout << "Enter a number (<100) : " << userInput << endl;
cout << "Enter a number (<100) : " << userInput << endl;
cin >> userInput;
} while ( userInput < 100);/* Your solution goes here */
cout << "Your number < 100 is: " << userInput << endl;
return 0;
}
[/code]
Here is what happens when it runs:
The expected output:
Enter a number (<100):
Enter a number (<100):
Enter a number (<100):
Your number < 100 is: 25
My output:
Enter a number (<100) : 0
Enter a number (<100) : 0
Enter a number (<100) : 0
Your number < 100 is: 123
I think you wanted something like this ???
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
|
#include <iostream>
using namespace std;
int main() {
int userInput = 0;
do{
cout << "Enter a number (<100) " << "\n>>";
cin >> userInput;
if (userInput > 100)
{
cout << "Your number > 100 is: " << userInput << endl;
continue;
}
} while (userInput > 100);
cout << "Your number < 100 is: " << userInput << endl;
cin.clear();
cin.ignore();
fflush(stdin);
cout << "PRESSED ENTER TO EXIT...";
getchar();
return 0;
}
| |
Last edited on
I got it. It was suppose to be:
#include <iostream>
using namespace std;
int main() {
int userInput = 0;
do{
cout << "Enter a number (<100):" << endl;
cin >> userInput;
}while (userInput > 100);
/* Your solution goes here */
cout << "Your number < 100 is: " << userInput << endl;
return 0;
}