Hello all,

I am trying to make a lottery number generator. So I need 6 random numbers.

The current code below displays the same number 6 times. Thoughts?

#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;


char userchoice;
int main()
{
	cout<<"Welcome to The Lottery Wizard!"<<endl<<endl;
	cout<<"This program will allow you to play the lottery from"<<endl;
	cout<<"the comfort of your computer desk."<<endl<<endl;

	

   cout  << "Do you want to play the Lottery? (y = yes, n = no):  ";

            cin.sync();
            cin.get(userchoice);
            userchoice = tolower(userchoice);  //convert user's answer to lowercase
                                                                             

            while (userchoice != 'y' && userchoice != 'n')

            {

                        cout << "Invalid response.  Enter a y for yes or n for no: ";

                        cin.sync();          // clear out buffer
                        cin.clear();          // set read to good
			            cin.get(userchoice);

            }

 system("cls");

 

 while (tolower(userchoice) == 'y')
  {           

cout<<"The six random lottery numbers are:";

int number_back;

srand( time (NULL) ); //gets seed from clock

number_back = rand() % 53 + 1; //formula for 1-53 random number




for (int count = 0; count < 6 ; count++)

cout<<number_back<<endl;

cout  << "Do you want to play the Lottery? (y = yes, n = no):  ";

            cin.sync();
            cin.get(userchoice);
            userchoice = tolower(userchoice);  //convert user's answer to lowercase
                                                                             

            while (userchoice != 'y' && userchoice != 'n')

            {

                        cout << "Invalid response.  Enter a y for yes or n for no: ";

                        cin.sync();          // clear out buffer
                        cin.clear();          // set read to good
			            cin.get(userchoice);

            }

 }


system("cls");



cout<<"Thanks for playing"<<endl<<endl;

return 0;
}

Recommended Answers

All 6 Replies

You are generating the number, and then outputting it 6 times!

You need to move the random generation into the loop

for (int count = 0; count < 6 ; count++)
{
number_back = rand() % 53 + 1;
cout<<number_back<<endl;
}

Try:

srand( time (NULL) ); //gets seed from clock


    for (int count = 0; count < 6 ; count++)
    {

        number_back = rand() % 53 + 1; //formula for 1-53 random number

    cout<<number_back<<endl;
}

Oh sorry, TOO SLOW, daviddoria has already answered it.

Haha, thanks for the helps guys. Newbie mistake. :).

Also, how would I format them to output horizontal in a row. right now they are straight up and down.

instead of putting cout<<number_back<<endl;
put: cout<<number_back<<' ';

i haven't tryed it so sorry if its wrong.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.