Click here to Skip to main content
15,886,788 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C++
#include<iostream>
using namespace std;

int main(){
 int iCount=0;
  int array[iCount], iInput,i;
  cout<<"HOW MANY NUMBERS:" ;
  cin >> iInput;
  
  for(i=0; i<iinput;++i){>
  		cin>> array[iCount];
  		iCount++;
 }
return 0;
}


simple code in taking n numbers.
can someone explain why iInput is updating its value to 6.

when I inpputed lets say 8 after 6 entries to the array. iInput updates itself to 6.
Please help. I dont understand :((((
Posted
Updated 4-Feb-15 4:13am
v2

1 solution

Well...it's slightly complex.
C++
#include<iostream>
using namespace std;

int main(){
 int iCount=0;
  int array[iCount], iInput,i;
  cout<<"HOW MANY NUMBERS:" ;
  cin >> iInput;

  for(i=0; i<iinput;++i){>
        cin>> array[iCount];
        iCount++;
 }
return 0;
}
Look at your definition of array - it's a array of integers, but the number of integers you can store in it is controlled by iCount - which at the time you create the array is zero (because the line above says so).

So when you get to the point of storing a value in the array:
C#
cin>> array[iCount];
there isn't really anywhere to store it, so it will overwrite the "next" variable on the stack - which happens to be iInput.

Give the array a size, and it should start to work.
 
Share this answer
 
Comments
TheRealSteveJudge 4-Feb-15 10:20am    
5*

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900