array 0 0 0...

M

m2k_stp

#include "callFirst.hpp" //contain iostream, namespace, and basic
operations

int main()
{
int x[10];
int Ops;
for(int i = 0; i < 10; i++){
x = i * 2;
}

int k = 0;
while( k < 10){
int j = 0;
cout << x[j];
cout << " ";
j++;
k++;
}

int dummy;
cin >> dummy;
return 0;
}

why I get output as 0 0 0 0 0 0 0 ...
suppose it should be 0 2 4 6 ...
how do I fix this?
 
F

fcvcnet

(e-mail address removed) 写é“:
#include "callFirst.hpp" //contain iostream, namespace, and basic
operations

int main()
{
int x[10];
int Ops;
for(int i = 0; i < 10; i++){
x = i * 2;
}

int k = 0;
while( k < 10){
int j = 0;
cout << x[j];
cout << " ";
j++;
k++;
}

int dummy;
cin >> dummy;
return 0;
}

why I get output as 0 0 0 0 0 0 0 ...
suppose it should be 0 2 4 6 ...
how do I fix this?

while( k < 10){
int j = 0;
cout << x[j];
....
}
Always x[0].
 
B

BobR

#include "callFirst.hpp" file://contain iostream, namespace, and basic
operations

int main(){
int x[10];
int Ops;
for(int i = 0; i < 10; i++){
x = i * 2;
}

int k = 0;
while( k < 10){

int j = 0;

Every time through the loop, you set 'j' to zero.
Why didn't you just use 'k'?
cout << x[j];

So this always outputs x[ 0 ].
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top