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.

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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
    #include<iostream>
    #include <stdio.h>
    #include <stdlib.h>
    #include <cstdlib>
    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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <string>

    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.

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
27
28
29
30
31
32
33
    vector<int> 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<nums.size(); ++i) 
    {
        cout << nums[i] << endl;
    }
    cout << "\n --- Finished --- ";
Last edited on
Topic archived. No new replies allowed.