I don't know what is wrong with my program :(

//I'm trying to write a program that uses linear search to see if an entered winning 5-digit lottery number matches the number on any of the player’s tickets(limit this number to 8 tickets).


#include <iostream>

using namespace std;

int main()
{
bool found=false;
long winticket=31419;
long ticket[8];
int index=0;

for (int i=0; i<8; i++)
{
cin>>ticket[i];
}

while (index<8 && !found)
{
if (ticket[index]==winticket)
{
found=true;
break;
}
index++;
if (found=true)
{
cout<<"Congratulations! You have the winning lottery ticket!";

else
cout<<"Sorry, you are not the holder of a winning ticket.";
}
You need to double check that while loop. Without proper indentation it's impossible to tell what you wrote.
What do you mean indentation? Is it really impossible to read?
Here's your code with indentation. As you can more easily see, your if (found=true) (which should be == BTW) are missing an }, and you main() is also missing an }.

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
#include <iostream>

using namespace std;

int main()
{
	bool found=false;
	long winticket=31419;
	long ticket[8];
	int index=0;

	for (int i=0; i<8; i++)
	{
		cin>>ticket[i];
	}

	while (index<8 && !found)
	{
		if (ticket[index]==winticket)
		{
			found=true;
			break;
		}
		index++;
		if (found=true)
		{
			cout<<"Congratulations! You have the winning lottery ticket!";

		else
			cout<<"Sorry, you are not the holder of a winning ticket.";
	}
Is it really impossible to read?
Yes.

It is now obvious why it's not working, right?

if (found is using assignment rather than comparison, and shouldn't be in the while loop at all.
Last edited on
Topic archived. No new replies allowed.