Thread: why does this not work

  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
    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, 07:48 PM
  2. getline() don't want to work anymore...
    By mikahell in forum C++ Programming
    Replies: 7
    Last Post: 07-31-2006, 10: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, 10: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, 04:36 PM