Matching Game

The user has to guess 6 numbers before they are generated. Your program will then tell the
user what are the matched numbers. If the user chooses any number less than 1 or more
than 42, your program has to prompt the user to key in another set of 6 numbers. Your
program should ask the user whether he/she wants to continue playing the game or not. If
the user chooses to stop, the main menu will be displayed.

Example :
Example 1:
Please choose 6 numbers between 1 until 42:
2 5 8 13 22 31
The 6 random numbers generated are:
22 16 23 2 39 40
Matched numbers(s):
2 22

Example 2:
Please choose 6 numbers between 1 until 42:
1 2 3 4 5 6
The 6 random numbers generated are:
32 16 23 10 18 29
Matched numbers(s):
None

Problem : My code doesnt prompt the message where when user enter less than 42 or more than 1 error message.

my 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
48
49
50
51
52
53
54
55
  #include "stdafx.h"
#include <iostream>
#include <ctime>
#include <cstdlib>

using namespace std;

int main()
{
	srand((unsigned int)time(NULL));

	while (true)
	{
		int num;
		int numrand;
		int tries = 0;

		numrand = rand() % 42 + 1;//give a random number between 1 and 42
		cout << "MATCHING GAME" << endl;
		cout << "------------------------------------" << endl;
		cout << "Choose 6 NUMBERS BETWEEN 1 & 42 " << endl;
		cout << endl;

		while (true)
		{
			cout << "Please choose 6 numbers between 1 and 42 " << endl;
			for (int x = 0; x < 5; x++)
			{
				cin >> num;
			}
			//check number
			if (num == numrand)
			{
				cout << "Matched numbers(s): \n";
				cout << numrand << endl;
				break;
			}
			else if (num != numrand)
			{
				cout << "Matched numbers(s): \n";
				cout << "None" << endl;
				cout << endl;
			}
		}
			//check for error
			if (num > 1 && num < 42)
			{
				cout << "Value must be less than 42 and more than 1" << endl;
				cout << "Please choose 6 numbers between 1 and 42 " << endl;
				cin >> num;
			}

		return 0;
	}
}

Last edited on
I’ve just skimmed through the code, so I don’t know if it’s the main problem, but there’s at list one issue:
1
2
3
4
5
int num;
for (int x = 0; x < 5; x++)
{
    cin >> num;
}

num can store one single value, so the only one which is saved once that for-loop ends is the last one. The first fours are lost.

By the way (second issue): your loop should iterate 6 times, not 5...
@Enoizat

thanks for the replies.
should I use array for num?
Topic archived. No new replies allowed.