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:

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
// 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 <iostream>
#include <cstdlib>
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:

1
2
3
4
5
6
7
    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.