Thread: lotto program homework help

  1. #1
    Registered User
    Join Date
    Mar 2018
    Posts
    9

    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. #2
    Registered User
    Join Date
    Dec 2017
    Posts
    1,632
    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.
    A little inaccuracy saves tons of explanation. - H.H. Munro

  3. #3
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,787
    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. #4
    Registered User
    Join Date
    Mar 2018
    Posts
    9
    Quote Originally Posted by Hodor View Post
    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. #5
    Registered User
    Join Date
    Mar 2018
    Posts
    9
    Quote Originally Posted by john.c View Post
    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. #6
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,787
    Quote Originally Posted by apelletier View Post
    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. #7
    Registered User
    Join Date
    Dec 2017
    Posts
    1,632
    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;
    Last edited by john.c; 03-10-2018 at 08:44 PM.
    A little inaccuracy saves tons of explanation. - H.H. Munro

  8. #8
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,787
    Edit: comment no longer relevant

  9. #9
    Registered User
    Join Date
    Mar 2018
    Posts
    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. #10
    Registered User
    Join Date
    Mar 2018
    Posts
    9
    Quote Originally Posted by john.c View Post
    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. #11
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    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.
    Last edited by stahta01; 03-11-2018 at 01:37 PM.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  12. #12
    Registered User
    Join Date
    Mar 2018
    Posts
    9
    Quote Originally Posted by stahta01 View Post
    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. #13
    Registered User
    Join Date
    Dec 2017
    Posts
    1,632
    Quote Originally Posted by apelletier View Post
    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.
    A little inaccuracy saves tons of explanation. - H.H. Munro

  14. #14
    Registered User
    Join Date
    Mar 2018
    Posts
    9
    Quote Originally Posted by john.c View Post
    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. #15
    Registered User
    Join Date
    Mar 2018
    Posts
    9
    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 subscribe to a feed

Similar Threads

  1. Lotto SystemV5
    By BIGDENIRO in forum C Programming
    Replies: 9
    Last Post: 10-22-2013, 05:11 PM
  2. Lotto program
    By MrQ in forum C Programming
    Replies: 3
    Last Post: 06-24-2013, 12:35 AM
  3. lotto program in c
    By vyshant in forum C Programming
    Replies: 7
    Last Post: 11-07-2011, 12:19 PM
  4. Lotto problem
    By Roaring_Tiger in forum C Programming
    Replies: 11
    Last Post: 03-13-2003, 10:17 PM
  5. Lotto game in C
    By fun2sas in forum C Programming
    Replies: 2
    Last Post: 03-02-2003, 07:19 PM

Tags for this Thread