Can someone help me with my code for pointers

my assignment is:
Lottery Winners
A lottery ticket buyer purchases ten tickets a week always playing the same ten five-digit "lucky" combinations. Write a program that initializes an array with these numbers and then lets the player enter this week's winning five digit number. the program should perform a linear search through the list of the player's numbers and report whether or not one of the ticket is a winner this week. here are the numbers;
13579 26791 26792 33445 55555
62483 77777 79422 85647 93121

Extend this challenge by permitting any number of lottery entries, and using a pointer instead of a fixed array. The general idea is this:

§ Declare your array of numbers as a pointer to int, rather than as a fixed array of int.

§ Have the first number entered by the user of your program be the number of entries. Let this number be N.

§ Allocate space for the array using new.

§ The remaining input should be the N lottery numbers.

§ Proceed with the rest of the program to carry out binary searches in your array. You need a loop that repeatedly asks for a number, then says whether its a “winner” or not. Enter a 0 to stop the loop and return from the program.

§ Release the array space before returning using delete.

so far, after 2-3 hours of trying i have this done but i dont know what else to add or if im even going the right way for this assignment. please help i am a newbie to c++.

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
//This program calculates whether or not a person's lottery numbers 
//are the winning numbers.
#include <iostream>
using namespace std;

//function prototypes

int binarySearch(int [], int, int);

const int SIZE=10;

int main()
{

	int *numbers;
	int N;
	int count;
	int results;

	//Get number of lottery tickets bought
	cout<<"How many lottery tickets were purchased?:";
	cin>> N;

	//allocate an array 
	numbers= new int[N];

	//Get lottery numbers for each ticket
	cout<<"\nEnter each lottery ticket's numbers"<<endl;
	for (count=0; count<N; count++)
	{
		cout<< "Lottery ticket #"<<(count+1)<<": ";
		cin>>numbers[count];
	}

	do
	{
	cout<<"Please enter a lottery ticket's number"<<endl;
	cin>>*numbers;
	
	
	while (numbers == numbers);
	
	
		cout<<"Winner!";
	
	while (numbers != numbers)
	
		cout<<"Loser!";
	}
	
	
	//Free allocated memory
	
	delet [] numbers;
	numbers = 0;
	return 0;
}


//*************************************************
// Binary Search

int binarySearch(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;
}
1
2
3
4
5
6
7
8
9
10
	do
	{
	cout<<"Please enter a lottery ticket's number"<<endl;
	cin>> result;
	binarySearch(numbers,N,result);
	if(position!=-1);
	    cout<<"Winner!";
                else	
	    cout<<"Loser!";
	}while(result)

I changed your code in do{} like this. And one more thing, you should not pass "int []" to a funtion, use "int *" instead, anytime you want pass a array to a funtion, you should use pointer.
ok, so i revised my source codes and got most of to work. what is trying to be done here is, i enter how many lotto tickets were bought, then i enter the winning lottory numbers, and then im supposed to enter my "own lottery ticket's number" and a message is supposed to tell me whether i am the winner or not. the dialog that asks me to enter my own "lottery ticket's number" is supposed to continously loop around with my numbers until i enter "0" which quits the program. The problem i have is...
if i enter 3 tickets bought....
it will then asks me to enter 3 winning numbers...
i then enter my own lottery ticket's numbers and it will only prompt me as a "winner" if i enter my own lottery ticket's number in the exact same order and spot i entered previously for the "winning numbers".
how do i get the program to print "winner" every time i enter a winning number as my own lottery ticket's number.


ok if it still sounds a little confusing, i'll give you the outputs i get on my screen.

How many lottery tickets were purchased?: 3

Enter winning lottery ticket's numbers...

Winning Number Lottery ticket#1: 4
Winning Number Lottery ticket#2: 5
Winning Number Lottery ticket#3: 6

Enter your lottery ticket number: 6
Loser!

Enter your lottery ticket number: 5
Winner!


Enter your lottery ticket number: 4
Loser!
_________________________________________
[and so forth......]

as you can tell, only the second number for my lottery ticket number showed up as a winner because "5" is pressed as the 2nd choice for each input. Please help me fix this so as long as my numbers match any of the winning numbers, it will print "Winner!" instead of having it need to be typed according to the same choice. kind of hard to explain but if you test out my codes, you will get the point. anyways, here is my revised codes....

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
//This program calculates whether or not a person's lottery numbers 
//are the winning numbers.
#include <iostream>
using namespace std;

//function prototypes

int binarySearch(int [], int, int);

const int SIZE=10;

int main()
{

	int *numbers;
	int N;
	int count;
	int results;
	
	
	//Get number of lottery tickets bought
	cout<<"How many lottery tickets were purchased?:";
	cin>> N;


	//allocate an array 
	numbers= new int[N];


	//Get lottery numbers for each ticket
	cout<<"\nEnter winning lottery ticket's numbers...\n"<<endl;
	for (count=0; count<N; count++)
	{
		cout<<"Winning Number Lottery ticket #"<<(count+1)<<": ";
		cin>>numbers[count];
	}

	int binarySearch();

	for (count=0; count<1000; count++)
	{
		cout<<"\nEnter your lottery number:";
		cin>>results;

		if (results == 0)
		{
			cout<<"Quitting Program\n";
			break;}

		{
		if (results == numbers[count])
		{
			cout<<"Winner!\n";
		}
		else
		
			cout<<"Loser!\n";
		}
		
	}
	
	//Free allocated memory
	delete [] numbers;
	numbers = 0;
	return 0;
}


//*************************************************
// Binary Search

int binarySearch(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;
}

I just want to mention...I was surprised the above compiled cleanly. Mainly looking at this:

1
2
3
4
5
6
7
8
9
	//Get lottery numbers for each ticket
	cout<<"\nEnter winning lottery ticket's numbers...\n"<<endl;
	for (count=0; count<N; count++)
	{
		cout<<"Winning Number Lottery ticket #"<<(count+1)<<": ";
		cin>>numbers[count];
	}

       int binarySearch();     //<<<<< 


Honestly, I've never seen anyone write like this before. So I don't know what is happening, but my best guess is that you are invoking the default constructor for an int.

For example, here's a copy constructor:
int IntA(5);
OR
int IntA = 5;

You've done something that compiles but doesn't function as it should since I cannot debug into it and see what is going on. But I know it doesn't function as you intended since I'm certain you wanted to call your binarySearch(...,...,...) function.

Since I don't want to guess your intentions here, are you trying to create a new variable or trying to call your binarySearch function with that call?

Next - I tried to clean up your code so its readable.

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
	//a quick glance through this loop and I see a problem:
	//the array size of 'numbers' is N (ref: numbers= new int[N];)
	for (count=0; count<1000; count++)
	{
		cout<<"\nEnter your lottery number:";
		cin>>results;

		if (results == 0)
		{
			cout<<"Quitting Program\n";
			break;
		}

		{

			//However here you're attempting to index numbers with count, which
			//the loop says ends at 1000.  With 'numbers' with the size of only N
			//This will cause Bad Things to happen since now the index is accessing 
			//memory out of the bounds of this array.

			//To Fix:  You should index an array up to the size of that array

			//Next: This section is a logical mistake.  Plz fix.
			if (results == numbers[count])
			{
				cout<<"Winner!\n";
			}
			else
				cout<<"Loser!\n";
		}
	}
honestly, i've been at this assignment for the last 2 days. i don't know any students in my class and my teacher is the type that doesnt tend to help much. if anyone can help me out by giving my more precise tips (since i'm new to C++) about how to fix things, it would be greatly appreciated. i've done well in my class up until now when i got lost in arrays and pointers. I'm trying to call on my binary search inputs that i put in for the winning numbers to let me know if any of my numbers match up with the winning numbers.
Last edited on
I suggest reading up on how arrays function before looking up how your binary search function works.


For now, comment out
int binarySearch();
Because it isn't essential to the function of the program.

Focus on the part that I mentioned that is the logical error. For example, if say N is 5, and our numbers is declared to be the size of N (so the size of numbers is 5) then it is illogical that we are able to go into numbers anything greater than 5.

This should start you off regarding your main loop.

1
2
3
4
5
6
7
8
9
10
11
12
13

//we keep iterating unless results == 0
while( results != 0 )
{
    cout<<"\nEnter your lottery number:";
    cin>>results;

    if(results != 0)
    {
       //main code
    }
}    

Topic archived. No new replies allowed.