-
November 30th, 2011, 07:58 PM
#1
C++ Lottery program
Something is going wrong in my program, it will run, but when I put in any number or character in for an answer, it says it is a correct lottery number.
My program:
#include <iostream>
#include <string>
using namespace std;
int LinearSearch (int [], int, int);
int main()
{
const int Lottery = 10;
int nums [Lottery] = {13579, 26791, 26792, 33445, 55555, 62483, 77777, 79422, 85647, 93121 };
int UserSelection, location;
double USER;
cout << "Type in the lottery number you have purchased now:\n\n ";
cin >> UserSelection;
location = LinearSearch(nums, Lottery, UserSelection);
if ( location >-1 )
cout << "The number you have entered is a direct match!!\n\n YOU HAVE WON!!!\n\n" << location << endl;
else
cout << "We are sorry but the number you have entered was not found, please play again!\n\n ";
cin >> USER;
return 0;
}
int LinearSearch (int list[], int size, int key)
{
int x;
for (x=0; x < size; x++);
{
if (list [x] == key);
return 0;
}
return -1;
}
-
November 30th, 2011, 08:14 PM
#2
Re: C++ Lottery program
You need to be more careful with where you put your semicolons.
For example in this code:
Code:
for (x=0; x < size; x++);
{
if (list [x] == key);
return 0;
}
The semicolons after the for and if statements end those statements. So what happens is this:
1) The 'for' loop runs, but does nothing except increment x
2) We go into the curly brackets, and the 'if' statements runs, but does nothing
3) We reach the 'return 0;' line, which returns 0
So the search function will always return 0 whether or not the lottery number is found.
As an aside, if you had run your code through a debugger you would have spotted this straight away.
-
November 30th, 2011, 08:21 PM
#3
Re: C++ Lottery program
Thank you for the speedy and very helpful response!
It is running perfect now, cheers.
-
November 30th, 2011, 08:37 PM
#4
Re: C++ Lottery program
Glad to be of service
Incidentally, a safer way to make the array is like this:
Code:
int nums [] = {13579, 26791, 26792, 33445, 55555, 62483, 77777, 79422, 85647, 93121 };
const int Lottery = sizeof(nums)/sizeof(int);
This will automatically make sure that 'Lottery' has the correct value, even if you add or remove values from the array.
Of course, using std::vector would be even better
-
November 30th, 2011, 09:27 PM
#5
Re: C++ Lottery program
Just made the change, thanks!
Any ideas on how to change this as easily as possible from a linear search to a binary ?
-
December 1st, 2011, 12:18 PM
#6
Re: C++ Lottery program
Originally Posted by london92
Just made the change, thanks!
Any ideas on how to change this as easily as possible from a linear search to a binary ?
The standard library contains std::binary_search (if you just want to check existence) or std::lower_bound (if you want to actually get a handle to the element if it exists).
Whether or not you are allowed to use these in your class is a different matter.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|