Extracting values from "for" loop

Hi,

I have two issues, I hope you guy can help.

Here's an example I have prepared:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <curses.h>
#include <stdafx.h>
    
    
double Array[10];
int x;

int main () 
{
    
initscr();
printw("Extracting value from loop\n\n");
Array[5]=12345;

for (x=0; x++; x<10)
{
Array[x]=1;
printw("Array %d value is now %f\n\n",x, Array[x]); 
Sleep(200);
refresh();
}

printw("Value extracted is: %f\n\n",Array[5]); 

refresh();

getch();

endwin();
return 0;
}


My first problem is that the "for" loop for some reason is not working, the program just skips it (?!), why that ?

My second issue is that, even if the "for" loop is working, for some reason the
"printw("Value extracted is: %f\n\n",Array[5]); " command instead of returning 1, is still returning 12345.

In other terms I don't know how to make a reference, in an command that is outside the "for" loop, to an array whose results are feeded by this same loop.

Can anyone share his knowledge ?

Many thanks and regards,

Wissam
Last edited on
The for loop you have the last two arguments inverted. It should be this:

for (x=0; x<10; x++)


I'm not really familiar with printf yet so I can't advise there.

hth
Also, you're declaring your variables at the wrong place.
Array belongs inside main and x inside the for loop: for (int x=0;x<10;x++)
also, note that Array[x]=1; would be effectively setting all of the values in the array to 1, which, with the for loop working correctly, would explain the behavior when printing Array[5].
Thx very much, fixing the the way I was initializing the for loop solved both problem.

thx again and regards,

Wissam
Topic archived. No new replies allowed.