need hlp with C++ code

Please tell me why do i get this error saying "error C2181: illegal else without matching if". The last else statement doesn't work. There is a red line under the else. Can some plz tell me the solution. I hv been trying since hours. Please do not worry about variables.


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

int main()
{
	int a = 10;
	double b;
	int c;

	cout << "Enter" << endl;
	cin >> c;
	{
		if (c >= 1 && c < 20)
			b = c * 0.10 + a;
		else if (c >= 20 && c <= 39)
			b = c * 0.08 + a;
		else if (c >= 40 && c <= 59)
			b = c * 0.06 + a;
		else if (c >= 60)
			b = c * 0.04 + a;
		cout << "It is" << b << endl;
	}	
	    else 
		cout << "It is not." << endl;
	

		cin.get();
		cin.ignore();
		return 0;
}
Last edited on
I see no if between lines 12 and 13...

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

int main()
{
	int a = 10;
	double b;
	int c;

	cout << "Enter" << endl;
	cin >> c;
	
	if (c >= 1 && c < 20)
			b = c * 0.10 + a;
	else if (c >= 20 && c <= 39)
			b = c * 0.08 + a;
	else if (c >= 40 && c <= 59)
			b = c * 0.06 + a;
	else if (c >= 60)
			b = c * 0.04 + a;
		
	
	    else 
    {
        cout << "It is not." << endl;
        cin.get();
        cin.ignore();
        return 0;
        }
	cout << "It is" << b << endl;

		cin.get();
		cin.ignore();
		return 0;
}


There is probably a better solution but this should work too.
because use used {} to contain your if elses, it read as a block. now you have entered the else right after line 23, now according to the compiler you put an "else" with out an "if". what you should do is remove the {}.
anyone else..??
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
#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
    int a = 10;
    double b;
    int c;

    cout << "Enter" << endl;
    cin >> c;
    
        if (c >= 1 && c < 20)
        {
            b = c * 0.10 + a;
        if (c >= 20 && c <= 39)
            b = c * 0.08 + a;
        else if (c >= 40 && c <= 59)
            b = c * 0.06 + a;
        else if (c >= 60)
            b = c * 0.04 + a;
        cout << "It is" << b << endl;
    }    
        else 
        {
        cout << "It is not." << endl;
        }

        cin.get();
        cin.ignore();
        return 0;
}
that doesn't solve my problem..anyone else no one can answer y is it so hard for ppl im just a beginner and i know ppl r experts here..still
I'm not exactly sure what the purpose of your program is, but I do know two things.
1. You have worthless brackets on lines 13 and 23.
2. Indivilual comparisons need to be enclosed in parenthesis like this if ((c >= 1) && (c < 20))
2. Indivilual comparisons need to be enclosed in parenthesis


Not really, but it does improve readability.
I might be getting this rule mixed up with Visual Basic. I'm pretty sure it's required there. Thanks for the clarification though.
Topic archived. No new replies allowed.