### Trying to make inputting q quit when inputting integers

So I am very new to coding, "hello world" was three weeks ago, but the professor gave us the assignment to track different information about the numbers inputted. The code tracks the average value, max, min, sum, and number of values inputted.

The prof also had us put an if loop in it where if the user inputted a -1 the code would break. I would like to be able to input -1 so I am trying to set change the way the code breaks.

The way I thought to do this was to prompt for a character, and if the character was q break the code, but if it wasn't to convert it to an integer. I read that you can use the atoi function to do this but I can't seem to get it to work. I'm sure it's something trivial that I just don't understand, so sorry for the dumb question. Thanks for the help!

This is what I have so far.

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051`` `````` #include #include #include #include using namespace std; int main() { int userInput = 0; char quit; int count = 0; int total = 0; int big; int tiny; while(1) { cout << "Input numbers (type q to stop)" << endl; cin >> quit; if(quit == 'q'){ break; } else{ userInput = atoi(quit); if(count == 0){ big = userInput; tiny = userInput; } if(userInput > big){ big = userInput; } if(userInput < tiny){ tiny = userInput; } total = total + userInput; count++; } } float average = total/count; cout << "__________________________________" << endl; cout << " " << endl; cout << "So, you entered a total of " << count << " numbers." << endl; cout << "Sum of all those numbers: " << total << endl; cout << "The average value is: " << average << endl; cout << "The smallest number you entered was " << tiny << "." << endl; cout << "The largest number you entered was " << big << "." << endl; cin.get(); cin.get(); return 1; }``````
You could use a string instead of a character for user input.

something like this
 ``123456789101112131415161718`` ``````#include string input; cin >> input; if (input.length() > 0) { if (input[0] == 'q') { cout << "Finished"; // etc. } else { int number = atoi(input.c_str()); cout << "Number: " << number; } }``````

There may be much better solutions...
Hey thanks for the reply Chervil,

It worked! I don't really understand how it works though... If you get a minute to give me an explanation that would be great.

Thanks so much,
Matt

P.S. If someone knows a better or alternate way to do this, please let me know.
Well, here's a bit more detail.

`cin >> input;` - this gets the user to input something via the keyboard, and store it as a string. It's possible that the user might just press enter, in which case the string would be empty.

Hence this test, `if (input.length() > 0)`, if the string is empty, we can't do anything useful with it. An alternative would have been the `.empty()` function.

This line `if (input[0] == 'q')` is accessing an individual character in the string. [0] is the first character, [1] is the second and so on.

Finally this line `int number = atoi(input.c_str());` uses the `atoi()` function which gets an integer from a string of characters. It needs a plain c-string, not a C++ string. That's the purpose of the `.c_str()` function.

Please take a look at the reference section of this site; it's very useful.
http://www.cplusplus.com/reference/string/string/
Thank you so much. You were very helpful. I will be taking a look at that site shortly.

Thanks again,
Matt
Well, the code I showed above was not ideal. In fact the test for input.length() is not really required.

But let's move on.
Back to the original problem, quote:
 if the character was q break the code, but if it wasn't to convert it to an integer

Here's a better alternative, using `cin.fail()` to distinguish between a valid number and a character input.

 ``123456789101112131415161718192021222324252627282930313233`` `````` vector nums; char ch; while (true) { cin >> number; if (cin.fail()) { cin.clear(); // Not a number. Clear the error condition cin >> ch; // and get a character if (ch == 'q') { break; // 'q' will terminate the 'while' loop } else { cout << " invalid input ignored\n"; // it wasn't 'q', so continue cin.ignore(1000,'\n'); // ignore the incorrect input } } else { nums.push_back(number); // store the valid number } } // Do something with the numbers which were previously input. cout << "\n -- Processing -- \n"; for (int i=0; i
Last edited on
Topic archived. No new replies allowed.