cin question?

Ok this isn't really a cry for help since my code is fully functional but this is just something I quite don't understand about input stream and the cin statement. I was doing an exercise on how to input some integers and then the program is suppose to find out what is the highest values of those integers the user types in. Here is the code
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
int main()
{

	int highest,num;
	vector<int>numbers;
	cout <<"Enter 10 digits and I'll tell you\n";
	cout <<"which one is the highest\n";

	while(cin>>num)
	{
		numbers.push_back(num);
	}
	
		highest = numbers[0];

	cout <<"You entered\n";

	for(int i = 0; i < numbers.size();i++)
	{
		if(highest < numbers[i])
		{highest = numbers[i];}
		
		cout <<numbers[i] <<" ";
	}
	cout <<endl;
	cout <<"and the highest number is: " <<highest <<endl;


return 0;
}


So I ran the code to try it out. When I entered numbers:
8 3 4 5 9 1 2 3
then I hit enter. Nothing happened.

I then tried
8 3 4 5 9 1 2 3; -> semicolon added

and it ran perfectly
You entered
8 3 4 5 9 1 2 3
and the highest number is: 9

My original understanding of cin is that it ignores everything, including whitespace until it get's to the '\n' character, that's why I thought pressing enter would do it. Is this treated differently with integers? cin ignores everything until a non integer is entered maybe?
When you entered the semicolon, the statement cin>>num failed because the semicolon is not an int so you broke out of the loop.

If you want to be lazy just prompt them to enter F when they are done. It won't even matter if they actually hit F because any non-integer will throw them out of the loop.

while (cin >> num)

This statement returns the state of the cin object. Everything was fine and it would have kept taking in numbers, but you gave it a semi-colon, which set the fail bit of the cin object.

my opinion is that you should find some other way to limit the amount of inputs to 10.
For some reason I thought cin only eats one white space, but I ran your program and it worked with any amount of spaces.

Learn something new everyday.
cin ignores ALL whitespace and getline includes whitespace
thanks for clearing this up for me guys. Yeah the command given by the program isn't really correct since you can input 10 integers but you can also enter 100 or any number if you want. I guess this would do it?

vector<int>numbers(10);

for(int i = 0; i < 10; i++)
{
cin>>numbers[i];
}

anyway, thanks for your input...
If you are going to do it like that you should use another condition in case the stream fails before all 10 numbers are read in.

1
2
3
4
5
6
7
8

int num;

for(int i = 0; i < 10 && cin; i++)
{
     cin>> num;
     if (cin) numbers[i] = num;
}


If you are really want to make it robust you can make it so that it will only read and store valid ints and ignore everything else. You will need to use cin.clear() to reset the fail flags and cin.ignore(256, ' ') to ignore everything till the next whitespace. In this case you will not want to set a predetermined size for the vector and instead use the .push_back() method to append each valid int on the end.

I have some code that does the same thing but from a file and it's fairly robust.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
        char buff;
	int tmp;
	vector<int> v;
	ifstream fin;	
	fin.open("filename.txt");
	
	fin >> tmp;
	while (!fin.eof()) {
		if (fin.fail()) {
			fin.clear();
			buff = fin.get();		
			while (buff != ' ' and buff != '\n')
				buff  = fin.get();
		}
		else
			v.push_back(tmp);	
		fin >> tmp;
	}


It will store any integers that do not come directly after invalid characters so the input:

1
2
3
24adsasd agdf27
asdsd1 3asdasd2 7asds3
4 5 6


will store the numbers 24, 3, 7, 4, 5, and 6


If you want to make it incredibly robust you could even read char by char check which one are digits so that it store every single integer but that might be a bit too much. lol
Last edited on
Topic archived. No new replies allowed.