Problems with a for loop Visual C++ Express

I am writing a program to have the user enter numbers which will in turn be placed in an array. This array will then be printed on the screen I am using a For loop for both, but the program seems to skip some lines of code and not others. Here is the code:

#include "stdafx.h"
#include "math.h"

using namespace System;

int main(array<System::String ^> ^args)
{
int i; // variable for tracking for loops
int a; // variable to enter into array
int count; //Track array data
int array1[5]; //Define array

for(i=0;i<6;i++) // build array
{
Console::WriteLine("Enter a number:");
a=Console::Read();//user input
a=array1[count]; // place user input into array
count++;
}
for(i=0;i<6;i++) // display array
{
Console::WriteLine(array1[i]);
}
Console::WriteLine("Press enter to close"); // close program
Console::Read();
return 0;
}

---------------------------------------------------------------------

This is the output:

Enter a number: <- all seems good here
1
Enter a number: <- These three lines print out immediatly and don't allow me to
Enter a number: input anything
Enter a number:
2 <- Program allows me to enter another number
Enter a number: <- Last two lines are printed immediatly
Enter a number:
5246767 <-The output is random, but I expected that as I am not using
657 pointers yet. My real problem is with the for loop.
0
25645
4577
Press enter to close

Is this a coding error or logic problem? Any help is appreciated.

I do not know this language but your code contains obviously invalid constructions.
First of all you define the array as containing 5 elements

int array1[5];

but in loop you try access 6 elements

for(i=0;i<6;i++) // build array

Further, you did not initialize variable count

int count; //Track array data

so it has undefined value. So the statement

count++;

has no a greate sense.

It is obvious that instead of

a=array1[count]; // place user input into array

you shall write

array1[i] = a;

As for a=Console::Read();//user input you should read documentation how this function reads user input.


Topic archived. No new replies allowed.