ERROR ELSE

Upon asking recommendee, the R works and N works. But I want to add an else so that pressing buttons other than R/r & N/n will say wrong input code. But it tells me this (error: else without previous if)
Ive been adding this after cout<<"Rejected"<<endl;
1
2
else 
cout<<"Wrong input"<<endl;

Also another problem when pressing R/r works fine telling Accepted but it doesnt end the program.

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
 #include<iostream>
using namespace std;
main()
{float h,a;
char cc,rc;

cout<<"Are you a recommendee of Jedi Master Obi Wan? Press R/r if yes and N/n if no."<<endl;
cin>>rc;
if (rc=='R'||rc=='r')
cout<<"Accepted"<<endl;
else if (rc=='N'||rc=='n')
	cout<<"Enter your height: ";
	cin>>h;
	cout<<"Enter your age: ";
	cin>>a;
	cout<<"Enter citizenship code, Press C/c if your Endor and N/n if not: ";
	cin>>cc;
		if (h>=200)
			if (a>=21 && a<=25) 
				if (cc=='C'||cc=='c')
					cout<<"Accepted."<<endl;
		else 
		cout<<"Rejected"<<endl;
system("PAUSE");
}
line 3: main() must be type int.

But it tells me this (error: else without previous if)

I get no such error when I compile what you posted.

lines 12-23: Only line 12 is conditional upon the else if on line 11. This isn't Python. Indentation has no bearing on execution in C++. If you want more than one statement to be conditional, use { }.

Line 25: main() should do a return 0;

Last edited on
To elaborate on what AbstractionAnon wrote, the code you've written is equivalent to:

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
#include<iostream>
using namespace std;
main()
{
    float h,a;
    char cc,rc;

    cout<<"Are you a recommendee of Jedi Master Obi Wan? Press R/r if yes and N/n if no."<<endl;
    cin>>rc;
    if (rc=='R'||rc=='r')
    {
        cout<<"Accepted"<<endl;
    }
    else if (rc=='N'||rc=='n')
    {
        cout<<"Enter your height: ";
    }
    cin>>h;
    cout<<"Enter your age: ";
    cin>>a;
    cout<<"Enter citizenship code, Press C/c if your Endor and N/n if not: ";
    cin>>cc;
    if (h>=200)
    {
        if (a>=21 && a<=25)
        {
            if (cc=='C'||cc=='c')
            {
                cout<<"Accepted."<<endl;
            }
            else 
            {
                cout<<"Rejected"<<endl;	
            }
        }
    }
    system("PAUSE");
}


And if you make the change you described, you get:

27
28
29
30
31
32
33
34
35
36
37
38
            if (cc=='C'||cc=='c')
            {
                cout<<"Accepted."<<endl;
            }
            else 
            {
                cout<<"Rejected"<<endl;
            }
            else 
            {
                cout<<"Wrong input"<<endl;
            }


I trust you can see now why you're getting the errors you're getting?
Last edited on
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
#include<iostream>
using namespace std;
main()
{float h,a;
char cc,rc;

cout<<"Are you a recommendee of Jedi Master Obi Wan? Press R/r if yes and N/n if no."<<endl;
cin>>rc;
if (rc=='R'||rc=='r')
cout<<"Accepted"<<endl;
else if (rc=='N'||rc=='n')
	{cout<<"Enter your height: ";
	cin>>h;
	cout<<"Enter your age: ";
	cin>>a;
	cout<<"Enter citizenship code, Press C/c if your Endor and N/n if not: ";
	cin>>cc;
		if ((h>=200) && (a>=21 && a<=25) &&(cc=='C'||cc=='c'))
		cout<<"Accepted."<<endl;
		else 
		cout<<"Rejected"<<endl;}
else
cout<<"Invalid recommendee code!"<<endl;
system("PAUSE");
}

Thanks for the help. I just added {} so that the else im talking about would trigger continue on the else if right? also its working without int main() and return 0;
Thanks for the help. I just added {} so that the else im talking about would trigger continue on the else if right?

I think so, but it would be a hell of a lot easier for both you and me to see for sure, if you adopted a proper indentation style.

also its working without int main() and return 0;

Your compiler might allow it, but it's not legal C++.
Topic archived. No new replies allowed.