Click to See Complete Forum and Search --> : Why doest this code work?????


June 2nd, 1999, 07:17 AM
I am trying to get this program to print to screen ut it doesnt???
Could anyone tell me why.
Or is there a better way of doing it.
All I want to do is see the result on screen.
I am using ms visiul c++ 6.
I am new arrays so I might be missing something but i dont know what.
Please help me
Thank You in advance.

ps: Is there a way of putting a loop to also get the results out without
typing each array one by one.????

#include <iostream.h>
#include <stdio.h>
#include <conio.h>
main()
{

int num[7];

num[0]=47;
num[1]=( num[6]+1 );
num[2]=0;
num[3]=( num[0]+num[1]+num[5]+num[6] );
num[4]=0;
num[5]=( num[0]-13 );
num[6]=65;

cout<<num[0]<<num[1]<<num[2]<<num[3]<<num[4]<<num[5]<<num[6];

getch();
return 0;
}

Wayne Fuller
June 2nd, 1999, 07:31 AM
What do you mean it doesn't work? I compiled it as is and it works. But change it to the following to see better results.
#include <iostream.h>
#include <stdio.h>
#include <conio.h>
main()
{

int num[7];

num[0]=47;
num[1]=( num[6]+1 );
num[2]=0;
num[3]=( num[0]+num[1]+num[5]+num[6] );
num[4]=0;
num[5]=( num[0]-13 );
num[6]=65;

cout << num[0] << endl;
cout << num[1] << endl;
cout << num[2] << endl;
cout << num[3] << endl;
cout << num[4] << endl;
cout << num[5] << endl;
cout << num[6] << endl;

getch();
return 0;
}




There is a huge problem, though. What exactly are you tring to do? If it is just to learn how to do arrays, then ok. But in the assignment statement you assign num[1] to num[6] + 1. num[6] has not been initialized, it could be anything at this moment.

Hope that helps,

Wayne

June 2nd, 1999, 09:08 AM
Thanks Wayne. I dont know why ti didnt work on my compiler. I am just trying to learn arrays here.
I thought the num[1]=num[6]+1 would add one to the num[6] number and put it in num[1] location. Did
I do wrong there. I was wanting to to put numbers into the arrays and put them out on the screen once done.
I am new at this.
Thanks you very much for respending.
Eric

chiuyan
June 2nd, 1999, 09:27 AM
Yes, num[1] = num[6] + 1 would add 1 to whatever value is stored in num[6] and then store it in num[1]. The problem is that in your code, num[6] has never been given any value before you do this assignment, so the value of num[6], and hence num[1], is undefined.

--michael

Dave Lorde
June 2nd, 1999, 10:18 AM
> ps: Is there a way of putting a loop to also get the results out without
> typing each array one by one.????

If you want to use a loop, it goes something like this:



const int ItemCount = 7;
// initialise array items here
...
...
// Output array items
for (int item = 0; item < ItemCount; ++item)
{
cout << num[item] << endl;
}




Dave