Cannot get this code to compile!

Hello,

I am working on a program to perform both a linear and binary search to pick a lottery winner. Here is the task:

Write a program that determines if the user is a winner in the lottery game. A lottery ticket buyer purchases 10 tickets a week, always playing the same 10 5-digit “lucky” combinations. The program will initialize an array with these numbers and then lets the player enter this week’s winning 5-digit number. The program should perform both a linear search and a binary search through the list of player’s numbers and report whether or not one of the tickets is a winner this week. Here are the numbers, and the array must be created with the values in this order: 13579, 62483, 26791, 77777, 26792, 79422, 33445, 85647, 55555, 93121. NOTE: The numbers are not in order, they must start this way.

I just can't seem to get it to compile, there are a few errors. Any assistance is appreciated. Here is what I have so far.


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
#include <iostream>

using namespace std;

int searchList(const int [], int, int);
const int SIZE = 10;

int main()

{ // variables
	
	int lotteryNum;
	int lotteryOptions[SIZE] = { 13579, 62483, 26791, 77777, 26792, 79422, 33445, 85647, 55555, 93121 };
	lotteryNum = searchList(lotteryOptions, SIZE, 10);

	//Get the input
	cout << "Please enter a five-digit number: " << endl;
	cin >> lotteryNum;

	if (lotteryNum == -1)
		cout << "Sorry, that is not a winning number. " << endl;
	else
	{
		cout << "Congratulations, the winning number was " << endl;
		cout << (lotteryNum + 1) << endl;
	}
	return 0;
}
int searchList(const int list[], int numElems, int value)
{
	int index = 0;
	int position = -1;
	bool found = false;

	while (index < numElems && !found)
	{
		if (list[index] == value)
		{
			found = true;
			position = index;
		}
		index++;
	}
		return position;
	}

	

// Perform linear search on player's numbers
int linearSearch(const int list[], int elements, int value);
{
		int index = 0;        // Used as a subscript to search array
		int position = −1;    // To record position of search value
		bool found = false;   // Flag to indicate if the value was found
		
		while (index < numElems && !found)
		{
			if (list[index] == value)   // If the value is found
			{
				found = true;           // Set the flag
				position = index;       // Record the value's subscript
			}
			index++;                   // Go to the next element
		}
		return position;               // Return the position, or −1
}
// Binary search

		int binarySearch(const int array[], int numElems, int value)
		const int SIZE = 10;

	{
		int lotteryChoices[SIZE] = { 13579, 62483, 26791, 77777, 26792, 79422, 33445, 85647, 55555, 93121 };
		int lotteryNum;

		//Get the input
		cout << "Please enter a five-digit number: " << endl;
		cin >> lotteryNum;

		lotteryNum = binarySearch(lotteryChoices, SIZE, 10);

		if (lotteryNum == -1)
			cout << "Sorry, that is not a winning number. " << endl;
		else
		{
			cout << "Congratulations, the winning number was " << endl;
			cout << (lotteryNum + 1) << endl;
		}
		return 0;
	}

int binarySearch(const int array[], int size, int value)
{
	int first = 0,
		last = size - 1,
		middle,
		position = -1;
	bool found = false;

	while (!found && first <= last)
	{
		middle = (first + last) / 2;
		if (array[middle] == value)
		{
			found = true;
			position = middle;
		}
		else if (array[middle] > value)
			last = middle = 1;
		else
			first = middle + 1;
	}
	return position;
}

Last edited on
there are a few errors

If the compiler says that there is an error, then it also says the line that has the error and what kind of error it is.

Show the error messages, or else we cannot teach you to understand them.
That makes sense. Here is what I have:

Line 51: expected a declaration/ '{': missing function header (old-style formal list?)
Line 53: '0x2212': this character is not allowed as a first character of an identifier
Line 70: a type qualifier is not allowed on a nonmember function/ syntax error: 'int' should be preceded by ';'/ 'binarySearch': modifiers not allowed on nonmember functions/ 'SIZE': redefinition; different type modifiers
Line 72: expected a declaration/ '{': missing function header (old-style formal list?)

Last edited on
Please re-edit your initial post by enclosing your code example by 'code'-tags, so that we see, which line numbers refer to the code.

(code)
my_code_example
(/code)

! You must replace the '(' and ')' by '[' and ']'
Last edited on
I put it into source code formatting, my mistake.
* You have a bad semi in line 50
* Missing semi in line 69
* Repeated partial body of main in lines 72-90
* SIZE is redeclared in line 70 (look at line 6)
* function binarySearch prototype in line 69 should be above main
Last edited on
So I went through and changed some things and now this is what I have. Still getting some rookie error codes. Any assistance is appreciated

Line 72: expected a declaration/ '{' missing function header (old-style formal list?)
Line 52: expected a declaration/ '{' missing function header (old-style formal list?)
Line 54: '0x2212': this character is notal lowed as a first character of an identifier


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
#include <iostream>

using namespace std;

int binarySearch(const int [], int, int);
int linearSearch(const int [], int, int);
const int numElems = 10;

int main()

 // variables
{	
	int lotteryNum;
	int lotteryOptions[numElems] = {13579, 62483, 26791, 77777, 26792, 79422, 33445, 85647, 55555, 93121};
	lotteryNum = linearSearch(lotteryOptions, numElems, 10);
	
	//Get the input
		cout << "Please enter a five-digit number: " << endl;
		cin >> lotteryNum;

	if (lotteryNum == -1)
		cout << "Sorry, that is not a winning number. " << endl;
	else
	{
		cout << "Congratulations, the winning number was " << endl;
		cout << (lotteryNum + 1) << endl;
	}
	return 0;
}
int linearSearch(const int lotteryOptions[], int numElems, int lotteryNum)
{
	int index = 0;
	int position = -1;
	bool found = false;

	while ((index < numElems) && !found)
	{
		if (lotteryOptions[index] == lotteryNum)
		{
			found = true;
			position = index;
		}
		index++;
	}
		return position;
	}


// Perform linear search on player's numbers
int linearSearch(const int lotteryOptions[], int numElems, int lotteryNum);

{
		int index = 0;        // Used as a subscript to search array
		int position = −1;    // To record position of search value
		bool found = false;   // Flag to indicate if the value was found
		
		while (index < numElems && !found)
		{
			if (lotteryOptions[index] == value)   // If the value is found
			{
				found = true;           // Set the flag
				position = index;       // Record the value's subscript
			}
			index++;                   // Go to the next element
		}
		return position;               // Return the position, or −1
}
// Binary search

int binarySearch(const int lotteryOptions[], int numElems, int lotteryNum);

	{
		int lotteryOptions[numElems] = {13579, 62483, 26791, 77777, 26792, 79422, 33445, 85647, 55555, 93121};
		int lotteryNum;

		//Get the input
		cout << "Please enter a five-digit number: " << endl;
		cin >> lotteryNum;

		lotteryNum = binarySearch(lotteryOptions, numElems, 10);

		if (lotteryNum == -1)
			cout << "Sorry, that is not a winning number. " << endl;
		else
		{
			cout << "Congratulations, the winning number was " << endl;
			cout << (lotteryNum + 1) << endl;
		}
		return 0;
	}

int binarySearch(const int lotteryOptions[], int numElems, int lotteryNum)
{
	int first = 0,
		last = numElems - 1,
		middle,
		position = -1;
	bool found = false;

	while (!found && first <= last)
	{
		middle = (first + last) / 2;
		if (lotteryOptions[middle] == lotteryNum)
		{
			found = true;
			position = middle;
		}
		else if (lotteryOptions[middle] > lotteryNum)
			last = middle = 1;
		else
			first = middle + 1;
	}
	return position;
}

Remove the semicolon at the end of line 50.
Remove the semicolon at the end of line 70.
Topic archived. No new replies allowed.