f



array 0 0 0...

#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?

0
m2k_stp (1)
8/22/2007 1:49:12 AM
comp.lang.c++ 49423 articles. 7 followers. Post Follow

2 Replies
768 Views

Similar Articles

[PageSpeed] 11

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
fcvcnet
8/22/2007 1:59:21 AM
<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


0
8/22/2007 2:37:54 AM
Reply: