Thread: even more loop trouble

  1. #1
    C++ beginner
    Join Date
    Jun 2004
    Posts
    66

    even more loop trouble

    Now that I'm over my fear of random numbers I'm working on the next exercise in my book.
    Code:
    Exercise 5-6:
    A lottery entry requires you to choose six different integers in the 
    range 1 to 49 inclusive. Write a program that generates 5 lottery 
    entries each time it runs
    It's my understand this exercise wants me to generate a series of 6 random integers from 1-49 five different times. So far, I've been succesful on the first set of 6 numbers - but when my program outputs those to the screen it just hangs there. The logic is as follows: a do while loop initiates the loop with the random number generator... this loop continues until 6 numbers have been found and have been output onto the screen. From here -the program is supposed to execute the if statement - that checks for the 6 lotto numbers, which then increments the do while loops counter. Here is the code:
    Code:
    #include <cstdlib>
    #include <iostream>
    #include <ctime>
    
    using namespace std;
    
    int main()
    {
        srand(time(NULL));
        int i = 0;
        int num = 0;
        int a = 0;
       
        do{
                
                while(a <= 5){
                        num = rand() % 48 + 1; //numbers 1-49
                        cout <<" "<< num; //outputs 1 lotto digit (should be 6 total)
                        a++; //increments the variable a to match conditions in the while loop
                        
                          if(a == 6){
                           cout << endl;
                             i++; //increments the first while loop counter
                              }
                        
                        }
                                   
                        
             }while(i <= 5)   ;         //end of while loop               
                
    
        
        system("PAUSE");
        return EXIT_SUCCESS;
    }
    The error I made I am completely oblivious too I guess - I can't seem to find the problem.. and I'm sorry I can't - because I feel like I'm wasting you guys' time!
    Oh my goodness.

  2. #2
    Cat Lover
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    109
    Code:
    while(a <= 5){
                        num = rand() % 48 + 1; //numbers 1-49
                        cout <<" "<< num; //outputs 1 lotto digit (should be 6 total)
                        a++; //increments the variable a to match conditions in the while loop
                        
                          if(a == 6){
                           cout << endl;
                             i++; //increments the first while loop counter
                              }
                        
                        }
    Testing if a > 5 inside a loop that only runs while a<=5 will never trigger, so it will always be stuck at 1.

    You also never reset a, so it will go through 5 times, then repeat the outside loop endlessly, without entering the inside loop.

  3. #3
    C++ beginner
    Join Date
    Jun 2004
    Posts
    66
    Yikes, what was I thinking?

    EDIT: ...and I've just fixed it - thank you for the help!
    Last edited by mabufo; 02-23-2006 at 06:45 PM.
    Oh my goodness.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Rewriting a for loop as a while/do-while loop
    By Ashfury in forum C++ Programming
    Replies: 7
    Last Post: 04-27-2007, 02:20 PM
  2. loop the loop - feeling foolish
    By estos in forum C Programming
    Replies: 2
    Last Post: 04-07-2007, 02:45 AM
  3. loop needed also how to make input use letters
    By LoRdHSV1991 in forum C Programming
    Replies: 3
    Last Post: 01-13-2006, 05:39 AM
  4. trouble with a for loop with an if statement nested in
    By phoenix-47 in forum C++ Programming
    Replies: 4
    Last Post: 12-14-2005, 04:24 PM
  5. I need help as soon as possible.
    By hyrule in forum C++ Programming
    Replies: 7
    Last Post: 11-09-2005, 05:49 PM