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