weird program execution

Hi, im learning c++ from 1 book and here is a sample program to demonstrate some stuff about while loop. Its supposed to count all in inputs of the user untill he enters not integer character.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
using namespace std;
int main() {
	int input, sum = 0;
	cout << "Enter numbers to sum, type 'q' to end the list:";
	while (cin >> input){
		sum += input;
	}
	cout << "Sum = " << sum << endl;
        //up from here is original code

        //below is my code
	cout << "wtf" << endl;
	system("pause");        // tryed to use cin.get(); --> didnt work
	int just_rand_num;
	cin >> just_rand_num;   // program never asks for this input
	cout << "this is just_rand_num ... " << just_rand_num << endl;
        //instead of number what i wanted to enter i got number -858993460
	system("pause");
	return 0;
}


Enter numbers to sum, type 'q' to end the list:4 3 5 g
Sum = 12
wtf
Press any key to continue . . .
this is just_ran_num ... -858993460
Press any key to continue . . .


So my questions are
1. I read earlier that char symbols like 'h' can be assigned to integers and integer will take this chars ASCII code number as his value. U can also assing integer to char and you will get character that has this integer number in ASCII code.
So why isnt this symbol g what i enter interpreted as number 103?
2. Why this program doesnt care about cin.get(); (i saw in 1 tutorial how to stop programs window from dissapearing if im using visual studio 2013. He suggested to use this cin.get() instead of system("pause"))
3. If i actually wanted to continue to code more stuff after this summing thing, why isnt program even asking me for just_rand_num input?
And where is this number -858993460 is coming from every time i run this program.

So small program but so many confusions. Just when i felt like i own this stuff :D
Thanks a lot for any help
Last edited on
I continued to read and turns out

"the cin input stream object is left in an
error state and cannot be used for the rest of the program’s execution."

Also tested that u cant do this
int x = g;
But u can only do this
int x = 'g';

So basically i just dont understand where is this magical number -858993460 is coming from
I can easily see that you do not have C++11 turned on. Do it. It will help you later.

3.
and
the cin input stream object is left in an error state and cannot be used for the rest of the program’s execution
...unless you clear error state.

And do not forget that if reading fails then whatever program couldn't read is left in stream
http://www.cplusplus.com/forum/beginner/153907/

So why isnt this symbol g what i enter interpreted as number 103?
Because it is not a number. Being able to implicitely convert numbers←→chars is a nice low-level technical trick. Ability to read characters as numbers would defeat all purpose of type-safety.

Why this program doesnt care about cin.get();
It does. Just your input buffer still have other characters so it does not need to request one from user.

And where is this number -858993460 is coming from every time i run this program.
It is some random bit pattern left on stack rinterpreted as number.
Thanks very much man! :)
Topic archived. No new replies allowed.