why does this not work

This is a discussion on why does this not work within the C++ Programming forums, part of the General Programming Boards category; I want this while loop to assign sequntial values to different array so here is what I got Code: int ...

  1. #1
    Registered User
    Join Date
    May 2002
    Posts
    208

    why does this not work

    I want this while loop to assign sequntial values to different array so here is what I got

    Code:
    int j=0;
    while (j<=100)
    { for(int y=0;y<=(int)liklihood[0];y++)
       { zero[y]=j;
         cout<<"\n"<<zero[y];
         j++;   
       }
      for(int y=0;y<=(int)liklihood[1];y++)
       { one[y]=j;
         cout<<"\n"<<one[y];
       j++;   
       }
         for(int y=0;y<=(int)liklihood[2];y++)
       { two[y]=j;
         cout<<"\n"<<two[y];
       j++;   
       } 
        for(int y=0;y<=(int)liklihood[3];y++)
       { three[y]=j;
         cout<<"\n"<<three[y];
       j++;   
       }
         for(int y=0;y<=(int)liklihood[4];y++)
       { four[y]=j;
         cout<<"\n"<<four[y];
       j++;   
       }
    }
    and it works fine but for some reason it keeps going after 100 and I have no idea why.
    Jeff Paddon
    Undergraduate Research Assistant
    Physics Department
    St. Francis Xavier University

  2. #2
    RoD
    RoD is offline
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331
    hmm, jus a guess but try

    do
    {

    ......
    .....


    }

    while(j<100)


    OR

    //dunno bout this one not home to test

    while(int j = 0; j<100; j++)

  3. #3
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    You never get back the the conditional to check j until after the 5th for loop, yet j is incremented each time through each loop. If the sum of likelihood[0] through likelihood[4] exceeds 100 then j will be greater than 100 by the time you get to the while conditional to stop the while loop.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. strcmp returning 1...
    By Axel in forum C Programming
    Replies: 12
    Last Post: 09-08-2006, 08:48 PM
  2. getline() don't want to work anymore...
    By mikahell in forum C++ Programming
    Replies: 7
    Last Post: 07-31-2006, 11:50 AM
  3. Why don't the tutorials on this site work on my computer?
    By jsrig88 in forum C++ Programming
    Replies: 3
    Last Post: 05-15-2006, 11:39 PM
  4. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 12:39 PM
  5. DLL __cdecl doesnt seem to work?
    By Xei in forum C++ Programming
    Replies: 6
    Last Post: 08-21-2002, 05:36 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21