|
|
if else loops |
Am I completely off base with my programming then? |
|
|
|
|
|
|
#include <limits>
for this method.stringstream
s, like DTSCode mentioned.
|
|
Enter a number: No. I said enter a *number*: I don't want to. I said enter a *number*: I absolutely refuse! I said enter a *number*: Okay, fine... I said enter a *number*: 42 You entered: 42 |
Another method uses stringstreams, like DTSCode mentioned. This works if you want to keep it compact: 1234567891011121314 #include <iostream> #include <string> #include <sstream> using namespace std; int main() { cout << "Enter a number: "; int num; string input; while (getline(cin, input), !(istringstream(input) >> num)) cout << "I said enter a *number*: "; cout << "You entered: " << num; } Enter a number: No. I said enter a *number*: I don't want to. I said enter a *number*: I absolutely refuse! I said enter a *number*: Okay, fine... I said enter a *number*: 42 You entered: 42 |
int num;
with char num;
. although i would change the name to something that makes more sense
|
|
|
|