Thread: Program Not Functioning Correctly

  1. #1
    Programmer in Training TWIXMIX's Avatar
    Join Date
    Feb 2004
    Posts
    57

    Program Not Functioning Correctly

    Hello,
    I'm in the process of creating a dog simulation program to see what c++ I remember. I am having a problem, though. When my dog dies, I would like to it go back to the init function. I can do that fine but if I decide to choose exit after the dog dies, it does not quick the program. It continues on to the gamemenu function. I was wondering if anyone knew how I oculd fix this.

    This code is a WIP and still needs to be organized better and finished. I only have the feeding part completed.

    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    int dHappiness = 10;    //Dog Happiness Level
    int dHunger    = 10;    //Dog Hunger Level
    int dExercise  = 10;    //Dog Exercise Level
    int dObedience = 10;    //Dog Obedience Level
    
    string dName = "Shermin"; //Dog Name
    
    int  main();
    void init();            //Initilization Function
    void begin();           //Begin the Simulation
    void rename();          //Rename your Dog
    void gamemenu();        //Main Game Menu
    void feed();
    
    void init()
    {
        int menuChoice = 0;
    	
    	cout << "Welcome to the Dog Simulator v.1.0.\n";
    	cout << "Please Press:";
    	cout << "\n\n1) New Game";
    	cout << "\n2) Exit";
    	cout << "\n\nChoice: ";
    	cin >> menuChoice;
    	
    	if (menuChoice == 1)                    //If New Game Goto Begin Func to Change Name
            begin();
        else if (menuChoice == 2 || dAlive != 1) {              //If Exit Quit Sim
        }
        else {
        	cout << "Please choice 1 or 2.";    //If None Retry
            init();
        }
    }
    
    void begin()
    {
    	char changeName;
        cout << "\n\n\n";
        cout << "Welcome to TWIX's Dog Simulator. You have been left in charge of" << endl; 
        cout << "a friends dog named " << dName<<" and you have a bad memory. Would" << endl;
        cout << "you like to rename the dog?";
    	cout << "\n\nChoice(Y/N): ";
        cin  >> changeName; 
        
    	if (changeName == 'y') {
            rename();
    	}
        else if (changeName == 'n') {
            gamemenu();
        }
        else {
        	cout << "Please enter the correct letter.";
        	begin();
        }
    }
    
    void rename() 
    {
        cout << "\n\nWhat would you like the dog to be named?";
    	cout << "\nName: ";
    	cin >> dName;
    	
    	cout << "\n\nGreat! The dog will be named " << dName<< " until the owner returns.";	
    	
    	gamemenu();
    }
    
    void gamemenu() 
    {
    	int actionChoice;
        do {
        cout << "\n\n\nPlease use the menu below to interact with " << dName;
    	cout << "\n1)Feed";
    	cout << "\n2)Play";
        cout << "\n3)Exercise";
        cout << "\n4)Discipline " << dName;
        cout << "\n5)Exit";
        cout << "\n\nChoice: ";
        cin  >> actionChoice;
        
        if (actionChoice == 1) {
        	feed();
        }
        else if (actionChoice == 2) {
        	//play();
        }
        else if (actionChoice == 3) {
        	//exercise();
        }
        else if (actionChoice == 4) {
        	//discipline();
        }
        else if (actionChoice == 5) {
        }
        else {
        	cout << actionChoice << " is not an choice.Please try again.";
        }
        } while(actionChoice != 5 );
    }
    
    void feed()
    {
    	int foodChoice;
        cout << "\n\nPlease choice a food type that you would like to feed " << dName;
    	cout << "\n1)Beef";
    	cout << "\n2)Chicken";
    	cout << "\n3)Vegetables";
    	cout << "\n4)Dry Dog Food";
    	cout << "\n5)Turkey";
    	cout << "\n6)Chocolate";
    	
    	cout << "\n\nChoice: ";
    	cin  >> foodChoice;
    	
    	if (foodChoice == 1) {
    		cout << endl;
    		cout << dName << " loves beef.\n"; 
    		cout << dName << " gains +1 Hunger, +2 Happiness but, gets -1 Exercise.";
    		dHunger    += 1;
    		dHappiness += 2;
    		dExercise  -= 1;
    	}
    	else if (foodChoice == 2) {
    		cout << endl;
    		cout << dName << " enjoys his Chicken.\n"; 
    		cout << dName << " gains +3 Hunger, +1 Happiness but, loses 1 Excercise point.";
    		dHunger    += 3;
            dHappiness += 1;
    		dExercise  -= 1;
        }
        else if (foodChoice == 3) {
    		cout << endl;
            cout << "Vegetables make " << dName << " sick!\n";
    		cout << dName << " loses 3 Hunger points and loses 5 Happiness points.";
    		dHunger -= 3;
            dHappiness -= 5;
    	}
        else if (foodChoice == 4) {
        	cout << endl;
    		cout << dName << " loves dry dog food but it makes him thirsty.\n"; 
    		cout << dName << " keeps his current Hunger level.";
    	}
        else if (foodChoice == 5) {
        	cout << endl;
    		cout << dName << " really likes turkey.\n"; 
    		cout << dName << " gains +2 Hunger, +3 Happiness and loses 2 exercise points.";
    		dHunger += 2;
            dHappiness += 1;
    		dExercise  -= 1;
    	}
        else if (foodChoice == 6) {
    		cout << "You fed " << dName << " chocolate you idiot!\n";
    		cout << dName << " is now dead. Exiting simulation.\n\n\n";
    		init();
    	}
    }
    
    int main()
    {
        init();
        return 0;
    }
    This forum kinda messed up the formatting a bit.
    Learning C++
    Programmer in training

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I think the problem lies in your recursive use of init().

    When the program runs, init() is called.
    The user chooses to start a new game, so begin() is called.
    In begin(), the user may choose to rename the dog, or otherwise. Either way gamemenu() is called.
    In gamemenu(), the user may choose to feed the dog, upon which feed() is called.
    In feed(), the user may choose to feed the dog chocolate, upon which the dog dies and init() is called.
    In init(), the user chooses to exit.
    init() completes, and returns control to feed(), not to main()!
    Now feed() completes, and returns control to gamemenu().
    This is why the game menu is displayed.

    EDIT:
    oh, and I think your dAlive variable is missing, and you dont need to have a prototype for main().
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Programmer in Training TWIXMIX's Avatar
    Join Date
    Feb 2004
    Posts
    57
    Yea, I forgot to remove the dAlive before I posted here. I took that out of my code.
    Thanks for the advice, I'll look into it.
    Learning C++
    Programmer in training

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > This forum kinda messed up the formatting a bit.
    Change your IDE to only use spaces for indenting. Mixing spaces and tabs only results in chaotic indentation for most everyone not using your particular editor.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Code:
    else if (actionChoice == 4) {
        	//discipline();
        }
        else if (actionChoice == 5) {
        }
        else {
        	cout << actionChoice << " is not an choice.Please try again.";
        }
        } while(actionChoice != 5 );
    You could change that to
    Code:
    else if (actionChoice == 4) {
        	//discipline();
        }
        else if(actionChoice != 5) {
        	cout << actionChoice << " is not an choice. Please try again.";
        }
        } while(actionChoice != 5 );
    To fix the recursive problem, what I would do is have feed() return and int. It returns 0 if the dog is not dead, and 1 if it is. feed()s calling function, gamemenu(), would check the return value of feed(). If it is 1, gamemenu() returns 1 as well. And so on up the function calls, until you get to init() or where ever you wanted to get to.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. have i written this program correctly ?
    By broli86 in forum C Programming
    Replies: 6
    Last Post: 07-20-2008, 03:20 PM
  2. Inserting text into MDI program
    By Rutabega in forum Windows Programming
    Replies: 0
    Last Post: 12-23-2005, 11:25 AM
  3. my server program auto shut down
    By hanhao in forum Networking/Device Communication
    Replies: 1
    Last Post: 03-13-2004, 10:49 PM
  4. insufficient memory for tsr
    By manmohan in forum C Programming
    Replies: 8
    Last Post: 01-02-2004, 09:48 AM
  5. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 12:39 PM