Error checking issues

Hi Folks,

I am learning C++ and I was doing fairly well until I ran into functions and arrays. I am trying to create a simple program that will allow me to create or choose lottery ticket numbers, check those tickets against the winning lottery numbers, and print out all of my tickets with details including the number of correct numbers on each ticket and an alert for all numbers correct. I am starting assuming 5 tickets arbitrarily and hope to be able make that number a variable after I solve more fundamental problems. At the moment I have code that no longer produces syntax errors, so I know that I have created some logic errors that I could use assistance troubleshooting. The first part om my main works up until I get to num_errorcheck. It sometimes works and sometimes does not. It allows numbers outside of the desired range. Sometimes when i select random number generation I am then prompted to enter numbers. I cannot figure out where my errors are Please help?!! Thank you in advance :)

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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
 //5 tickets. Either randomly select 6 numbers (1-60) for each ticket or 6 values.
//Compare all their tickets to the winning lotto numbers.
//For each ticket show numbers and how many matches.
//Issues:	Running into duplicate numbers on tickets. Error check for dupes on each ticket.
//			Error check on num_errorcheck is not working properly

#include <iostream>
#include <string>
#include <fstream>
#include <ctime>
#include <cmath>
#include <cstdlib>

using namespace std;

int menu_errorcheck(int users_choice)
{
	//Error check the entered users numbers to see if they are either 1 or 2
	while (users_choice > 2 || users_choice < 1)
	{
		cout << endl << endl;
		cout << "Invalid option" << endl;
		cout << "Please enter choice again: ";
		cin >> users_choice;
	}

	return users_choice;
}


void num_errorcheck(int lotto[], int array_size)
{
	//this is supposed to error check the entered users numbers to see if they are within 1-60. *This is not working. 
	//It is not checking for dupes and allowing numbers out of range. Other anomalies. ???

	while (lotto[0] > 60 || lotto[0] < 1 || lotto[1] > 60 || lotto[1] < 1 || lotto[2] > 60 || lotto[2] < 1 
		|| lotto[3] > 60 || lotto[3] < 1 || lotto[4] > 60 || lotto[4] < 1 || lotto[5] > 60 || lotto[5] < 1)
	{
		for (int i = 0; i < array_size; i++)
		{
			cout << "Invalid number entry, try again.";
			cout << "Please enter " << array_size << " values: ";
			cin >> lotto[i];
		}

		cout << endl << endl;
		break;

	}
}


void populate_ticket(int lotto[], int array_size)
{
	//Populate tickets manually or automatically when called upon
	cout << "\t***** Menu *****" << endl;
	cout << "1. Choose lotto numbers for ticket" << endl;
	cout << "2. Randomize lotto numbers for ticket" << endl;
	cout << "Enter choice: ";
	int users_choice;
	cin >> users_choice;

	users_choice = menu_errorcheck(users_choice);
	cout << endl << endl;

	switch (users_choice)
	{
	case 1:
		//function allows the user to enter 6 (unique?) lotto ticket numbers
		cout << "Please enter " << array_size << " values: ";

		for (int i = 0; i < array_size; i++)
		{
			cin >> lotto[i];
		}

		cout << endl << endl;
		break;

	case 2:
		//Randomly gen 6 (unique?) lotto ticket numbers
		cout << "Randomizing lotto ticket numbers" << endl;

		srand(time(0));

		for (int i = 0; i < array_size; i++)
		{
			lotto[i] = rand() % 60;
		}

		cout << endl << endl;
		break;
	}
}

int main()
{
	//Declare variables
	int users_choice;
	const int array_size = 6;
	int ticket_1[array_size];
	int ticket_2[array_size];
	int ticket_3[array_size];
	int ticket_4[array_size];
	int ticket_5[array_size];


	//Menu: Select random number gen or pick numbers. (do-while?)
	cout << "Starting with 5 tickets that each contain 6 numbers between 1 - 60." << endl;
	cout << "Choose the 6 numbers manually or have the numbers randomized." << endl;
	cout << endl;
	cout << "\t\t Press [Enter] to begin.";
	cin.ignore();
	cout << endl << endl;


	//Switch board of either (1) picked numbers or (2) random numbers being given to them and sends info to array


	populate_ticket(ticket_1, array_size);
	num_errorcheck(ticket_1, array_size);

	cout << endl << endl;

	cout << "\t\t Press [Enter] to choose ticket 2.";

	cout << endl << endl;

	populate_ticket(ticket_2, array_size);
	num_errorcheck(ticket_2, array_size);

	cout << endl << endl;

	cout << "\t\t Press [Enter] to choose ticket 3.";

	cout << endl << endl;

	populate_ticket(ticket_3, array_size);
	num_errorcheck(ticket_3, array_size);

	cout << endl << endl;

	cout << "\t\t Press [Enter] to choose ticket 4.";

	cout << endl << endl;

	populate_ticket(ticket_4, array_size);
	num_errorcheck(ticket_4, array_size);

	cout << endl << endl;

	cout << "\t\t Press [Enter] to choose ticket 5.";

	cout << endl << endl;

	populate_ticket(ticket_5, array_size);
	num_errorcheck(ticket_5, array_size);


	cout << "Your ticket numbers are: " << endl;

	cout << "Ticket 1: ";
	for (int i = 0; i < array_size; i++)
	{
		cout << ticket_1[i] << " ";

	}

	cout << endl;

	cout << "Ticket 2: ";
	for (int i = 0; i < array_size; i++)
	{
		cout << ticket_2[i] << " ";

	}

	cout << endl;

	cout << "Ticket 3: ";
	for (int i = 0; i < array_size; i++)
	{
		cout << ticket_3[i] << " ";

	}

	cout << endl;

	cout << "Ticket 4: ";
	for (int i = 0; i < array_size; i++)
	{
		cout << ticket_4[i] << " ";

	}

	cout << endl;

	cout << "Ticket 5: ";
	for (int i = 0; i < array_size; i++)
	{
		cout << ticket_5[i] << " ";

	}


	//Array containing stored user lotto numbers as well as the actual lotto numbers is accessed to
	//check how many matches there are (if...then...else,)



	//The system displays how correct each ticket is (if...then...else)


	cout << endl << endl;
	system("pause");
	return 0;
}
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <iomanip>

const int MIN_NUMBER = 1 ;
const int MAX_NUMBER = 60 ;
// http://en.cppreference.com/w/cpp/language/static_assert
static_assert( MIN_NUMBER < MAX_NUMBER, "invalid min/max range" ) ; // sanity check

const int NUMBERS_PER_TICKET = 6 ;
const int NUM_TICKETS = 6 ;
static_assert( NUMBERS_PER_TICKET > 0 && NUM_TICKETS > 0, "invalid array size" ) ; // sanity check

// ticket is just another name for an array of NUMBERS_PER_TICKET integers
using ticket = int[NUMBERS_PER_TICKET] ;

// return integer in range [minv,maxv] entered by the user
int get_int_between( int minv, int maxv, const char* prompt = "?" )
{
    std::cout << prompt << '[' << minv << '-' << maxv << "]: " ;
    int n ;

    if( std::cin >> n ) // user entered a number
    {
        if( n >= minv && n <= maxv ) return n ; // within range, return it
        else std::cout << "the value is out of range. try again\n" ;
    }
    else // user did not enter a number
    {
        std::cout << "that is not an integer. try again\n" ;
        std::cin.clear() ; // clear the failed state of the stream
        std::cin.ignore( 1000, '\n' ) ; // throw away invalid characters in the input buffer
    }

    return get_int_between( minv, maxv, prompt ) ; // try again
}

// note: the ticket is passed my reference
void random_fill( ticket& tkt ) // fill a ticket with random values
{
    // for each int in the ticket, assign a random value in [MIN_NUMBER,MAX_NUMBER]
    // range based loop: http://www.stroustrup.com/C++11FAQ.html#for
    for( int& v : tkt )
        v = MIN_NUMBER + std::rand() % ( MAX_NUMBER - MIN_NUMBER + 1 ) ;
        // note: consider using facilities in <random>
        // http://en.cppreference.com/w/cpp/numeric/random
}

void user_fill( ticket& tkt ) // fill a ticket with values entered by the user
{
    std::cout << "Please enter " << NUMBERS_PER_TICKET << " values\n";
    // for each int in the ticket, assign a value entered by the user
    for( int& v : tkt ) v = get_int_between( MIN_NUMBER, MAX_NUMBER ) ;
}

void fill( ticket& tkt )
{
	std::cout << "\t***** Menu *****\n"
              << "1. Choose lotto numbers for ticket\n"
              << "2. Randomly generate lotto numbers for ticket\n" ;

	const int choice = get_int_between( 1, 2, "Enter choice" ) ;
	if( choice == 1 ) user_fill(tkt) ;
	else random_fill(tkt) ;
}

void print( const ticket& tkt )
{
    std::cout << "[ " ;
    for( int v : tkt ) std::cout << std::setw(2) << v << ' ' ;
    std::cout << "]\n" ;
}

int main()
{
    // seed the legacy rng once, at the start of the program
    std::srand( std::time(nullptr) ) ;

    ticket lotto_tickets[NUM_TICKETS] ; // array of NUM_TICKETS tickets

    // fill the tickets in lotto_tickets
    for( int i = 0 ; i < NUM_TICKETS ; ++i ) // for each ticket
    {
        std::cout << "ticket #" << i+1 << "\n---------\n" ;
        fill( lotto_tickets[i] ) ; // fill the ticket
    }

    // print the filled tickets
    std::cout << "\n\n" ;
    for( int i = 0 ; i < NUM_TICKETS ; ++i ) // for each ticket
    {
        std::cout << "ticket #" << i+1 << ' ' ;
        print( lotto_tickets[i] ) ; // print the ticket
    }
}
Thank You JLB!
In order to match the characteristic of an actual lottery where a number is taken out of play once it is chosen, I am trying to take the chosen number out of play so that it cannot possibly be drawn again for each individual ticket. Can anyone point me to a formula or method for this that I can add to my error check?
Something along these lines, perhaps:

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
#include <iostream>
#include <vector>
#include <algorithm>
#include <random>

const int MIN_NUMBER = 1 ;
const int MAX_NUMBER = 60 ;
// http://en.cppreference.com/w/cpp/language/static_assert
static_assert( MIN_NUMBER < MAX_NUMBER, "invalid min/max range" ) ; // sanity check

int main()
{
    // create a vector to hold the set of available numbers
    // https://cal-linux.com/tutorials/vectors.html
    std::vector<int> available_numbers ;
    // fill it up with all the possible numbers that could be drawn
    for( int i = MIN_NUMBER ; i <= MAX_NUMBER ; ++i ) available_numbers.push_back(i) ;

    // to simulate random drawing of numbers, shuffle the available numbers
    // like one would thoroughly shuffle a pack of cards
    // http://en.cppreference.com/w/cpp/algorithm/random_shuffle
    // http://en.cppreference.com/w/cpp/numeric/random
    std::shuffle( available_numbers.begin(), available_numbers.end(),
                  std::mt19937( std::random_device{}() ) ) ;

    const int NDRAWS = 30 ; // simulate 30 draws
    std::cout << "numbers drawn, in order: [ " ;
    for( int i = 0 ; i < NDRAWS ; ++i )
    {
        // for each random draw, pick the number at the back of the vector
        // (this is random, since the sequence was randomly shuffled)
        std::cout << available_numbers.back()<< ' ' ;

        // and remove the picked number from the set of available numbers
        available_numbers.pop_back() ;
    }

    std::cout << "]\nnumbers left after " << NDRAWS << " draws: [ " ;
    for( int v : available_numbers ) std::cout << v << ' ' ;
    std::cout << "]\n" ;
}

http://coliru.stacked-crooked.com/a/ea16d26d65745b68
Thank you again JLB, but I am afraid that this coding is way out of my grasp at my present stage of learning and I am trying to take it in stages. I was able to study your first suggestion for the bulk of the code, break it down and rewrite it so that I can understand it and it jives with what i have studied so far, but I just cannot grasp this one.
I do have another question though. I am just finishing this program (without the code to make each number unique per ticket yet) and I am trying to compare the winning ticket number against each of the user tickets. While only adding to this code, in order to stay within my present learning and not open up whole levels of advanced difficulties, access any special libraries, or use operands and methods that I have not yet studied, how can I fix this code so that each integer from each ticket is checked against each integer from the winning ticket? I realize at this point that I am currently comparing integer 0 to integer 0, integer 1 to integer 1.... and i cannot find a method for the check that I need.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
	//compare each lottery ticket against winning ticket for matching numbers
	for (int i = 0; i < 5; i++)
	{
		for (int j = 0; j < 1; j++)
		{
			if (lotto_tickets[i] == winning_ticket[j])
			{
				cout << "ticket #" << i + 1 << ' ';
				cout << ++matches << "Matches " << ": ";
				print(lotto_tickets[i]);
			}
			else
			{
				cout << "ticket #" << i + 1 << ' ';
				cout << "Matches 0: ";
				print(lotto_tickets[i]);
			}
		}
	}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
for( const auto& ticket : lotto_tickets ) // for each lottery ticket
{
    int cnt_matches = 0 ;

    for( int number_in_ticket : ticket ) // for each number in this lottery ticket
    {
        // check if this number is present in the winning ticket

        // for each number in the winning ticket
        for( int number_in_winning_ticket : winning_ticket )
        {
            if( number_in_winning_ticket == number_in_ticket ) // matches the number
            {
                ++cnt_matches ;
                break ;
            }
        }
    }

    // print the ticket and the count of matches for this ticket
    print(ticket) ;
    std::cout << "#matches: " << cnt_matches << "\n\n" ;
}


Or using classic for loops (best avoided for this):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
for( int i = 0 ; i < NUM_LOTTERY_TICKETS ; ++i ) // for each lottery ticket
{
    int cnt_matches = 0 ;
    // for each number in this lottery ticket
    for( int j = 0 ; j < NUMBERS_PER_LOTTERY_TICKET ; ++j )
    {
        // check if this number is present in the winning ticket

        // for each number in the winning ticket
        for( int k = 0 ; k < NUMBERS_IN_WINNING_TICKET ; ++k )
        {
            if( winning_ticket[k] == lotto_tickets[i][j] ) // matches the number
            {
                ++cnt_matches ;
                break ;
            }
        }
    }

    // print the ticket and the count of matches for this ticket
    print( lotto_tickets[i] ) ;
    std::cout << "#matches: " << cnt_matches << "\n\n" ;
}
Topic archived. No new replies allowed.