For Loops

I'm just writing a lottery game so that the computer randomly generates 3 sets of 3 numbers : ex) 123 456 789

My problem is finding to see if the players numbers match the lottery numbers. If the player matches 1 set, they win 3rd prize, if player matches 2 sets, the player wins 2nd prize and if the player gets all them right they win the grand prize.

I don't know if using a for loops is the best idea here so if you have another idea that would work better please share.

I'm storing myNumbers and lotteryNumbers in an array

1
2
3
4
5
6
7
8
9
10
11
12
13
  for (int i = 0; i < 3; ++i)
	{
		if (playersNumbers[i] == lotteryNumbers[i])
		{
			cout << "Congradulations! You got all the numbers right!\n"
				<< "You won $50,000!" << endl;
		}

		if (playersNumbers[i] != lotteryNumbers[i])
		{
			cout << "You won nothing." << endl;
		}
	}



And another question, is there a way to get random number between 100-999. This is how I did it

1
2
3
4
5
6
7
8
for (int i = 0; i < 3; ++i)
	{
		lotteryNumbers[i] = (rand() % 999);
		if (lotteryNumbers[i] < 100)
		{
			lotteryNumbers[i] += 100;
		}
	}
Last edited on
1
2
3
4
5
6
7
8
9
10
       for (int i = 0; i < 3; ++i)
	{
		if (playersNumbers[i] == lotteryNumbers[i]){
		cout << "congrats" endl;  //this line is printing every time a # matches
		}

		if (playersNumbers[i] != lotteryNumbers[i]){
			cout << "You won nothing." << endl; //same trouble
		}
	}

I am not a professional or anything... but I can tell you that you need to rethink the logic of your code.

Lets go through this again:
You first need to see if the players numbers match a set.
Lets imagine you have this array: [123, 456, 789] for winning #s
and the player draws [123, 555, 789]
With your code as is, it would cout that they are a winner of $50,000 when they are not. (actually it would cout it 2 times along with "you won nothing" which is confusing to any avid gambler)

The player has matched the first and last places in the array, and thus we need to tell him he won the 2nd place price.

For a 1st place, 2nd place, and 3rd place you need to keep track of the amount of times a player has matched up. "if lotteryNumber[i] == playerNumber[i] { timesWon++;
}
then at the end of your code use the timeWon value to determine how many times they truly won.

--------
for your second problem:
int random_num;
random_num = 1+rand()%999;
this will give you an int between 1 and 999

Last edited on
Yeah when I run it it outputs it 3 times nomatter what. I saw that. So would a for loop be the best to see of they matched 2 out of the 3? Using a for loop i have no idea hoe to code that. I only can think of how to make it with all 3 or none and even then it prints out 3 times
That just happens to be my specialty at the moment!
1
2
3
4
5
int amount_won= 0;

for(int = 0; i < 3; i++) {
  if(lottoNumbers[i] == playerNumbers[i]){
      amount_won ++; }

this bit of code will set amount_won = to the amount of times the player has won.

now try using an if statement (or a switch perhaps if you know how to use that) to cout what they won.

Your problem was that you were trying to tell the player if they won before you really knew what they won.
Last edited on
I could use a series of if- elseif statements but I want to do it in the most efficent way
Ahhh okay I see how that works, thanks
Ok. This guy isn't the most professional teacher of C++ but he will teach you the basics of c++ and then you will have to do some reading and practice to get a better grasp on the language.

Try this video: http://thenewboston.org/watch.php?cat=16&number=25
it goes over switch statements that you can use to do this.
Topic archived. No new replies allowed.