Lottery game only using if conditions

I need to write a lottery game application that will generate three random numbers each between 0 and 9. The user should guess three numbers and the program should compare each of the user's guess to the three random and display an appropriate output based on whether they got:

-any one matching
-two matching
-three matching, not in order
-three matching in exact order
-or no matches at all

The major catch here is that I cannot use any loops whatsoever. I have to do this using only if conditions and or switch statements. I cannot use functions, arrays or anything advanced only if and or switch. Is this possible to do without having to do if conditions for every single possible combination? I've tried using counters to narrow things down but that only got me more confused. Can anyone help point me in the right direction? Thanks.

What I have so far just checks for one matching, three matching in order, or none matching
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include <iostream> 
#include <ctime>
using namespace std;
int main() //function required header
{
	//declare variables
	int guessNum, guessNum2, guessNum3;
	int randomNum, randomNum2, randomNum3;
	int counter3 = 0;
	int counter2 = 0;
	int counter = 0;

	srand((unsigned)time(NULL));  // need to create unique random numbers for every execution
	// generate the random numbers
	randomNum = rand() % 10;
	randomNum2 = rand() % 10;
	randomNum3 = rand() % 10;

	cout << "random number 1 = " << randomNum << endl;
	cout << "random number 2 = " << randomNum2 << endl;
	cout << "random number 3 = " << randomNum3 << endl;

	cout << "Enter first guess between 0 and 9: ";
	cin >> guessNum;
	cout << "Enter second guess between 0 and 9: ";
	cin >> guessNum2;
	cout << "Enter third guess between 0 and 9: ";
	cin >> guessNum3;

	//three match in order
	if (guessNum == randomNum && guessNum2 == randomNum2 && guessNum3 == randomNum3)
	{
		cout << "\nYou guessed all three correct in order you win 1$ million!" <<endl;
	}

	//none match
	else if (guessNum != randomNum && guessNum2 != randomNum2 && guessNum3 != randomNum3)
	{
		cout << "\nYou guessed none: $0!" << endl;
	}

	//one match
	else if (guessNum == randomNum || guessNum2 == randomNum2 || guessNum3 == randomNum3)
	{
		cout << "\nYou guessed one: $10!" << endl;
	}


	system("Pause");
	return 0;


}
Last edited on
Really glad I never took an assignment this annoying. I haven't thought of any methods yet that could make this less repetitive but I do see some problems with your current logic statements.

This statement only verifies that none match in order, the number guessed can still match out of order.(Dont understand why out of order has to be checked seeing how lotteries aren't won this way) .

1
2
3
4
5
 //none match
	else if (guessNum != randomNum && guessNum2 != randomNum2 && guessNum3 != randomNum3)
	{
		cout << "\nYou guessed none: $0!" << endl;
	}  



This statement only checks if one of the 3 comparisons are true. You can guess two correct numbers and this would still execute saying you guessed one right.

1
2
3
4
else if (guessNum == randomNum || guessNum2 == randomNum2 || guessNum3 == randomNum3)
	{
		cout << "\nYou guessed one: $10!" << endl;
	}
Last edited on
Thanks for the corrections. Now my head hurts even more lol. I may just have to write out all the combinations although I'm sure I'll lose "efficiency points"....
You could probably use nested if statements to simply your life a bit. Regardless, this is a really silly assignment (at what point would you need to do something like this in industry?)
this is a really silly assignment

With out a doubt. I think switch would be your friend here though coupled with a bunch of ifs.
How much order counts plays a big part too, since three out of order is considered does two out of order count as matching or is that only only relevant to three since that is the only condition "out of order" is mentioned. It simplifies greatly if order only matters for all the numbers.
NT3 wrote:
You could probably use nested if statements to simply your life a bit. Regardless, this is a really silly assignment (at what point would you need to do something like this in industry?)

It's not about using something in the industry, it is about making you understand and learn languages and features. In this case it is a "silly assignment" that apparently is to help solidify the use of if statements. Though, I wouldn't call an assignment silly as all programs you write just help solidify what you are learning.

My question is: Why are they only comparing one match or all three matches? Shouldn't they get a prize for matching two?
Last edited on
Yeah, it is a silly and somewhat pointless assignment; it teaches very little in return for the effort. Learning conditional statements is not about writing the same conditional statement with different variables over and over and over again - it does not "solidify" anything worthwhile. Learning programming is not about writing the same (or almost the same) program in the same amateurish way, year after year for a dozen or more years.

@AnEndingAscent, you may find that keeping a count of exact and inexact matches would simplify the code.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <iostream>
#include <cstdlib>
#include <ctime>

int main()
{
    ////////   generate random numbers /////////
    std::srand( std::time(0) );

    const int rand_num1 = std::rand() % 10 ;
    const int rand_num2 = std::rand() % 10 ;
    const int rand_num3 = std::rand() % 10 ;


    //////// accept three guesses ///////////
    int guess1, guess2, guess3 ;
    std::cout << "enter three guesses: " ;
    std::cin >> guess1 >> guess2 >> guess3 ;


    //////// count exact and inexact matches ////////////
    int exact_matches = 0 ;
    int inexact_matches = 0 ;

    if( guess1 == rand_num1 ) ++exact_matches ;
    else if( guess1 == rand_num2 || guess1 == rand_num3 ) ++inexact_matches ;

    if( guess2 == rand_num2 ) ++exact_matches ;
    else if( guess2 == rand_num1 || guess2 == rand_num3 ) ++inexact_matches ;

    if( guess3 == rand_num3 ) ++exact_matches ;
    else if( guess3 == rand_num1 || guess3 == rand_num2 ) ++inexact_matches ;


    /////////// check counts and print out result ///////////////
    if( exact_matches == 3 ) std::cout << "three matching in exact order" ;

    else
    {
        const int matches = exact_matches + inexact_matches ;

        // a switch could be used here
        if( matches == 0 ) std::cout << "no matches at all\n" ;
        else if( matches == 3 ) std::cout << "three matching, not in order\n" ;
        else std::cout << matches << " matching\n" ;
    }
}
Thanks so much guys! Thanks JLBorges!
Topic archived. No new replies allowed.