i'm newby to C++.

Small code as follows:

int main(int argc, char* argv[]) {

cout << "function main() .." << '\n';

char ch1;
int int1;
cin >> ch1;
cin >> int1;
cout << ch1 << '\n';
cout << int1 << '\n';

return 0;
}

when i run the program and input the following:
function main() ..
az

i get :

a
32767

i understand the 'a' but why the integer value of 32767?
I just want to test and see what happen if instead of a numeric value assigned to int1 i used a 'z'.

i try :
ax

and i also get same results.

ps: by the way the sizeof(int) = 4 on my machine. 64 bit chip.
Now if instead of int i use short and running the program.

function main() ..
az

i get:
a
0

Recommended Answers

All 6 Replies

When you declare some thing of the sort

int int1;

The compiler gives any arbitrary value to i. This could be 0,1,2,... any value what so ever.

As you input a char in response to the second cin, the compiler ignores the value entered by you.
Later when you want to display the entered value, the compiler prints the value of i, which will be garbage in your case

Run this code. If you give the input AZ, it will print A-1

int main()
{
 char ch1;
int int1=-1;
cin >> ch1;
cin >> int1;
cout << ch1 <<"\n";
cout << int1 << "\n";
      getch();
    return 0;
}

I hope this clears your confusion

When you declare some thing of the sort

int int1;

The compiler gives any arbitrary value to i. This could be 0,1,2,... any value what so ever.

As you input a char in response to the second cin, the compiler ignores the value entered by you.
Later when you want to display the entered value, the compiler prints the value of i, which will be garbage in your case

Run this code. If you give the input AZ, it will print A-1

int main()
{
 char ch1;
int int1=-1;
cin >> ch1;
cin >> int1;
cout << ch1 <<"\n";
cout << int1 << "\n";
      getch();
    return 0;
}

I hope this clears your confusion

Yes. Thanks!!

What's actually happening is something a bit different

cin >> ch1;
cin >> int1;

ch1 is a char so it holds 1 character. When you are typing in the character to go into this variable, you hit enter. Enter is a character which remains in the input stream and is processed by the cin >>int1; call which interprets it as "someone has pressed enter" so it skips the statement entirely without letting you enter an integer. The answer above about int1 being an uninitialized value is pretty much correct (though I believe it's more that the compiler sets aside an "empty" portion of the memory which could have any state and not so much that it assigns the memory an arbitrary value -- I may be inaccurate on that point).

P.S. You can eliminate that extra newline from the stream by placing a cin.ignore() between the 2 lines above.

ch1 is a char so it holds 1 character. When you are typing in the character to go into this variable, you hit enter. Enter is a character which remains in the input stream and is processed by the cin >>int1; call which interprets it as "someone has pressed enter" so it skips the statement entirely without letting you enter an integer. <snip>

The extraction operator (>>) will skip over the enter (newline) as that's whitespace.
If you enter the input as :
a <enter>
z <enter>
or as:
a z <enter>
or as
az <enter>
it will all be handled alike. The 'a' will be stored in the char variable, and cin will attempt to read the z and store it to the integer variable, which fails. The z will still be in the input stream, but the stream is in a bad state.

Why the OP gets various values displayed for the integer is compiler dependent action, as I showed in the response to the earlier post dealing with this same issue. In release version code, uninitialized automatic variables should hold random values, whatever the bits happen to be in the assigned memory. In "debug" code (which is the default in VC++), variables do get assigned a default value. In VC++ this value is 0xcccccccc for an int.

Well vmanes, that was a well-deserved semi truck driven through that garage sized hole in my knowledge :)

I do normally test these things. I went and tested to see what I was really thinking of and I guess that I made too much of a generalization from the stream problems resulting from combining cin and cin.getline. I had also misunderstood that the OP had been feeding 2 chars into the cin. Apologies to the OP.

Also thanks for the heads up on the Debug vs. Release behavior.

That was more like a Volkswagen than a semi.

We're here to learn and to share.

I'm pretty sure I've given answers to things other than the stated problem myself. As long as nobody gets hurt....

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.