Thread: Question about loops

  1. #1
    Registered User
    Join Date
    Dec 2008
    Posts
    2

    Question Question about loops

    Ok so I'm pretty lost in this programming class, I have to write a program for PowerBall, where the user enters a date and the number of people playing and the program outputs 6 random numbers for each person. This is what I've got so far (try not to laugh!)

    Code:
    #include <iostream>
    #include <string>
    #include <cstdlib>
    #include <fstream>
    
    using namespace std;
    
    //declare variables
    string dateNum;
    int participants;
    int rand1, rand2, rand3, rand4, rand5, PB;
    
    void getInfo ();
    void generateRandom ();
    
    int main ()
    {
    	getInfo();
    	generateRandom ();
    	cout<<dateNum<<endl;
    	cout<<rand1<<" "<<rand2<<" "<<rand3<<" "<<rand4<<" "<<rand5<<"     "<<PB<<endl;
    	return 0;
    }
    void getInfo()
    {
    	//get information from user
      	cout<<"Enter date of lottery: (mm/dd/yy): ";
    	cin>>dateNum;
    	cout<<endl;
    	cout<<"Enter number of participants: ";
    	cin>>participants;
    	cout<<endl;
    }
    
    void generateRandom ()
    {
    	//generate random numbers
    	for(participants= 1; participants <= 25; participants++)
    	{
    		rand1 = (rand()%49)+1;	
    		rand2 = (rand()%49)+1; 	
    		rand3 = (rand()%49)+1;	
    		rand4 = (rand()%49)+1;	
    		rand5 = (rand()%49)+1;	
    		PB = (rand()%49)+1;	
    	}
    }
    But I can't get it to loop based on the number of participants. Thanks so much in advance!!

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    You probably have to loop for each person, and then in an inner loop 6 times to create each random number. And you'd have to store them in an array.

    If you are going to use functions, I'd suggest you use parameters and return values, otherwise it is quite pointless to break up the program into functions in the first place.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> otherwise it is quite pointless to break up the program into functions in the first place.

    I disagree with this, but I think your larger point is that global variables are generally not good practice and the data should be passed to and from the functions instead of via global variables.

  4. #4
    Registered User
    Join Date
    Dec 2008
    Posts
    2
    Thank you!

    And I agree about the functions, I'm just required to have 3 in addition to the main, so I just broke up the steps into separate functions. I ended up doing this instead:

    Code:
    void generateRandom ()
    {
    	int counter=0;
    	for(;;)
    	{
    		if (counter<participants)
    		{
    			cout<<(rand()&#37;49)+1<<"	"<<(rand()%49)+1<<"	"<<(rand()%49)+1<<"	"<<(rand()%49)+1<<"	"<<(rand()%49)+1<<"		"<<(rand()%49)+1<<endl;
    			counter++;
    		}
    		else
    			break;
    	}
    }

  5. #5
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Quote Originally Posted by lroberts1016 View Post
    Thank you!

    And I agree about the functions, I'm just required to have 3 in addition to the main, so I just broke up the steps into separate functions. I ended up doing this instead:

    Code:
    void generateRandom ()
    {
        int counter=0;
        for(;;)
        {
            if (counter<participants)
            {
                cout<<(rand()%49)+1<<"    "<<(rand()%49)+1<<"    "<<(rand()%49)+1<<"    "<<(rand()%49)+1<<"    "<<(rand()%49)+1<<"        "<<(rand()%49)+1<<endl;
                counter++;
            }
            else
                break;
        }
    }
    What is the deal with the red code? Why not loop that?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. noob question re: switch case & loops
    By scooglygoogly in forum C++ Programming
    Replies: 13
    Last Post: 08-25-2008, 11:08 AM
  2. question on GCD and for loops
    By dals2002 in forum C Programming
    Replies: 9
    Last Post: 03-15-2008, 01:59 AM
  3. question about while loops
    By volk in forum C++ Programming
    Replies: 4
    Last Post: 03-22-2003, 09:21 AM
  4. opengl DC question
    By SAMSAM in forum Game Programming
    Replies: 6
    Last Post: 02-26-2003, 09:22 PM
  5. Very simple question, problem in my Code.
    By Vber in forum C Programming
    Replies: 7
    Last Post: 11-16-2002, 03:57 PM