"Illegal Else Without Matching If" - Error

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
#include <iostream>

using namespace std;

int main()
{
calc:
	system ("cls");
	char ans;
	int length;
	int width;
	int area;
	int perimeter;
	cout << "Input The Length Of The Rectangle: " << endl;
	cin>> length;
	cout << "Input The Width Of The Rectangle: " << endl;
	cin>> width;
	perimeter = 2 * (length + width);
	area = length * width;
	cout << "The Perimeter Of The Rectangle Is: "<< perimeter << endl;
	cout << "The Area Of The Rectangle Is: "<< area << endl;
	system("PAUSE>NUL");
choice:
	system("cls");
	cout << "What Do You Want To Do?" << endl;
	cout << "(A) Calculate Another Rectangle's Area And Perimeter." << endl;
	cout << "(B) Quit The Program." << endl;
	cin>> ans;
	if(ans == 'A' || ans == 'a');
	{
		goto calc;
	}
	else if(ans == 'B' || ans == 'b');
	{
		exit (1);
	}
	else
	{
		cout << "Illegal Choice You DumbArse" << endl;
		system("Pause>nul");
		goto choice;
	}
}


This is my code. Its Job Is To Calculate A Rectangle's Area And Perimeter. In The End I Have Added 2 Choices. A and B. A Is to do it again. B is to exit. I get these errors while compiling:

warning C4390: ';' : empty controlled statement found; is this the intent? error C2181: illegal else without matching if
warning C4390: ';' : empty controlled statement found; is this the intent?
error C2181: illegal else without matching if

If It Helps I Am Usin Windows 7 and using visual c++ 2008 express. I know i should search the forums for such errors before posting, but ihave and i did not find anything like this. if there is such a topic i would be glad if you send me a link. And If you dont mind, explain the warnings to me. Thanks!
remove the ';' at the end of line 33
Right, instead of using goto's use functions. Forget goto even exists !!!

You also have semicolons after your if conditions remove them. That is what the warnings are about, and the cause of the error.

And get rid of the system calls, there is an article on this site about why not to do that, and the proper way.

Sorry for sounding harsh, have a go at fixing it up, - happy to help with any further questions :)

Edit: Also consider using while loops
Last edited on
Thanks Alot Script Coder And TheIdeasMan, And I Will Consider While Loops!
Its Just That Im trying to make the code smaller , because when i actually finish thewhole program, its gonnabe for all shapes, at the user's choice, and there will be 3 choices, exit, again, again with dif shape ;D Anyways thanks alot!!
Sounds like you need to implement a menu.

Check this out, a skeleton program i did to help someone else.

http://www.cplusplus.com/forum/beginner/76482/4/#msg412946


It use classes, (you don't have to do that if you don't want to), but the main() shows the ShowMenu & ProcessMenu functions. You could extend it with functions for each menu option.

Have a go, see how you get on.

Its Just That Im trying to make the code smaller


If you use functions properly, it will make your code more concise.

Also consider using local variables in your functions.

We look forward to seeing your new code. :D
TheIdeasMan, If you could make what u just said a bit easier, because i just started learning c++, and barely know anything :P Thanks For the help btw!
Can you figure out what is going on in this code? I have removed some bits that aren't relevant to you.

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
78
79
#include <iostream>

using std::cout;	//this is much better then using namespace std
using std::cin;		//I prefer to do this rather than std:: everywhere
using std::endl;


void ShowMenu();
bool ProcessMenu(const unsigned int MenuOpt, CBank* pCBank, bool
				QuitMenu);

int main() {
	
	unsigned MenuOpt = 0; //The selected Menu Option
	bool QuitMenu = false;
	
	
	while(!QuitMenu) {
		ShowMenu();
		cin >> MenuOpt;	//haven't done any error checking
		QuitMenu = ProcessMenu(MenuOpt, pCBank, QuitMenu);
	}
    
    return 0;
}


void ShowMenu() {
	cout << "Welcome to My Banking System\n\n" ;
	
	cout << "(1) Add account\n";
	cout << "(2) Delete Account\n";
	cout << "(3) Account Inquiry\n";
	cout << "(4) Save Accounts to File\n";
	cout << "(5) Load Accounts from File\n";
	cout << "(6) Exit\n\n";
	
	cout << "Please Enter your Selection" << endl;
}

bool ProcessMenu(const unsigned int MenuOpt, CBank* pCBank, bool QuitMenu)
{
	switch (MenuOpt)
	{
		case 1:
			//cout << "(1) Add account Option not implemented Yet\n";
			pCBank->NewAcct();
			
		break;
		
		case 2:
			cout << "(2) Delete Account Option not implemented Yet\n";
		break;
		
		case 3:
			cout << "(3) Account Inquiry Option not implemented Yet\n";
		break;
		
		case 4:
			cout << "(4) Save Accounts to File Option not implemented Yet\n";
		break;
		
		case 5: 
			cout << "(5) Load Accounts from File Option not implemented Yet\n";
		break;
		
		case 6:
			cout << "Exiting Program\n";
			QuitMenu = true;
			return QuitMenu;
			break;
			
		default:
			cout << "Invalid selection. Please Choose another option\n";
		break;
		
		
	}
}
Not At All :/
Do you understand the ShowMenu function?
Nop :(
Is this homework?

If it is, then you must have learnt what a function is.

If not , what learning have you done so far? Have you read any books, looked on the net for anything?

If you haven't done any of these , you need to do something easier, and do more research.
The you can check this page for more info about switch cases
http://www.cplusplus.com/doc/tutorial/control/
By the way the function

1
2
3
4
5
6
7
8
9
10
11
12
void ShowMenu() {
	cout << "Welcome to My Banking System\n\n" ;
	
	cout << "(1) Add account\n";
	cout << "(2) Delete Account\n";
	cout << "(3) Account Inquiry\n";
	cout << "(4) Save Accounts to File\n";
	cout << "(5) Load Accounts from File\n";
	cout << "(6) Exit\n\n";
	
	cout << "Please Enter your Selection" << endl;
}


can be rewritten as

1
2
3
4
5
6
7
8
9
10
11
12
13
inline void ShowMenu( std::ostream &os = std::cout ) 
{
	const char *menu =  "Welcome to My Banking System\n\n"
                            "(1) Add account\n"
                            "(2) Delete Account\n"
                            "(3) Account Inquiry\n"
                            "(4) Save Accounts to File\n"
                            "(5) Load Accounts from File\n"
                            "(6) Exit\n\n"
                            "Please Enter your Selection";

	os << menu << std::endl;
}
Last edited on
@ScriptKiddie

I saw your PM's, I would rather not communicate this way unless there is a good reason.

I saw the code you posted, it is the same as before. You haven't answered the questions I asked - I would like to see something more substantial from you.

I am beginning to wonder if you are trolling.

If not, show us something to prove otherwise.

Sorry if I sound harsh, but I am not going to sit here all night, putting effort in - and not seeing any results.

I am pleased to help, but I need to see something from you.
On Line 33,There Should Be No Space!(:It is Supposed to be elseif!(:Nice Code Also!
@Techy24
On Line 33,There Should Be No Space!(:It is Supposed to be elseif!(:Nice Code Also!


In C/C++ there is no such statement as elseif


@Techy24
Actually You Are Wrong, Its Is 'Else If' not 'ElseIf'

@TheIdeasMan
I am beginning to wonder if you are trolling.


I Am Sorry That You Think That Way, But I Asure You, I Am Not Trolling At All.

@TheIdeasMan
Sorry if I sound harsh, but I am not going to sit here all night, putting effort in -and not seeing any results.

I am pleased to help, but I need to see something from you.


You Do Not Sound Harsh At All, Because You Have A Huge Point Here, And I Agree With You,But Take It Easy On Me, Because I Do Not Open The Internet Often, AND I am Just 11 years old, and this may be a bit hard for me to understand, but my passion to learning programming is how i survive :)

And Btw Thanks All For Your Replies!! :D
Topic archived. No new replies allowed.