#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] = 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?
m2k_stp@hotmail.com 写道: > #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] = 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].
![]() |
0 |
![]() |
<m2k_stp@hotmail.com> wrote in message... > #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] = 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 ]. > 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? > -- Bob R POVrookie