tags:

views:

116

answers:

2

I made a function to create cows variable, then later I made another function to butcher them. The two functions are methods in a class. When I use the create cow function it works, and I get the value of cows. However when I access the butcher function the value of cows is zero.

It's like there are two different variables; I didn't think that functions could have local variables if they were void functions.

How can I fix this?

#include <string>
using namespace std;

class Cheif
{
    int points;
    string name;

protected:
    int NoodleS;
    int RosemaryS;
    int ParcleyS;
    int PepperS;
    int Field;
    int Water;

public:
    int star; 
    int foodPrep; 
    int ingrPrep;     
    int endOfPrep;
    int money;
    int ingr1;
    int ingr2;
    int ingr3;
    int Noodle;
    int Rosemary;
    int Parcley;
    int Pepper;
    int chickMeat;
    int beef;
    int chickens;
    int cows;

// constructor 
    Cheif()
    {
        money = 1000;
        points = 0;
        star = 10;    
        endOfPrep = 0; 
        ingr1 = 0;
        ingr2 = 0;
        ingr3 = 0;
        Noodle = 0;
        Rosemary = 0;
        Parcley = 0;
        Pepper= 0 ;
        cows = 0;
        chickens = 0;
        beef = 0;
        chickMeat = 0;
    }

// method

//////////////////////////////////////////////////
//                                              //
//            ask user for their name           //
//                                              //
//////////////////////////////////////////////////
    void FindName()
    {

        cout << "what is your name? " << endl;;
        cin >> name;
        cout << endl << " Welcome " << name
         << " let us begin... " <<endl;   


    }

//////////////////////////////////////////////////
//                     END                      //
//////////////////////////////////////////////////

//////////////////////////////////////////////////
//                                              //
//         Buy animal live stock                //
//                                              //
//////////////////////////////////////////////////
    void Buy_Cow()
    {
        if(money > 199)
        {
        money -= 200;
        cows += 1;
        cout <<" you now have " << cows << " cows" << endl;
        }
    }

    void Buy_Chick()
    {
        if(money > 99)
        {
        money -= 200;
        chickens += 1;
        } 
    }

//////////////////////////////////////////////////
//                     END                      //
//////////////////////////////////////////////////


//////////////////////////////////////////////////
//                                              //
//         if user goes to open store           //
//                                              //
//////////////////////////////////////////////////

    void GOTO_Store()
    {
        // while food is not prepared yet
        while(endOfPrep == 0){
        PREPAREMEAL:
        // show menu
        cout << "<1> Make Food \n"
             << "<2> Collect Ingridents \n"        
             << "<3> Butcher Animal \n"
             << "<4> Go Back... \n" << endl;


        // create variable to hold choice
        string OS_Choice;

        cin >> OS_Choice;   

        /////////// if user decides to "make food" /////////////
        if (OS_Choice == "1")
        {
            if(foodPrep == 1)
            {
            cout << "you've already prepared the meal..." << endl;         
            }else{          
            if(ingr1 <= 0 ||ingr2 <= 0 || ingr3 <= 0)
            {
                goto PREPAREMEAL;
            }else{          
                cout << "your using" << ingr1 << " " << ingr2<< " " << ingr3 << endl;
            } // end of ingredient check

            cout << " and how shall this mean be prepared? " << endl;

            int prepMethod;

            cout << "<1> Baked \n "
                 << "<2> boiled \n "
                 << "<3> Fried \n "
                 << "<4> mixed \n ";

            cin >> prepMethod;

            cout << " And what kind of sides would you like to add? " << endl;

            int sideChoice;

            cout << "<1> bread Roll \n " 
                 << "<2> Rice \n "
                 << "<3> Beans \n" << endl;

            cin >> sideChoice;

            foodPrep = 1;
            }// end of food choice 

            //begin food compare. 
            /////////// if user decides to get ingrediants /////////////
        }else if(OS_Choice == "2"){
            if (ingrPrep == 1){
            cout << " you have already collected the ingredients " << endl;  

            }else{     
            cout << "what 3 ingridents will you use? " << endl;
            cin >> ingr1;
            cin >> ingr2;
            cin >> ingr3;
            }// end of ingrident prep
            /////////// if user decides to get ingrediants /////////////
        }else if(OS_Choice == "3")
        {
            cout << " you have " << cows << " cows \n "
             << " you have " << chickens << " Chickens \n ";

            cout << "what would you like to butcher? " << endl;
            cout << "<1> Cows    : " << cows << endl;
            cout << "<2> Chicken : " << chickens << endl;
            int B_Choice;

            cin >> B_Choice;

            if(B_Choice == 1){
            if(cows == 0){
                cout << " sorry you dont have any cows " << endl;
            }else{
                cows = cows - 1;
                beef = beef + 5;
                cout << " you now have " << beef << "peices of cow meat" << endl;
                ingrPrep = 1;
            }//end of cow check
            }else if(B_Choice == 2){
            if(chickens == 0){
                cout << " sorry you dont have any chickens " << endl;
            }else{
                chickens = chickens - 1;
                chickMeat = chickMeat + 2;
                cout << " you now have " << chickMeat << "peices of Chicken meat" << endl;
                ingrPrep = 1;
            } // end chicken Check
            }else {
            cout << "invalid Choice" << endl;
            }// end of b choice
        }else if(OS_Choice == "4") {
            endOfPrep = 1;      
            foodPrep = 0;
            ingrPrep = 0;
            ingr1 = 0;
            ingr2 = 0;
            ingr3 = 0;
        }// end of ingr prep. 
        }//end of while loop  
    }
//////////////////////////////////////////////////
//                     END                      //
//////////////////////////////////////////////////
};
+1  A: 

Whether or not the methods have return type void or not doesn't matter; if they're part of the same class they will use the class' instance variables.

But your methods don't look like methods; there's no class name before their names. Are these defined in the declaration of the class?

unwind
He might of implemented the methods inside the his class. Still, it would be very unorganized to do so, and he also refers to them as functions.
thyrgle
@thyrgle they are methods in my class. and the code is horribly unorganized at the moment. i usually clean up after i hit a checkpoint in my code
TimothyTech
yes the class is huge, whenever i post the entire code of my program people slap my hand, when i dont i still get slapped. there isnt much victory on this forum for me.
TimothyTech
@Timothy You should reduce your source code to a compilable example that reproduces your problem and requires no domain knowledge. Doing so is often the way to solving the problem.
pmr
+1  A: 

There's nothing obviously wrong with the construction of your class that would be causing the problem you're describing. (The point unwind makes about it not usually being a good idea to put your method implementations directly inside the class declaration is correct, but it's not the cause of your problem.)

In fact, if I add a simple main() to the end of the code you posted:

int main() {

   Cheif c;

   c.Buy_Cow();
   c.GOTO_Store();
}

and compile & run it, it gives the expected results (value of cows=1).

So, that tells you the problem isn't with this class, it's with how you're calling it from the rest of your program.

To track that down, the usual debugging techniques apply. For example:

  • try to identify a specific sequence of events that's causing the problem
  • use a debugger and/or print statements to figure out where things are going wrong
David Gelhar