C++ Lottery program
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Thread: C++ Lottery program

  1. #1
    Join Date
    Nov 2011
    Posts
    3

    Unhappy 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;
    }

  2. #2
    Join Date
    Jan 2009
    Posts
    596

    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.

  3. #3
    Join Date
    Nov 2011
    Posts
    3

    Smile Re: C++ Lottery program

    Thank you for the speedy and very helpful response!

    It is running perfect now, cheers.

  4. #4
    Join Date
    Jan 2009
    Posts
    596

    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

  5. #5
    Join Date
    Nov 2011
    Posts
    3

    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 ?

  6. #6
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Fairfax, VA
    Posts
    10,891

    Re: C++ Lottery program

    Quote Originally Posted by london92 View Post
    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
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center