GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 30-Sep-2005, 20:32
tylerfelix tylerfelix is offline
New Member
 
Join Date: Sep 2005
Posts: 28
tylerfelix is on a distinguished road

Illegal else without matching if


CPP / C++ / C Code:
//Filename: Winner.cpp
//Purpose: To display the winner out of three teams
//Author: Tyler Felix, A00261464
//Date: 2005.09.30

#include <iostream>

using namespace std;


int main()

{

cout << "#################################" << endl;
cout << "# CSC226 Section B - 2005" << endl;
cout << "# ASSIGNMENT 3" << endl;
cout << "# Tyler Felix - Id #A00261464" << endl;
cout << "#################################" << endl;

//declares score variables
int superTeam;
int ultraTeam;
int megaTeam;
//get score value for the three teams
cout << "Please enter the amount of games won by the Super Team: ";
cin >> superTeam; 
cout << "" << endl;

cout << "Please enter the amount of games won by the Ultra Team: ";
cin >> ultraTeam;
cout << "" << endl;

cout << "Please enter the amount of games won by the Mega Team: ";
cin >> megaTeam;
cout << "" << endl;
cout << "" << endl;

//make Mega Team > Super Team > Ultra Team
		if ((superTeam > ultraTeam) && (superTeam < megaTeam))

	{	
		cout << "Congratulations to Mega Team for being first place!" << endl;
		cout << "" << endl;
		cout << "Summary: " << endl;
		cout << "First Place: Mega Team" << endl;
		cout << "Second Place: Super Team" << endl;
		cout << "Third Place: Ultra Team" << endl;
	}

//make Mega Team > Ultra Team > Super Team
		else ((ultraTeam > superTeam) && (ultraTeam < megaTeam)){
	{
		cout << "Congratulations to Mega Team for being first place!" << endl;
		cout << "" << endl;
		cout << "Summary: " << endl;
		cout << "First Place: Mega Team" << endl;
		cout << "Second Place: Ultra Team" << endl;
		cout << "Third Place: Super Team" << endl;
	}

		
//make Super Team > Ultra Team > Mega Team
		else ((ultraTeam > megaTeam) && (ultraTeam < superTeam))
	{
		cout << "Congratulations to Super Team for being first place!" << endl;
		cout << "" << endl;
		cout << "Summary: " << endl;
		cout << "First Place: Super Team" << endl;
		cout << "Second Place: Ultra Team" << endl;
		cout << "Third Place: Mega Team" << endl;
	}


//make Super Team > Mega Team > Ultra Team
		else ((megaTeam > ultraTeam) && (megaTeam < superTeam))
	{
		cout << "Congratulations to Super Team for being first place!" << endl;
		cout << "" << endl;
		cout << "Summary: " << endl;
		cout << "First Place: Super Team" << endl;
		cout << "Second Place: Mega Team" << endl;
		cout << "Third Place: Ultra Team" << endl;
	}	
			

//make Ultra Team > Mega Team > Super Team
		else ((megaTeam > superTeam) && (megaTeam < ultraTeam))
	{
		cout << "Congratulations to Ultra Team for being first place!" << endl;
		cout << "" << endl;
		cout << "Summary: " << endl;
		cout << "First Place: Ultra Team" << endl;
		cout << "Second Place: Mega Team" << endl;
		cout << "Third Place: Super Team" << endl;
	}
	
	
//make Ultra Team > Super Team > Mega Team
		else (( superTeam > megaTeam) && (superTeam < ultraTeam));
	{
		cout << "Congratulations to Ultra Team for being first place!" << endl;
		cout << "" << endl;
		cout << "Summary: " << endl;
		cout << "First Place: Ultra Team" << endl;
		cout << "Second Place: Super Team" << endl;
		cout << "Third Place: Mega Team" << endl;
	
	}
		

	return 0;
}




^^ my code.
Code:
D:\Program Files\Microsoft Visual Studio\MyProjects\proj3\proj3.cpp(52) : error C2181: illegal else without matching if D:\Program Files\Microsoft Visual Studio\MyProjects\proj3\proj3.cpp(52) : error C2143: syntax error : missing ';' before '{' D:\Program Files\Microsoft Visual Studio\MyProjects\proj3\proj3.cpp(64) : error C2181: illegal else without matching if D:\Program Files\Microsoft Visual Studio\MyProjects\proj3\proj3.cpp(65) : error C2143: syntax error : missing ';' before '{' D:\Program Files\Microsoft Visual Studio\MyProjects\proj3\proj3.cpp(76) : error C2181: illegal else without matching if D:\Program Files\Microsoft Visual Studio\MyProjects\proj3\proj3.cpp(77) : error C2143: syntax error : missing ';' before '{' D:\Program Files\Microsoft Visual Studio\MyProjects\proj3\proj3.cpp(88) : error C2181: illegal else without matching if D:\Program Files\Microsoft Visual Studio\MyProjects\proj3\proj3.cpp(89) : error C2143: syntax error : missing ';' before '{' D:\Program Files\Microsoft Visual Studio\MyProjects\proj3\proj3.cpp(100) : error C2181: illegal else without matching if D:\Program Files\Microsoft Visual Studio\MyProjects\proj3\proj3.cpp(114) : fatal error C1004: unexpected end of file found
^^ my errors, sorry if i misused the format or the rules, i just need to pass this in in 30 minutes, so it's a long shot.
Last edited by admin : 30-Sep-2005 at 23:48. Reason: Please insert your C code between [c] & [/c] tags
  #2  
Old 30-Sep-2005, 21:04
alyks alyks is offline
New Member
 
Join Date: Sep 2005
Posts: 12
alyks is on a distinguished road

Re: I'm very screwed, but please help.


First of all, put your code in the right tags.
Secondly, don't put semicolons after the end parenthesis for your if/else stuff.
Third, you have an extra bracket after your "Mega Team > Ultra Team > Super Team" thing.

Also, for those cout<< ""<<endl; things, you can just use a "\n" if you want a newline, if that's what you're trying to do.
  #3  
Old 30-Sep-2005, 23:04
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,703
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: I'm very screwed, but please help.


Quote:
Originally Posted by tylerfelix
[c++]

D:\Program Files\Microsoft Visual Studio\MyProjects\proj3\proj3.cpp(52) : error C2181: illegal else without matching if
D:\Program Files\Microsoft Visual Studio\MyProjects\proj3\proj3.cpp(52) : error C2143: syntax error : missing ';' before '{'
D:\Program Files\Microsoft Visual Studio\MyProjects\proj3\proj3.cpp(64) : error C2181: illegal else without matching if
D:\Program Files\Microsoft Visual Studio\MyProjects\proj3\proj3.cpp(65) : error C2143: syntax error : missing ';' before '{'
D:\Program Files\Microsoft Visual Studio\MyProjects\proj3\proj3.cpp(76) : error C2181: illegal else without matching if
D:\Program Files\Microsoft Visual Studio\MyProjects\proj3\proj3.cpp(77) : error C2143: syntax error : missing ';' before '{'
D:\Program Files\Microsoft Visual Studio\MyProjects\proj3\proj3.cpp(8 : error C2181: illegal else without matching if
D:\Program Files\Microsoft Visual Studio\MyProjects\proj3\proj3.cpp(89) : error C2143: syntax error : missing ';' before '{'
D:\Program Files\Microsoft Visual Studio\MyProjects\proj3\proj3.cpp(100) : error C2181: illegal else without matching if
D:\Program Files\Microsoft Visual Studio\MyProjects\proj3\proj3.cpp(114) : fatal error C1004: unexpected end of file found

i just need to pass this in in 30 minutes, so it's a long shot.

Well, I guess you missed the deadline --- sorry. However, some others reading this thread might wonder what the problem is, so I'll take a shot.

There seems to be a lot of: "illegal else without matching if". What could that mean? Could the compiler be more clear in its description of the problem?

There are basically two ways to use an if():
Here is one way
CPP / C++ / C Code:
.
  if (condition) {
  /* stuff executed here if the condition is true */
  }

Here is another
CPP / C++ / C Code:
.
  if (condition) {
  /* stuff executed here if the condition is true */
  }
  else {
  /* stuff executed here if the condition is not true */
  }

Note that, if the stuff to be executed consists of a single statement, the braces are optional.

And that's it! When the closing bracket at the end of the else block, then that's all you can do with that if. You can't just put an else anywhere you want it to be; it's always associated with an if. So in summary: you can have an if without an else, but you aren't allowed to have an else without an if.

Now for the stuff your program wants to do, you can have another if nested within the else:

CPP / C++ / C Code:
.
  if (condition1) {
    /* stuff executed  condition1 is true */
  } /* closes if stuff for condition1 */
  else {
    if (condition2) {
      /* stuff executed here if condition1 is not true and condition2 is true */
    } /* this closes the condition2 stuff */
    else {
      if (condition3) {
        /* stuff executed here if condition1 and condition2 both false, condition3 true */
      } /* closes if for condition3 */
    } /* closes the condition2 else */
  } /* this closes the else for condition 1 */



You can continue nesting to as many levels as you want.

This kind of stuff is used so much, that lots of people use the following shorter method of writing, but note that it is exactly the same as the above. This kind of looks like it could be a model for fixing your chain of logic:

CPP / C++ / C Code:
.
  if (condition1) {
    /* condition1 true stuff */
  } /* closes the if stuff for condition1 */
  else if (condition2) {
    /* condition2 stuff */
  } /* this closed the if  for condition2 */
  else if (condition3) {
    /* condition3 stuff */
  } /* closes if for condition3 */

Regards,

Dave
  #4  
Old 02-Oct-2005, 09:06
tylerfelix tylerfelix is offline
New Member
 
Join Date: Sep 2005
Posts: 28
tylerfelix is on a distinguished road

Re: Illegal else without matching if


I didn't exactly miss the deadline, but the coding that i handed in (seen above) will receive 50% tops. but thank you very much, you made me realize that I was just typing the code in the wrong way. I've done work in VB so this is completely new to me. I'll be posting some things in the near future, and if I get good enough, even helping people? haha.
 
 

Recent GIDBlogToyota - 2008 September Promotion by Nihal

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
'%' illegal, left operand, has type 'double' quasimof C++ Forum 10 27-Jul-2006 14:39
C++ PhoneBook marita C++ Forum 46 12-Jun-2005 12:10
"No matching function" error crystalattice C++ Forum 3 03-Dec-2004 09:57

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 22:44.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.