array 0 0 0...

Discussion in 'C++' started by m2k_stp@hotmail.com, Aug 22, 2007.

  1. Guest

    #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?
     
    , Aug 22, 2007
    #1
    1. Advertising

  2. fcvcnet Guest

    写é“:
    > #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].
     
    fcvcnet, Aug 22, 2007
    #2
    1. Advertising

  3. BobR Guest

    <> 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 * 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
     
    BobR, Aug 22, 2007
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Daniel
    Replies:
    1
    Views:
    1,073
    Kevin Collins
    Aug 14, 2003
  2. S300
    Replies:
    4
    Views:
    22,129
    Roedy Green
    Aug 19, 2003
  3. Mara Guida

    const and array of array (of array ...)

    Mara Guida, Sep 2, 2009, in forum: C Programming
    Replies:
    3
    Views:
    524
    David RF
    Sep 3, 2009
  4. Tom
    Replies:
    3
    Views:
    236
    salsablr
    Dec 20, 2004
  5. Tuan  Bui
    Replies:
    14
    Views:
    532
    it_says_BALLS_on_your forehead
    Jul 29, 2005
Loading...

Share This Page