Thread: Several Probs

  1. #1
    Registered User
    Join Date
    Jun 2002
    Posts
    230

    Several Probs

    Hi and Merry x- mas all.

    Im trying to finish the first stage of my text based rpg game.
    Its my first game and im almost done. Im having one problem. Its compiling fine but its just not working and one point. It has a problem with the function whenfight(). Im going to put a bunch of if statments there so i can know when the person is going to fight. LIke if they were at 1 west and 2 south. or 3 north and 6 east.

    The problem is when the user enters one which is suppose to be a fight in any direction it says error error, forever.
    Im not posting the whole code, unless necessary because its 4 files. Ive narrowed the problem down. It says ERROR SOMEWHERE HERE in the comment.

    Code:
    #ifndef GUARD_Stage1
    #define GUARD_Stage1
    #include <iostream>
    int west, north, south, east; //directions
    
    class Stage1
    { //open class
    private:
    int t_count; // total count of directions
    int west, north, south, east; //directions
    public:
    Stage1() : west(0),north (0),south(0),east (0),t_count(0)
    {}//contstructor ones
    void whenfight();   // when should i fight
    void showbathealth(); // show the health in battle
    void showtothealth(); // show the health overall
    void gethealth(); // get the health
    void getarea(); // get users choice of destination
    void fight(); // if in a fight
    void win(); // if you win 
    void lose(); // if you lose
    }; //close class
    
    void Stage1::getarea() 
    { //stage1
        while (t_count <=100)
        { //while
        int areach = 0;
        Stage1::gethealth(); 
        Stage1::showtothealth();   
        cout << " Choose your destination(1,2,3,4)"<<endl;
        cout << " 1: West "<<endl;
        cout << " 2: East "<<endl;
        cout << " 3: South  "<<endl;
        cout << " 4: North "<<endl;
        cin >>areach;
        t_count+= 1; //increment total coutner of turns
        cin.ignore( 10, '\n' );
            switch (areach)
                {
                    case 1:  //west
                    west +=1; //increment coutner for batoptions
                    cout << "w";
                    break;
                    case 2:  // east
                    east +=1;//increment coutner for batoptions
                    cout << "e";
                    break;
                    case 3:  //south
                    south +=1;//increment coutner for batoptions
                    cout << "s";
                    break;
                    case 4:  //north
                    north +=1;//increment coutner for batoptions
                    cout << "n";
                    break;
                    default: 
                    cout << "INVALID INPUT"<<endl;
                }
             Stage1::whenfight(); // SOMEWHERE HERE!!!!!!
             
    //-----------------------------------------------------------------    
        }//end while
       
            
    } //end stage1
        
        double battdam = 100; //users damage
        double tothealth = 100; //total damage
        double compdam = 100; //computer damage
    
    void Stage1::fight() 
    {   
        
        int fch; // for switch
        cout << "You have entered a battle";
        cout << "1)punch";
        cout << "2)kick";
        cout << "3)defend";
        while (compdam > 0 || battdam >0) //while life is greater than0
        { //while one
            switch (fch)
                { //switch one
                        case 1:
                        cout << "you have punched 10 damage";
                        compdam -= 10; //subtract user and 
                        battdam -= 5; // computer life
                        cout << "You were hit, minus 5 damage";
                        Stage1::showbathealth();
                        break;
                        case 2:
                        cout <<"you have kicked 5 damage";
                        compdam -= 5;//subtract user and 
                        battdam -= 10;// computer life
                        cout <<"You were hit minus 10 damage";
                        Stage1::showbathealth();
                        break;
                        case 3:
                        cout <<"you defend, lose 5 damage";
                        battdam -= 5;//subtract user and 
                        Stage1::showbathealth();
                        break;        // computer life
                        default:
                        cout <<"error";  //ERROR SOMEWHERE HERE!!!!!
                        break;
                } //switch one
        } //while one
        
    }//end function
    void Stage1::showbathealth()  // show users total health
    {
        cout << "Your health now is " << battdam<<endl;
    }
    void Stage1::showtothealth()  // show users total health
    {
        cout << "Your health now is " << tothealth<<endl;
    }
    void Stage1::gethealth() // get health from battle to total
    {   
        tothealth = tothealth - ((100 - battdam) * .5);
    }
    void Stage1::whenfight() //ERROR SOMEWHERE HERE!!!!!!!!!!!
    {
        Stage1 sf1;  //FUNTION TO FIND OUT WHEN TO FIGHT
        if (west == 1 || south == 1 || north == 1 ||east == 1)
        {sf1.fight();}
        
    }
    #endif
    THX a bunch. any suggestions or help is appreciated. Have a good x-mas.

    NOTE ill post all code or attatch all code if needed.
    C++ Rules!!!!
    ------------
    Microsoft Visual Studio .NET Enterprise

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >switch (fch)
    fch is indeterminate at this point, you never initialize it or cin a value to it. Just reading an indeterminate variable is undefined, you're lucky that the program just prints the default case.

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

  3. #3
    Registered User
    Join Date
    Jun 2002
    Posts
    230
    well thx for the quick response but that only fixed a little prob.
    now it goes into battle but after i choose punch, kick , or defend it says error error error error...

    heres what i changed
    Code:
    int fch = 0; // added the = 0
        cout << "You have entered a battle";
        cout << "1)punch";
        cout << "2)kick";
        cout << "3)defend";
        cin.ignore( 10, '\n' );//added

    ANY MORE HEP PLZ!!
    C++ Rules!!!!
    ------------
    Microsoft Visual Studio .NET Enterprise

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    You didn't fix your problem, you just removed the undefined behavior. fch is 0, which still brings you to the default case, you need to read a value into fch like this:
    Code:
    int fch; // for switch
    cout << "You have entered a battle";
    cout << "1)punch";
    cout << "2)kick";
    cout << "3)defend";
    
    cin>> fch;
    cin.ignore() // Clean garbage
    -Prelude
    My best code is written with the delete key.

  5. #5
    Registered User
    Join Date
    Jun 2002
    Posts
    230
    THX A BUNCH


    that a fixed the problem. i kind of figured it would be something easy. Have a nice day.
    C++ Rules!!!!
    ------------
    Microsoft Visual Studio .NET Enterprise

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 2 simple C++ probs
    By alikoolg in forum C++ Programming
    Replies: 4
    Last Post: 03-18-2009, 09:02 PM
  2. Replies: 2
    Last Post: 11-04-2007, 12:55 PM
  3. WM_TIMER probs (FPS Calculations)
    By C+noob in forum C++ Programming
    Replies: 0
    Last Post: 10-02-2005, 02:39 AM
  4. magic sq probs
    By scuba22 in forum C++ Programming
    Replies: 2
    Last Post: 11-18-2002, 09:40 AM
  5. memory allocations probs (continue)
    By CyC|OpS in forum C Programming
    Replies: 9
    Last Post: 10-19-2002, 09:32 PM