cin ambigous error

getting a cin ambiguous error and else "expected a statement error"
everything was working until I tried adding a if statement to close the program if the answer was 'N'.



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

#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;

int main()
{
	int randNumber;
	int userNumber;
	int answer;
	bool win = false;
	unsigned seed = time(0);

		srand(seed);
		randNumber = 0 + rand() % 501;

		cout << "I'm thinking of a number between 0 and 501. Do you want to guess what it is? (Y/N)" <<endl;
		cin >> userNumber;
		cin >> answer;
		
		{
			if (answer == 'Y')

		
			else (answer == 'N')
			{
				break;
			}
			
			while (!win)
			{
				if (userNumber < randNumber)
				{
					cout << "Sorry, bud. You're number was out of range. You guessed too low, try again." << endl;
					cin >> userNumber;
				}
				else if (userNumber > randNumber)
				{
					cout << "Sorry, bud. You're number was out of range. You guess too high, try again." << endl;
					cin >> userNumber;
					cin.ignore();
				}
				else //winner
				{
					cout << "Yup, that was my number! We're done, you can leave now." << endl;
					win = true;
				}
			}



		
		}
		
cin.get();
return 0;
}


		











	cin.get();
	return 0;
}

Line 21 is empty, but I assume there should be something there considering the brackets on line 22 and 54.

Line 23, your if statement doesn't have "{}" anywhere. I assume the newly added code is the else statement on line 26?

The issue here is that if you leave the if statement without "{}", it'll simply assume the very next line with code is what you want to happen if it's found true. So lets follow the logic here:

If answer is 'Y', then execute else statement? That doesn't make sense and not what you wanted. In fact, there's no reason to even check if answer is 'Y', just assume it is if the answer wasn't 'N' and you can remove line 23 all together and turn the else statement into an if statement. (because you're not doing anything after checking if it was 'Y'). If you want to insist on checking, use "{}" to signify that you just want it to check and not actually do anything.
That fixed it, thank you so much!
Topic archived. No new replies allowed.