### Linear Search for Lottery Ticket

I know this is a popular problem but I need help stopping my program from exiting when 'y' or 'Y' is entered. The problem asks to provide a winning lottery ticket for each week. There is 10 weeks in total.

Here is my code:

 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798`` ``````// Prog Challeg 8.2 Lottery Winners.cpp : Defines the entry point for the console application. // #include "stdafx.h" int _tmain(int argc, _TCHAR* argv[]) { return 0; } //Lottery Winners #include #include using namespace std; const int LUCKY_NUMS =10; // Function prototype that searches winning ticket number int ticketSearch(const int [], int, int); int main () { //user continues playing lotto char again; const char QUIT = 'N'; //determines if player's ticket is a winner int winningNum; //5 digit ticket number entered by player int playerNum; //holds winning ticket number int ticket; //Array holding the winning tickets for each week int lottoTix[LUCKY_NUMS] = {13579, 26791, 26792, 33445, 55555, 62483, 77777, 79422, 85647, 93121}; //Player decides if they want to continue playing for (int week = 0; week < 10; week++) { //Winning lotto ticket for each week (10 weeks total) ticket = lottoTix[week]; cout << "Please enter your 5-digit ticket number for week " << (week + 1) << ": " << endl; //Player's ticket number cin >> playerNum; //Calls linear search for winning lotto ticket winningNum = ticketSearch(lottoTix, LUCKY_NUMS, playerNum); //Error message if player's number is not the winning ticket if ((winningNum == -1) || playerNum != ticket) { cout << "Sorry, you did not win the MEGAMILLIONS lottery. "; cout << "Thanks for playing! "; cout << "Play again? (Y/N)"; cin >> again; } //Player wins the lottery else if (playerNum == ticket) { cout << "You have just won 598 MILLION DOLLARS!!! "; cout << "CONGRATULATIONS!!!"; cout << "Play again? (Y/N)"; cin >> again; } if ((again != 'Y') || (again != 'y')) { //exit message cout << "Press [Enter] to exit...\n\n"; //exits program exit(0); } } return 0; } //linear search for winning ticket of the week int ticketSearch(const int ticketList[], int numTickets, int winningNum) { int index = 0; int position = -1; bool found = false; while ((index < numTickets) && !found) { if (ticketList[index] == winningNum) { found = true; position = index; } index ++; } return position; }``````
The main problem is the use of the "or" operator `||`.
`if ((again != 'Y') || (again != 'y'))`
Let's say the user enters 'Y'.
The first condition is `(again != 'Y')` which gives a result of `false`

The second condition is `(again != 'y')` which results in `true`

No matter what the user enters, one or both of these will always be true, so the program always exits.
Change the operator to "and" `&&` when using the not-equal operator:
`if ((again != 'Y') && (again != 'y'))`

Also, the message says the user needs to press enter to exit. You can do that like this:

 ``1234567`` `````` if ((again != 'Y') && (again != 'y')) { cout << "Press [Enter] to exit...\n\n"; // exit message cin.ignore(1000,'\n'); // remove any chars from input buffer cin.get(); // wait for user to press enter exit(0); // exits program }``````
Hey thanks for the help Chervil. I cannot believe it was something as small as the "&&" symbol verses "||". Countless hours could have been saved lol! I appreciate the quick response too. I left the cin.ignore () function without parameters and it appears to works ok. Alright now on to binary search, woohoo!

Thanks again!
Topic archived. No new replies allowed.