Confusing syntax error for me

Very new at this. I tried to figure out what the problem was here but there is an error specifically telling me that there is no "if" before the "else" and I am not sure why that is.

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
#include <iostream>
using namespace std;


int main()
{
	int month, day;
	char response;

	do 
	{
	cout << "Please enter the number representing the month you were born: \n";
	cin >> month;
	cout << "Now enter the day: \n";
	cin >> day;
	
	
	if ((month == 1) && ((day >= 20) && (day <= 31)))
		
		cout << "you are all good.\n";
		cout << "good day\n";
		
			{
			if ((day == 20)  || (day == 21))
			cout <<  "blah\n";
			cout << "blahblah\n";
			}
			
	else 
		cout << "Bad day.\n";
		

    cout << "Press Y to try again, or n and then return to exit: ";
    cin >> response; 
    cout << endl;
	 
	} while ((response == 'y') || (response == 'Y'));
return 0;	
}


If I can find out what went wrong here I think can finish the rest no problem.
Because there actually is no if before your else:
18
19
20
21
22
23
24
25
26
27
28
29
30
if ((month == 1) && ((day >= 20) && (day <= 31)))
    cout << "you are all good.\n";

cout << "good day\n"; // This is not part of the previous if statement
{ // These curly braces have essentially no effect here
    if ((day == 20)  || (day == 21))
        cout <<  "blah\n";
    
    cout << "blahblah\n"; // This is not part of the if statement above
}

else // No matching 'if' for this 'else'
    cout << "Bad day.\n";
(This is the same code you posted; I just reformatted it to show the errors.)
This should work...

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
#include <iostream>
using namespace std;


int main()
{
	int month, day;
	char response;

	do 
	{
	cout << "Please enter the number representing the month you were born: \n";
	cin >> month;
	cout << "Now enter the day: \n";
	cin >> day;
	
	{
	if ((month == 1) && ((day >= 20) && (day <= 31)))
	{
		cout << "you are all good.\n";
		cout << "good day\n";
	}
	if ((day == 20)  || (day == 21))
	{
		cout <<  "blah\n";
		cout << "blahblah\n";
	}
	else if (day>31) 
	{ 
		cout << "Bad day.\n";
	}	
	}
    cout << "Press Y to try again, or n and then return to exit: ";
    cin >> response; 
    cout << endl;
			}
while ((response == 'y') || (response == 'Y'));{
return 0;	
}
}
	
Last edited on
Thank you both, and thank you Stephanie for the more clear response. I figured it out finally and did something very similar to what was posted above and it worked. I will use your post if I run into another error.
your welcome
Topic archived. No new replies allowed.