Thread: Can Anyone See The Error Here?

  1. #1
    Registered User LordVirusXXP's Avatar
    Join Date
    Dec 2002
    Posts
    86

    Question Can Anyone See The Error Here?

    Here's my code:

    Cpp1.cpp:

    Code:
    #include <iostream.h>
    #include "header2.h"
    
    int main() 
    {
    	
    	int input;
    	cout << "What do you want to do?";
    	for ( ; ; )
    	{
    	enum {Enter = 1, Exit = 2};
    	bool exit = false;
    	cout << "\n1 - Play Game";
    	cout << "\n2 - Exit Program";
    	cout << "\nCommand: ";
    	cin >> input;
    	switch (input);
    	{
    	case Enter: cout << "Loading. ";
    		break;
    	case Exit: exit = true;
    		break;
    	}
    	{
    	if (exit)
    		break;
    	else
    		continue;
    	}
    		return 0;
    }
    header2.h:
    Code:
    //This creates a database structure for Character
    
    class Character
    {
    private:
    
    int HP;
    int Exp;
    
    public:
    
    Character();
    ~Character();
    
    void SetHP(int hit_points_left);
    void SetExp(int points);
    
    int GetHP() const;
    int GetExp() const;
    };
    
    //Constructors and destructors of Character:
    
    Character::Character()
    {
    	HP = 50;
    	Exp = 0;
    }
    Character::~Character()
    {
    }
    
    // Sets Character's HP:
    
    void Character::SetHP(int hit_points_left)
    {
    	HP = hit_points_left;
    }
    
    // Retrieves Character's HP:
    
    int Character::GetHP() const
    {
    	return HP;
    }
    
    // Sets Character's Experience Points:
    
    void Character::SetExp(int points)
    {
    	Exp = points;
    }
    
    // Retrieves Character's Experience Points:
    
    int Character::GetExp() const
    {
    	return Exp;
    }
    
    //Creates an object of your Character.
    
    Character You;
    
    //This is the level up database:
    
    int Check_Exp()
    {
    	int your_exp = You.GetExp();
    	enum {level1 = 20, level2 = 60, level3 = 100};
    	switch (your_exp)
    	{
    	case level1: cout << "Place holder one";
    		break;
    	case level2: cout << "Place holder two";
    		break;
    	case level3: cout << "Place holder three";
    		break;
        default:cout << "Default";
    		return your_exp;
    }

    and it gives me the following errors when I try to build Cpp1.cpp:

    Error C2601: 'main' : local function definitions are illegal

    Fatal error C1075: end of file found before the left brace '{'
    I, and several others looked this over, and can't find anything wrong with this source code. Can anyone see what's wrong with it?
    Last edited by LordVirusXXP; 01-03-2003 at 09:48 PM.
    - Dean

  2. #2
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    Location
    NJ
    Posts
    1,141
    Local function definitions typically mean your braces are messed up. Indeed, this is the case.

    Code:
    int Check_Exp()
    {
    int your_exp = You.GetExp();
    enum {level1 = 20, level2 = 60, level3 = 100};
    switch (your_exp) // unclosed switch
    {
    case level1: cout << "Place holder one";
    break;
    case level2: cout << "Place holder two";
    break;
    case level3: cout << "Place holder three";
    break;
        default:cout << "Default";
    // should close it here
    return your_exp;
    }

  3. #3
    Registered User LordVirusXXP's Avatar
    Join Date
    Dec 2002
    Posts
    86
    It still doesn't work. I'm starting to think it's not even my fault.

    The new code is the same exact thing as the first one only uses the above definition for Check_Exp(). It still gives me the same errors.
    Last edited by LordVirusXXP; 01-03-2003 at 09:53 PM.
    - Dean

  4. #4
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    New code? New errors?
    Let this be a testament to indentation. Had you indented properly, you would have noticed the error very quickly.

    Remember; it's always your fault.
    Last edited by Eibro; 01-03-2003 at 09:59 PM.

  5. #5
    Microsoft. Who? MethodMan's Avatar
    Join Date
    Mar 2002
    Posts
    1,198
    Code:
    #include <iostream.h>
    #include "header2.h"
    
    int main()
    {
    
    int input;
    cout << "What do you want to do?";
    for ( ; ; )
    {
    enum {Enter = 1, Exit = 2};
    bool exit = false;
    cout << "\n1 - Play Game";
    cout << "\n2 - Exit Program";
    cout << "\nCommand: ";
    cin >> input;
    switch (input);
    {
    case Enter: cout << "Loading. ";
    break;
    case Exit: exit = true;
    break;
    }
    {
    if (exit)
    break;
    else
    continue;
    }
    return 0;
    }
    Unless there is more to your code than this, you are also missing a closing } here too.
    -MethodMan-

    Your Move:Life is a game, Play it; Life is a challenge, Meet it; Life is an opportunity, capture it.

    Homepage: http://www.freewebs.com/andy_moog/home.html

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >switch (input);
    Oops, watch that semicolon.

    Check your braces in main, something is a little off:
    Code:
    }
    { // Try removing this one
    if (exit)
    -Prelude
    My best code is written with the delete key.

  7. #7
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    In addition to what has already been said, I would suggest avoiding break and continue. Now, before everyone comes rushing to defend those statements, I'd just like to say that in this case, I fell that they're unnecessary. LordVirusXXP already has some boolean flags in place. Instead of that switch, he could use a simple if statement if he keeps in mind that a boolean expression evaluates to zero if false and true if non-zero. I feel that a forever loop isn't necessary. while(!exit) seems more appropriate and will tighten the code.

    Also, there's no reason to post that header file if it's not being used in main, and it's not the source of the errors.
    FAQ

    "The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs." -- Joseph Weizenbaum.

    "If you cannot grok the overall structure of a program while taking a shower, you are not ready to code it." -- Richard Pattis.

  8. #8
    Registered User LordVirusXXP's Avatar
    Join Date
    Dec 2002
    Posts
    86
    My new source code:

    Code:
    #include <iostream.h>
    #include "header2.h"
    
    int main() 
    {
    	
    	int input;
    	cout << "What do you want to do?";
    	for ( ; ; )
    	{
    		enum {Enter = 1, Exit = 2};
    		bool exit = false;
    	
    	cout << "\n1 - Play Game";
    	cout << "\n2 - Exit Program";
    	cout << "\nCommand: ";
    	cin >> input;
    	
    	switch (input)
    	{
    	case Enter: cout << "Loading. ";
    		break;
    	case Exit: exit = true;
    		break;
    	}
    	if (exit)
    		break;
    	else
    		continue;
    	}
    		return 0;
    }
    ..and it still gives me the same errors.
    - Dean

  9. #9
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >..and it still gives me the same errors.
    With all of the changes made in this thread from the original code you posted, I only get one warning:

    'Check_Exp' : not all control paths return a value

    otherwise a clean compilation. Could you give us all of the code and exactly what errors you're recieving?

    -Prelude
    My best code is written with the delete key.

  10. #10
    Registered User LordVirusXXP's Avatar
    Join Date
    Dec 2002
    Posts
    86

    Thanks, Kermi3!

    I finally found it. Kermi3 pointed out that the switch statement in Check_Exp() never closes. Thanks, Kermi3.
    - Dean

Popular pages Recent additions subscribe to a feed