[Error] 'else' without a previous 'if'

It says that the else on line 53 isn't connected to an if. I am not sure what exactly it wants me to do here.

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
#include <iostream>
using namespace std;
int main()
{
	double FirstNum, SecondNum, Output;
	int FirstNumInt, SecondNumInt, OutputInt;
	char oper;
	char cont = 0;
	
	while (cont == 0);
	{
		cout<<"Enter Two Numbers!"<<endl;
		cin>>FirstNum;
		cin>>SecondNum;
		FirstNumInt = FirstNum;
		SecondNumInt = SecondNum;
	
		cout<<"Enter a mathmatical operator! Valid options are +, -, *, /, %, <, and >"<<endl;
		cin>>oper;

		if (oper == '+')
		{
			Output = FirstNum + SecondNum;
			cout<<Output<<endl;
		}
		else if (oper == '-')
		{
			Output = FirstNum - SecondNum;
			cout<<Output<<endl;
		}
		else if (oper == '*')
		{
			Output = FirstNum * SecondNum;
			cout<<Output<<endl;
		}
		else if (oper == '/')
		{
			Output = FirstNum / SecondNum;
			cout<<Output<<endl;
		}
		else if (oper == '%')
		{
			OutputInt = FirstNumInt % SecondNumInt;
			cout<<OutputInt<<endl;
		}
		else if (oper == '<')
		{
			if (FirstNum < SecondNum);
				{cout<<"True"<<endl;}
			}else;
				{cout<<"False"<<endl;}
		}
		else if (oper == '>')
		{
			if (FirstNum > SecondNum);
				{cout<<"True"<<endl;}
			}else;
				{cout<<"False"<<endl;}
		}
		else if (oper != 'N')
		{
		
			cout<<"That is not a valid operator"<<endl;
		}
		else if (oper == 'N')
		{
			cont = 1;
		}
	}

	system("PAUSE");
    return 0;
}
You have some braces out of place, try this...

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
#include <iostream>
using namespace std;
int main()
{
	double FirstNum, SecondNum, Output;
	int FirstNumInt, SecondNumInt, OutputInt;
	char oper;
	char cont = 0;
	
	while (cont == 0)
	{
		cout<<"Enter Two Numbers!"<<endl;
		cin>>FirstNum;
		cin>>SecondNum;
		FirstNumInt = FirstNum;
		SecondNumInt = SecondNum;
	
		cout<<"Enter a mathmatical operator! Valid options are +, -, *, /, %, <, and >"<<endl;
		cin>>oper;

		if (oper == '+')
		{
			Output = FirstNum + SecondNum;
			cout<<Output<<endl;
		}
		else if (oper == '-')
		{
			Output = FirstNum - SecondNum;
			cout<<Output<<endl;
		}
		else if (oper == '*')
		{
			Output = FirstNum * SecondNum;
			cout<<Output<<endl;
		}
		else if (oper == '/')
		{
			Output = FirstNum / SecondNum;
			cout<<Output<<endl;
		}
		else if (oper == '%')
		{
			OutputInt = FirstNumInt % SecondNumInt;
			cout<<OutputInt<<endl;
		}
		else if (oper == '<')
		{
			if (FirstNum < SecondNum)
				{cout<<"True"<<endl;}
			else
				{cout<<"False"<<endl;}
		}
		else if (oper == '>')
		{
			if (FirstNum > SecondNum)
				{cout<<"True"<<endl;}
			else
				{cout<<"False"<<endl;}
		}
		else if (oper != 'N')
		{
		
			cout<<"That is not a valid operator"<<endl;
		}
		else if (oper == 'N')
		{
			cont = 1;
		}
	}

	system("PAUSE");
    return 0;
} 
Probably copy/paste errors.

You have mismatching {braces}

This is the code you have:
1
2
3
4
5
6
7
	else if (oper == '<')
	{
		if (FirstNum < SecondNum);
			{cout<<"True"<<endl;}
		}else;
			{cout<<"False"<<endl;}
	}


Fixing the indentation to match the braces:
1
2
3
4
5
6
7
8
	else if (oper == '<')
	{
		if (FirstNum < SecondNum);  // <- PS, this semicolon is wrong
		{cout<<"True"<<endl;}
	}
	else;  // <- PS, this semicolon is wrong
	{cout<<"False"<<endl;}
}



notice the extra } before the else is screwing you up.

Also... don't put semicolons after else (or if) statements.



OPINION EDIT:

This is one reason why I don't like to put other things on the same line as open or close braces. It makes mistakes like this harder to spot.
Last edited on
Thank you! My only problem now is that it says system was not declared in this scope. More brace problems?
Ah, nevermind that second question, I figured it out. Thank you very much for your help.
Well for starters you shouldn't be using system (see http://www.cplusplus.com/forum/articles/11153/ )

But if you really want to, you need to include the header that defines it. I think it's in <cstdlib>
Well, now it will compile, it just doesn't do anything.
if it does nothing that must mean oper is failing to get a value.

before your if statement add this to see what oper is...

cout << oper << endl;
Still isn't working. It doesn't say anything at all.
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
#include <iostream>
using namespace std;
int main()
{
    double FirstNum, SecondNum, Output;
	int FirstNumInt, SecondNumInt, OutputInt;
	char oper;
	char cont = 0;

    cout <<"Enter two numbers"<<endl;
    cin>>FirstNum;
    cin>>SecondNum;
    FirstNumInt = FirstNum;
    SecondNumInt = SecondNum;

    cout<<"Enter a mathmatical operator such as +, -, *, /, %, <, and >"<< endl;
    cin>>oper;

    if (oper == '+')
    {
			Output = FirstNum + SecondNum;
			cout<<Output<<endl;
    }
    if (oper == '-')
    {
			Output = FirstNum - SecondNum;
			cout<<Output<<endl;
    }
    if (oper == '*')
    {
			Output = FirstNum * SecondNum;
			cout<<Output<<endl;
    }
    if (oper == '/')
    {
			Output = FirstNum / SecondNum;
			cout<<Output<<endl;
    }
    if (oper == '%')
    {
			OutputInt = FirstNumInt % SecondNumInt;
			cout<<OutputInt<<endl;
    }
    if (oper == '<')
    {
	if (FirstNum < SecondNum)
    {
            cout<<"True"<<endl;
    }
    else
    {
            cout<<"False"<<endl;
    }

    if (oper == '>')
    {
    if (FirstNum > SecondNum)
    {
            cout<<"True"<<endl;
    }}
    else{
            cout<<"False"<<endl;
		}

}}


Try something like that(removing if else and just using if and else).
This runs fine, however, I am sure it's not exactly what you need. Try tweaking this code into what you need.
Last edited on
All right, I am almost at the finish line. I hope you will all forgive me for my ignorance, this is the first real C++ program I have ever designed. I have this 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
#include <iostream>
#include <cstdlib>
using namespace std;
int main()
{
	double FirstNum, SecondNum, Output;
	int FirstNumInt, SecondNumInt, OutputInt;
	char oper;
	char cont = 0;
	
	while (cont == 0)
	{
		cout<<"Enter Two Numbers!"<<endl;
		cin>>FirstNum;
		cin>>SecondNum;
		FirstNumInt = FirstNum;
		SecondNumInt = SecondNum;
	
		cout<<"Enter a mathmatical operator! Valid options are +, -, *, /, %, <, and >"<<endl;
		cin>>oper;

		if (oper == '+')
		{
			Output = FirstNum + SecondNum;
			cout<<Output<<endl;
		}
		else if (oper == '-')
		{
			Output = FirstNum - SecondNum;
			cout<<Output<<endl;
		}
		else if (oper == '*')
		{
			Output = FirstNum * SecondNum;
			cout<<Output<<endl;
		}
		else if (oper == '/')
		{
			Output = FirstNum / SecondNum;
			cout<<Output<<endl;
		}
		else if (oper == '%')
		{
			OutputInt = FirstNumInt % SecondNumInt;
			cout<<OutputInt<<endl;
		}
		else if (oper == '<')
		{
			if (FirstNum < SecondNum)
				{cout<<"True"<<endl;}
			else
				{cout<<"False"<<endl;}
		}
		else if (oper == '>')
		{
			if (FirstNum > SecondNum)
				{cout<<"True"<<endl;}
			else
				{cout<<"False"<<endl;}
		}
		else if (oper != 'N')
		{
		
			cout<<"That is not a valid operator"<<endl;
		}
		else if (oper == 'N')
		{
			cont = 1;
		}
	}

	system("PAUSE");
    return 0;
}


And that code works, except if I enter N to terminate it it goes into an infinite loop. Other than that I think it works.
Wait, never mind, that actually does work! I was entering N as a number. When entered as an operator it works. Not the most robust program I admit, but it works! Thank you all for your help on this.
Topic archived. No new replies allowed.