c++ battle game code help

This is a discussion on c++ battle game code help within the C++ Programming forums, part of the General Programming Boards category; Hi, I started a battle game today for practice and maybe im blind but I cannot see why this does ...

  1. #1
    Registered User
    Join Date
    Jun 2004
    Posts
    52

    c++ battle game code help

    Hi, I started a battle game today for practice and maybe im blind but I cannot see why this does not work.

    Code:
    void town()
    {
    	int placechoice, life, maxLife;
    
    	life = 100;
    	maxLife = 200;
    
    	system("CLS");
    	cout << "Life = " << life << endl << endl;
    	cout << "Welcome to the townsquare." << endl;
    	cout << "1. Go into the forest" << endl;
    	cout << "2. Go to the shop" << endl;
    	cin >> placechoice;
    
    	if (placechoice == 1)
    		introforest();
    	else if (placechoice == 2)
    		shop(life, maxLife);
    }
    
    void shop(int &life, int &maxLife)
    {
    	int shopchoice;
    
    	system("CLS");
    	cout << "Welcome to my store, I am the shopkeeper Alkhaza." << endl;
    	cout << "What would you like?" << endl;
    	cout << "1. Free Healing" << endl;
    	cout << "2. Leave shop" << endl;
    	cin >> shopchoice;
    
    	if (shopchoice == 2)
    		town();
    	else if (shopchoice == 1)
    	{
    		system("CLS");
    		life = maxLife;
    		cout << "You have been healed for no fee!" << endl;
    		cout << "Goodluck on your adventures" << endl;
    		system("PAUSE");
    		town();
    	}
    }
    Well here is the problem.........
    I am testing changing the life when you heal, but for some reason it never sets it to 200, it stays 100. Any suggestions would be gladly appreciated. Thanks guys.

  2. #2
    Registered User
    Join Date
    Oct 2004
    Posts
    120
    Since you are using indirect recursion, the variable scope and initialization comes into play. Look at how your declaring and initializing the variables.

    If you can get a debugger, step through the code and you will see exactly what is happening. Or you could do it by hand, it only took me a minute to trace through statement by statement to see what is going to happen...and don't skip a line when reading. Thats probably your problem, your making assumptions because you wrote the code. Read it like someone else wrote it and your have better luck.

  3. #3
    Registered User
    Join Date
    Jun 2004
    Posts
    52
    Hrmmm.......I tested it and it does change the life in the shop function, but when it goes back to the town function it does not change the value of life. Because it is reference, shouldnt it permanantly change it? Or am I backwards?

  4. #4
    unleashed alphaoide's Avatar
    Join Date
    Sep 2003
    Posts
    696
    Code:
            if (shopchoice == 2)
    		// town();  no need to call town
                    // when you exit this function, control returns to caller
                    // which is town()
                    return;
    	else if (shopchoice == 1)
    	{
    		system("CLS");
    		life = maxLife;
    		cout << "You have been healed for no fee!" << endl;
    		cout << "Goodluck on your adventures" << endl;
    		system("PAUSE");
    		// town();
                    return;
    	}
    source: compsci textbooks, cboard.cprogramming.com, world wide web, common sense

  5. #5
    Teenage Mutant Ninja Nerd MMD_Lynx's Avatar
    Join Date
    Aug 2004
    Posts
    65
    can u say, "infinite loop?"
    Stupid people are useful. You can make them do all the mindless tasks you are too lazy to do yourself.

    Sphynx cats are just bald and wrinkly, like old people, and we don't reject them.

  6. #6
    Registered User
    Join Date
    Oct 2004
    Posts
    120
    Its not quite an inifinite loop because the user can always choose not to go to the shop, but the stack might not ever unwind properly. It is an example of indirect recursion , which is certaintly not desired by Siggy. Siggy, were you able to realize your error or do you need some more clarification?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C Programming 2d Array Question
    By jeev2005 in forum C Programming
    Replies: 3
    Last Post: 04-26-2006, 04:18 PM
  2. beach bar (sims type game)
    By DrKillPatient in forum Game Programming
    Replies: 1
    Last Post: 03-06-2006, 01:32 PM
  3. PC Game project requires c++ programmers
    By drallstars in forum Projects and Job Recruitment
    Replies: 2
    Last Post: 02-22-2006, 12:23 AM
  4. Battle cards game
    By sagi4626 in forum C++ Programming
    Replies: 8
    Last Post: 05-04-2002, 04:04 AM
  5. Game of Craps Source Code
    By Miles in forum C Programming
    Replies: 5
    Last Post: 02-17-2002, 01:46 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21