CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Jan 2017
    Posts
    2

    [RESOLVED] Need help with piece of code

    Code:
    #include "stdafx.h"
    #include <iostream>
    #include <string>	
    #include <Windows.h>
    
    int main()
    {
    	int mxhp, chp, emxhp, echp; 
    	int atk, def, dam;
    	int eatk, edef, edam;
    	int mxmp, cmp, matk, mdef, mdam, ematk, emdef, emdam; 
    	int choice; 
    	int level, mxlevel; 
    
    	level = 1; 
    	mxlevel = 1;
    		
    	while (level == 1)
    	{
    		mxhp = 100;
    		chp = 100;
    		atk = 20;                 //You and your Enemies stats 
    		def = 15;                //E at the beginning indicates it's the enemies stats
    		emxhp = 80;
    		eatk = 30;
    		edef = 10;
    		echp = 80;
    		dam = atk - edef;
    		edam = eatk - def;
    
                   std::cout << "#######################" << std::endl;
    		std::cout << "Your HP:" << chp << std::endl;           //Move selection menu 
    		std::cout << "#######################" << std::endl;
    		std::cout << "Enemy HP:" << echp << std::endl;
    		std::cout << "#######################" << std::endl;
    		std::cout << "What will you do?" << std::endl;
    		std::cout << "1. -> Attack" << std::endl;
    		std::cout << "2. -> Heal" << std::endl;
    
    		std::cin >> choice;
    
    		if (choice == 1)
    		{
    			std::cout << "You did" << dam << "to the opponent!" << std::endl;  //Showing your damage
    			system("pause");
    
    			echp = emxhp -= dam;
    
    		}
    		if (choice == 2)
    		{
    			std::cout << "You healed yourself!" << std::endl;
    
    			if (chp < 100)
    			{
    				chp = mxhp += 25;    //How much you heal yourself 
    
    			}
    
    		}
    
    		else
    		{
    			std::cout << "Please choose an option" << std::endl;
    		}
    
    		std::cout << "The enemy attacked!" << std::endl;                  //Showing Enemy damage
    		std::cout << "The enemy did" << edam << "to you!" << std::endl;
    		system("pause");
    		system("cls");
    
    		chp = mxhp -= edam;
    
    		if (echp <= 0)
    		{
    			std::cout << "You won!" << std::endl;  //End screen
    			std::cout << "Skills learned:" << std::endl;
    			std::cout << "Fire" << std::endl;
    			std::cout << "Items Obtained:" << std::endl;
    			std::cout << "None" << std::endl;
    			std::cout << "Stats Increased:" << std::endl;
    			std::cout << "Attack" << std::endl;
    			level = mxlevel += 1;
    			break;
    		}
    
    		if (chp <= 0)
    		{
    			std::cout << "You lost..." << std::endl; //End screen 
    			break;
    		}
    
    	}
    
    		while (level == 2)
    		{
    			mxhp = 100;
    			chp = 100;
    			mxmp = 20;                  //You and your Enemies stats
    			cmp = 20;
    			atk = 25;
    			matk = 15;
    			def = 15;
    			mdef = 10;
    			emxhp = 100;
    			eatk = 30;
    			ematk = 0;
    			edef = 20;
    			emdef = 0;
    			echp = 100;
    			dam = atk - edef;
    			mdam = matk - emdef;
    			edam = eatk - def;
    			emdam = ematk - mdef;
    
    			std::cout << "#######################" << std::endl;
    			std::cout << "Your HP:" << chp << std::endl;
    			std::cout << "Your Mp:" << cmp << std::endl;
    			std::cout << "#######################" << std::endl;
    			std::cout << "Enemy HP:" << echp << std::endl;
    			std::cout << "#######################" << std::endl;
    			std::cout << "What will you do?" << std::endl;
    			std::cout << "1. -> Attack" << std::endl;
    			std::cout << "2. -> Heal" << std::endl;
    			std::cout << "3. -> Fire" << std::endl;
    
    			std::cin >> choice;
    
    			if (choice == 1)
    			{
    				std::cout << "You did" << dam << "to the opponent!" << std::endl;  //Showing your damage
    				system("pause");
    
    				echp = emxhp -= dam;
    			}
    			if (choice == 2)
    			{
    				std::cout << "You healed yourself!" << std::endl;
    
    				if (chp < 100)
    				{
    					chp = mxhp += 25;    //How much you heal yourself 
    
    				}
    			}
    			if (choice == 3)
    			{
    
    				if (cmp >= 5)
    				{
    					std::cout << "You cast fire!" << std::endl;     //Showing your damage
    					cmp = mxmp -= 5;
    					echp = emxhp -= mdam;
    					std::cout << "You did " << mdam << "to the enemy!" << std::endl;
    				}
    			}
    
    			else
    			{
    				std::cout << "Please choose an option" << std::endl;
    			}
    
    			std::cout << "The enemy attacked!" << std::endl;                  //Showing Enemy damage
    			std::cout << "The enemy did" << edam << "to you!" << std::endl;
    			chp = mxhp -= edam;
    			system("pause");
    			system("cls");
    
    			if (echp <= 0)
    			{
    				std::cout << "You won!" << std::endl;  //End screen
    				std::cout << "Skills learned:" << std::endl;
    				std::cout << "None" << std::endl;
    				std::cout << "Items Obtained:" << std::endl;
    				std::cout << "Shield" << std::endl;
    				std::cout << "Stats Increased:" << std::endl;
    				std::cout << "Defense" << std::endl;
    				std::cout << "HP" << std::endl;
    				break;
    			}
    			if (chp <= 0)
    			{
    				std::cout << "You lost..." << std::endl;   //End screen
    				break;
    			}
    
    		}
    
    }
    So I'm doing this little project for school. It is a very simple RPG. The code worked fine with one enemy, but I tried to add a second enemy to fight after the first. That's when I encountered problems. For some reason the player or enemy can't do damage. I working on Visual Studio 2015. Please help me out. Thanks in advance.
    Last edited by 2kaud; January 23rd, 2017 at 03:28 AM. Reason: Added code tags

  2. #2
    Join Date
    Jan 2017
    Posts
    2

    Re: Need help with piece of code

    Nvm, figured it out.

  3. #3
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,824

    Re: Need help with piece of code

    That's great. If you post code in the future, please would you use code tags. Go Advanced, select the formatted code and click '#'.

    Cheers.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured