# Vector problem

• 01-04-2011
ychan031
Vector problem
So here is the assignment speculation:
Write a program that reads in a list of integers. This list may contain positive or negative integers and be any size, determined by the user of the program. The user should signal they are done entering values by entering the character q instead of an integer. When the user is done entering integer values, the program should output a two-column list. The first column is a list of the distinct vector elements; the second column is the count of the number of occurrences of each element. The list should be sorted on enteries in the first column, largest to smallest.

If the user enters the following values:

-12 3 -12 4 1 1 -12 1 -1 1 2 3 4 2 3 -12
the output should be:

N Count
4 2
3 3
2 2
1 4
-1 1
-12 4

Code:

```#include <iostream> #include <vector> using namespace std; /** */ int main() {     vector<double> integers;     cout << "Please enter integers. Enter q to see result.\n";     bool more = true;     while (more)     {         int value;         cin >> value;         if (value == )             more = false;         else             integers.push_back(value);     }     int i;     for (i = 1; i < integers.push_back(value); i++)     {             } return 0; }```
And this is what I got so far but I'm stuck on if (value == ) because I have no idea how to make the user to input the character instead of an integer in order to quit by entering "q" as stated above in cout. Anyone??
• 01-05-2011
Salem
The short answer is to test the stream status after the cin
cin.good()

The long answer is to always read user input into a std::string (using getline), then parse the line to see whether it contains numbers or a 'q'.
• 01-05-2011
Elysia
boost::lexical_cast (see boost.org) is an excellent tool to convert a string to a number. That way, you can read a string, check if it's equal to q, and if not, convert it to a number.