# Thread: lotto program homework help

1. ## lotto program homework help

Hello all,

I have done some digging through the archives, and cannot find anything that addresses my exact query, however if it has been answered before, feel free to delete.

Here are the instructions:
Develop a program to play lottery. The program randomly generates a lottery of a two-digit number, prompts the user to enter a two-digit number, and determines whether the user wins according to the following rules:

1. If the user input matches the lottery number in the exact order, the award is \$10,000.
2. If all digits in the user input matches all digits in the lottery number, the award is 3,000
3. If one digit in the user input matches a digit in the lottery number, the award is \$1,000 <-- this is where i am most confused.

Note that the digits of a two-digit number may be 0. If a number is less than 10, we assume the number is preceded by a 0 to form a two-digit number. For example, number 8 is treated as 08 and number 0 is treated as 00 in the program.

here is what I have so far (be gentle, I am still pretty new to this):
Code:
```#include <stdio.h>

#include <stdlib.h>
main() {
srand((unsigned)time(0));
int userNumber, winningNumber, winningNumberInv = 0, remainder, matchOne = 0;
printf("Enter your lottery pick (two digits):");
scanf_s("%i", &userNumber);

winningNumber = rand() % 100;
printf("The lottery number is %.2i\n", winningNumber);
if (userNumber == winningNumber)
printf("\nExact match: you win \$10,000");
while (winningNumber != 0)
{
remainder = winningNumber % 10;
winningNumberInv = winningNumberInv * 10 + remainder;
winningNumber /= 10;
}
if (userNumber == winningNumberInv)
printf("Match all digits: you win \$3000\n");

while (winningNumber != 0)
{
matchOne = winningNumber % 10;
winningNumber /= 10;
//this is where i am stuck.
}

system("pause");

}```
any hints, tips, or help will be greatly appreciated. 2. You know about integer division and the modulus operator. That's good. But you don't need the loop since you're only dealing with two digits. So how about:
Code:
```    int number = rand() % 100;
int units = number % 10;
int tens = number / 10;```
Maybe do the same with the guess. That way you can compare the individual digits. 3. Code:
```    if (a == c && b == d)
return 10000;

if (a < b)
swapints(&a, &b);
if (c < d)
swapints(&c, &d);
if (a == c && b == d)
return 3000;

if (a == c || b == c || a == d || b == d)
return 1000;``` 4. Originally Posted by Hodor Code:
```    if (a == c && b == d)
return 10000;

if (a < b)
swapints(&a, &b);
if (c < d)
swapints(&c, &d);
if (a == c && b == d)
return 3000;

if (a == c || b == c || a == d || b == d)
return 1000;```
Thanks very much for your reply. So for this solution, would I need to have two random single digit integers, then? 5. Originally Posted by john.c You know about integer division and the modulus operator. That's good. But you don't need the loop since you're only dealing with two digits. So how about:
Code:
```    int number = rand() % 100;
int units = number % 10;
int tens = number / 10;```
Maybe do the same with the guess. That way you can compare the individual digits.
Thanks! I will give this a try. 6. Originally Posted by apelletier Thanks very much for your reply. So for this solution, would I need to have two random single digit integers, then?
Looks like you'd have to split the 2 2-digit numbers up yeah.

Notice that the second condition (the part inside the 'if') is the same as the first once the numbers are sorted 7. Code:
```int x = (a == c) + (b == d);
if (x == 2) return 10000;

int y = (a == d) + (b == c);
if (y == 2) return 3000;

return (x || y) * 1000;``` 8. Edit: comment no longer relevant  9. I didn't use swapints, because we haven't learned that yet in our class, so I didn't think my professor would want us to use it. I set the winning numbers to 5 and 6 for now instead of randomly generating so I could more easily test my code. The problem I am running into now is that the last if statement seems to be true regardless of numbers scanned in, and I cannot figure out why.

Code:
```#include <stdio.h>
#include <stdlib.h>
main() {
srand((unsigned)time(0));
int userDigit1, userDigit2, winningDigit1, winningDigit2;
printf("Enter your lottery pick (two digits):");
scanf_s("%1i%1i", &userDigit1, &userDigit2);

winningDigit1 = 5;
winningDigit2 = 6;
printf("The lottery number is %i%i\n", winningDigit1, winningDigit2);
if (userDigit1 == winningDigit1 && userDigit2 == winningDigit2)
printf("Exact match: you win \$10,000!\n");
if (userDigit1 == winningDigit2 && userDigit2 == winningDigit1)
printf("Match all digits: you win \$3000!\n");
if (userDigit1 == winningDigit1 || userDigit1 == winningDigit2 || userDigit2 == winningDigit1 || userDigit2 == winningDigit2);
printf("Match one digit: you win \$1000!\n");

system("pause");

}``` 10. Originally Posted by john.c Code:
```int x = (a == c) + (b == d);
if (x == 2) return 10000;

int y = (a == d) + (b == c);
if (y == 2) return 3000;

return (x || y) * 1000;```
This worked, but I'm not 100% sure I understand it. Is this because true is 1 and false is 0, so true plus true equals 2? 11. You need to lookup how to use else if in C/C++.

Edit: This is about post number 9 that has 3 if statements that likely needs to be
if, else if, else Note: I forget right now how C does "else if" look it up.

Tim S. 12. Originally Posted by stahta01 You need to lookup how to use else if in C/C++.

Edit: This is about post number 9 that has 3 if statements that likely needs to be
if, else if, else Note: I forget right now how C does "else if" look it up.

Tim S.
Okay, thanks! This helps! 13. Originally Posted by apelletier This worked, but I'm not 100% sure I understand it. Is this because true is 1 and false is 0, so true plus true equals 2?
Yes, that's right. I wouldn't use it if I was you, though. I meant it as a bit of a joke I guess.

The last thing you posted is almost working. Just use else/if's like stahta01 said, but also note that you have an extra semicolon at the end of the last if. The extra semicolon creates an empty statement that becomes the entire body of the if, so get rid of it. 14. Originally Posted by john.c Yes, that's right. I wouldn't use it if I was you, though. I meant it as a bit of a joke I guess.

The last thing you posted is almost working. Just use else/if's like stahta01 said, but also note that you have an extra semicolon at the end of the last if. The extra semicolon creates an empty statement that becomes the entire body of the if, so get rid of it.
Thanks! This also helped! 15. Thanks, everyone who helped me with this. I finally got it, but I definitely could not have done it without your help. Popular pages Recent additions lottery, matches, number, program, user 