Unexpected output explanation

Mar 10, 2014 at 4:38pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

void main()
{
	fflush(stdout);
	int n ;
	cout <<"enter the number of values u want";
	cin>>n;
	int* list = new int[n];
	cout<<endl<<"enter the list"<<endl;
	for ( int counter = 0; counter <n;counter ++)
	{
		cin>>list[counter];
	}
	for ( int counter = 0; counter <n;counter ++)
	{
		cout<<list[counter]<<" , ";
	}
	

	_getch();

}



Hello,
I just wrote the following program and tried to input "3r" when i know i should only type int when the program asks for a int.I am getting a weird output and can someone explain me what is happening inside the compiler that is making this happen ?.I am automatically getting 3 numbers . Thanks!

OUTPUT:
enter the number of values you want 3R
enter the list-
-842150251,-842150251,-842150251

PLATFORM:
windows 7, vc++ 2012

Last edited on Mar 10, 2014 at 4:43pm
Mar 10, 2014 at 4:47pm
"r" is not a number, so it cannot be stored in an object of type int. The input attempt at line 12 fails (btw, you should check if the input succeeded), and nothing is ever written to your array.

The output you're seeing is Microsoft's debug mode value "uninitialized heap memory"
Last edited on Mar 10, 2014 at 4:52pm
Mar 30, 2014 at 6:50pm
Thanks!
Topic archived. No new replies allowed.