(iostream is a C++ library, so I am assuming you are compiling with a c++ compiler) In C++, you cannot allocate static arrays (arrays created with []'s ) and use a variable to specify the size.
So the code
int x = read_in_user_number();
int stuff[x];
is invalid as the compiler has no idea how big stuff is going to be at compile time, it depends on what the user inputs after it is compiled.
These are valid:
int stuff[5]; // Compiler knows "stuff" can ONLY be 5 elements long
const int x = 5;
int stuff[x]; // Compiler knows x will always and only be 5, and cannot be varied, and as such compiler knows "stuff" can only be 5 elements long
Here is some more hints on your program:
int main(int argc, char** argv)
{
int x; // x has not been set a value, and will most likely just be a seemingly-random, undefined number. this is referred to as "junk".
// This is not technically allowed in C++. You are using a variable (x) to try to specify the size of the array. The computer cannot know how long this will be when it is compiled.
int ages[x]; // x is still an unknown junk value, so you are trying to create an array whose length could be any number.
// x is still junk.
// arrays start at 0.
// To assign to the end of an array that is n long do something like:
// myArray[n-1] = 5; Which sets the last value of myArray to 5.
printf("Please type an age.\n");
scanf("%d",&ages[x]);
// In this for loop, you are now assigning x a value. Before it was a junk number, now it is zero.
// This loop will increase x by 1, starting at zero, until x is no longer greater than a negative number, than stop. (mathematically) x will always be greater than -1 as you are increasing x, and as such this is an infinite loop.
// This for loop needs curely brackets, or it will only loop the single next statement. In this case, the printf is in the for loop, the scanf is not.
for (x = 0; x > -1; x++)
printf("%d\n",ages[x]); // Executed in for loop indefinetely until Bad Things happen.
scanf("%d",&ages[x]); // will not be executed (program cant get past for loop).
getch();
For for loops, this will print 0,1,2,3,4 on the screen:
int x;
for (x = 0; x < 5; x++)
{
// All code in these brackets are looped. This is looped 5 times
printf("%d", x);
}
Your program appears to read in ages and print them back out to the user. How many ages is the program supposed to read in?