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

    C++ Porgram help

    Hello everyone! I was wondering if anyone would be willing to help me with a program I am working on. I will admit I am not very good at programing and have very little confidence in my skills so far. What I am trying to do is get the program below to continue after a battle and offer the player to 1. rest, 2. Move, 3. fight or 9. quit again. Right now if the user selects move and then fight the game ends.

    Can anyone give me a suggestion on how to do this?

    Thank you in advance.

    Code:
    #include<iostream>
    #include<string>
    #include<vector>
    #include<algorithm>
    #include<ctime>
    #include<cstdlib>
    
    using namespace std;
    
    int main()
    {
    	
    	vector<string> inventory;       //inventory of weapons
    	inventory.push_back("sword");
    	inventory.push_back("bow");
    	inventory.push_back("wand");
    	inventory.push_back("dagger");
    
    	vector<string> player;          //player options
    	player.push_back(" ");          //empty place holder to move character names into the #1-4 spot to match enum values
    	player.push_back("Knight");
    	player.push_back("Archer");
    	player.push_back("Sorcerer");
    	player.push_back("Elf");
    
    	vector<string> foe;             //foe options
    	foe.push_back("Dragon");
    	foe.push_back("Troll");
    	foe.push_back("Worlock");
    	foe.push_back("Fairy");
    
    	
    	enum Player{Knight=1, Archer, Sorcerer, Elf};   //enum to give the words integer assignments
    	enum Action{Rest=1, Move, Fight, Run, Quit=9};
    	Player myPlayer;     //assign specific variable to my enums        
    	Action myAction;
    
    	int health = 50;
    	int power = 25;
    	int choice;
    	
    	string name;  //assigned for user name input
    
    	cout<<"\tYour princess has been taken by an evil witch.\n";
    	cout<<"You must travel across the land through the forest,";
    	cout<<"into the caves, and over the marshlands to save her.\n\n";
    	cout<<"What name do you go by?  ";
    	cin>>name;  //user input character name
    
    	cout<<"\n\nVery well "<<name<<" , what type of adventurer are you?\n\n";
    	cout<<"Please select your character type: 1 - Knight\n";
    	cout<<"                                   2 - Archer\n";
    	cout<<"                                   3 - Sorcerer\n";
    	cout<<"                                   4 - Elf\n";
    	cin>>choice;    //user input character persona
    	bool success;   //test for condition in switch/case
    	myPlayer=Player(choice);  //assign user choice to enum variable
    	do       //do loop to execute user choice of character persona
    	{
    		switch (myPlayer)
    		{
    	
    			case Knight:
    	
    				cout<<"You are a brave Knight, "<<name<<".";
    				myPlayer==Knight;
    				success=true;
    				break;
    
    			case Archer:
    	
    				cout<<"You are a deadly Archer, "<<name<<".";
    				success=true;
    				break;
    
    			case Sorcerer:
    	
    				cout<<"Your magic is very powerful, Sorcerer "<<name<<".";
    				success=true;
    				break;
    
    			case Elf:
    	
    				cout<<"You are a sneaky Elf, "<<name<<".";
    				success=true;
    				break;
    
    			default:
    
    			success=false;
    			cout<<"Please choose another adventurer.";
    			cin>>choice;
    		
    		}
    	}while (success=false);
    	
    	
    	srand(static_cast<unsigned int>(time(0)));                //randomly selects weapon from inventory
    	random_shuffle(inventory.begin(), inventory.end());
    	const string WEAPON = inventory[0];
    	cout<<"\n\nYour weapon for the journey is a "<<WEAPON<<".";
    
    	cout<<"\n\n\t\tStart of Game:\n\n";
    	cout<<"Name:"<<name;
    	cout<<"\nAdventurer: "<<player[myPlayer];
    	cout<<"\nWeapon: "<<WEAPON;
    	cout<<"\nHealth: "<<health;
    	cout<<"\nPower: "<<power<<"\n\n";
    
    	int move;
    
    	cout<<"What would you like to do first?\n\n";       //user chooses first action
    	cout<<"                               1 - Rest\n";
    	cout<<"                               2 - Move\n";
    	cout<<"                               3 - Fight\n";
    	cout<<"                               9 - Quit\n";
    	
    	cin>>move;                               
    	myAction = Action(move);  //assigns specific action to enum variable
    
    	int select; //declaring select as int variable for switch loop
    
    	random_shuffle(foe.begin(), foe.end());    //randomly selects foe to interact with
    	const string STRANGER = foe[0];
    	random_shuffle(foe.begin(), foe.end());
    	const string FIGHT = foe[0];
    	
    		switch(myAction)    //output of user choice of action
    		{
    			case Rest:
    				cout<<"\nTime to rest and build your strength.";
    				health +=10;
    				power +=15;
    				success=true;
    				cout<<"\n\nWhat would you like to do next?\n";
    				cout<<"\n                                1-Fight";
    				cout<<"\n                                2-Run";
    				cout<<"\n                                3-Quit\n";
    				cin>>select;
    
    					if (select==1)  //displays consequence of user secondary choice of action
    						{
    							cout<<"You have been wounded.\n\n";
    							cout<<"You lose 10 Health points and 5 Power points.";
    							health-=10;
    							power-=5;
    						}
    					else if(select==2)
    						{
    							cout<<"You have escaped to travel on, but you have used a lot of energy.\n\n";
    							cout<<"Your Power points are decreased by 10.";
    							power-=10;
    						}
    					else if(select==3)
    						{
    							cout<<"Your journey has ended.";
    						}
    					break;
    
    			case Move://output of user choice of action
    			
    			cout<<"\nYou see a "<<STRANGER<<" . ";
    					if(STRANGER=="Troll")
    						{
    							cout<<"It blocks your path.\n";
    							cout<<"\n\nWhat would you like to do next?\n\n";
    							cout<<"                                1-Fight\n";
    							cout<<"                                2-Run\n";
    							cout<<"                                3-Quit\n";
    						}
    					else if(STRANGER=="Dragon")
    						{
    							cout<<"It is coming towards you.\n\n";
    							cout<<"\n\nWhat would you like to do next?\n\n";
    							cout<<"                                1-Fight\n";
    							cout<<"                                2-Run\n";
    							cout<<"                                3-Quit\n";
    						}
    					else if(STRANGER=="Worlock")
    						{
    							cout<<"He is picking herbs.\n\n";
    							cout<<"\n\nWhat would you like to do next?\n\n";
    							cout<<"                                1-Fight\n";
    							cout<<"                                2-Run\n";
    							cout<<"                                3-Quit\n";
    						}
    					else if(STRANGER=="Fairy")
    						{
    							cout<<"She is singing a song.\n\n";
    							cout<<"\n\nWhat would you like to do next?\n\n";
    							cout<<"                                1-Fight\n";
    							cout<<"                                2-Run\n";
    							cout<<"                                3-Quit\n";
    						}
    			cin>>select;
    					if (select==1)//displays consequence of user secondary choice of action
    						{
    							cout<<"You have been wounded.\n\n";
    							cout<<"You lose 10 Health points and 5 Power points.";
    							health-=10;
    							power-=5;
    						}
    					else if(select==2)
    						{
    							cout<<"You have escaped to travel on, but you have used a lot of energy.\n\n";
    							cout<<"Your Power points are decreased by 10.";
    							power-=10;
    						}
    					else if(select==3)
    						{
    							cout<<"Your journey has ended.";
    						}
    					success=false;
    			
    					break;
    
    			case Fight://output of user choice of action
    			
    			cout<<"You fought a "<<FIGHT<<" . ";
    					if(FIGHT=="Troll")
    						{
    							cout<<"\n\nYou have been wounded.\n\n";
    							cout<<"You lose 10 Health points and 5 Power points.";
    							health-=10;
    							power-=5;
    						}
    					else if(FIGHT=="Dragon")
    						{
    							cout<<"\n\nYou have slain the Dragon!  You gain 20 Power points!";
    							power+=20;
    						}
    					else if(FIGHT=="Worlock")
    						{
    							cout<<"\n\nYou have acquired a healing potion. You gain 20 Health points.";
    						}
    					else if(FIGHT=="Fairy")
    						{
    							cout<<"\n\nShe put you in a deep sleep. You lose 25 Health points.";
    							health-=25;
    						}
    					success=true;
    
    			cout<<"\n\nWhat would you like to do next?\n\n";
    			cout<<"                                1-Fight\n";
    			cout<<"                                2-Run\n";
    			cout<<"                                3-Quit\n";
    			cin>>select;
    					if (select==1)//displays consequence of user secondary choice of action
    						{
    							cout<<"You have been wounded.\n\n";
    							cout<<"You lose 10 Health points and 5 Power points.";
    							health-=10;
    							power-=5;
    						}
    					else if(select==2)
    						{
    							cout<<"You have escaped to travel on, but you have used a lot of energy.\n\n";
    							cout<<"Your Power points are decreased by 10.";
    							power-=10;
    						}
    					else if(select==3)
    						{
    							cout<<"Your journey has ended.";
    						}
    					break;
    
    					default:
    							success=false;
    					break;
    		}
    		
    		cout<<"\n\nThanks for playing.";   //displays final data to user
    		cout<<"\n\nYour final score:\n";
    		cout<<"Name: "<<name;
    		cout<<"\nAdventurer: "<<player[myPlayer];
    		cout<<"\nWeapon: "<<WEAPON;
    		cout<<"\nHealth: "<<health;
    		cout<<"\nPower: "<<power;
    
    	int pause;
    	cin>>pause;
    
    	return 0;
    }

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

    Re: C++ Porgram help

    The basic idea is to put a loop around the main switch with the condition test normally true but tests false when a user chooses to terminate. That way the program keeps looping until the user exits.

    Also, you have a couple of simple logic errors which I've highlighted with CAPITAL comments.

    Code:
    #include <iostream>
    #include <string>
    #include <vector>
    #include <algorithm>
    #include <ctime>
    #include <cstdlib>
    
    using namespace std;
    
    int main()
    {
    vector<string> inventory;       //inventory of weapons
    	inventory.push_back("sword");
    	inventory.push_back("bow");
    	inventory.push_back("wand");
    	inventory.push_back("dagger");
    
    vector<string> player;          //player options
    	player.push_back(" ");          //empty place holder to move character names into the #1-4 spot to match enum values
    	player.push_back("Knight");
    	player.push_back("Archer");
    	player.push_back("Sorcerer");
    	player.push_back("Elf");
    
    vector<string> foe;             //foe options
    	foe.push_back("Dragon");
    	foe.push_back("Troll");
    	foe.push_back("Worlock");
    	foe.push_back("Fairy");
    	
    enum Player{Knight=1, Archer, Sorcerer, Elf};   //enum to give the words integer assignments
    enum Action{Rest=1, Move, Fight, Run, Quit=9};
    
    Player myPlayer;     //assign specific variable to my enums        
    Action myAction;
    
    int health = 50;
    int power = 25;
    int choice;
    	
    string name;  //assigned for user name input
    
    	cout<<"\tYour princess has been taken by an evil witch.\n";
    	cout<<"You must travel across the land through the forest,";
    	cout<<"into the caves, and over the marshlands to save her.\n\n";
    	cout<<"What name do you go by?  ";
    	cin>>name;  //user input character name
    
    	cout<<"\n\nVery well "<<name<<" , what type of adventurer are you?\n\n";
    	cout<<"Please select your character type: 1 - Knight\n";
    	cout<<"                                   2 - Archer\n";
    	cout<<"                                   3 - Sorcerer\n";
    	cout<<"                                   4 - Elf\n";
    	cin>>choice;    //user input character persona
    
    bool success;   //test for condition in switch/case
    
    	myPlayer=Player(choice);  //assign user choice to enum variable
    	do       //do loop to execute user choice of character persona
    	{
    		switch (myPlayer)
    		{
    			case Knight:
    				cout<<"You are a brave Knight, "<<name<<".";
    				//myPlayer==Knight; THIS LINE NOT NEEDED
    				success=true;
    				break;
    
    			case Archer:
    				cout<<"You are a deadly Archer, "<<name<<".";
    				success=true;
    				break;
    
    			case Sorcerer:
    				cout<<"Your magic is very powerful, Sorcerer "<<name<<".";
    				success=true;
    				break;
    
    			case Elf:
    				cout<<"You are a sneaky Elf, "<<name<<".";
    				success=true;
    				break;
    
    			default:
    				success=false;
    				cout<<"Please choose another adventurer.";
    				cin>>choice;
    		
    		}
    	}while (success==false); //REQUIRES EQUALITY NOT ASSIGNMENT
    	
    	srand(static_cast<unsigned int>(time(0)));                //randomly selects weapon from inventory
    	random_shuffle(inventory.begin(), inventory.end());
    
    const string WEAPON = inventory[0];
    
    	cout<<"\n\nYour weapon for the journey is a "<<WEAPON<<".";
    
    	cout<<"\n\n\t\tStart of Game:\n\n";
    	cout<<"Name:"<<name;
    	cout<<"\nAdventurer: "<<player[myPlayer];
    	cout<<"\nWeapon: "<<WEAPON;
    	cout<<"\nHealth: "<<health;
    	cout<<"\nPower: "<<power<<"\n\n";
    
    int move;
    
    	cout<<"What would you like to do first?\n\n";       //user chooses first action
    	cout<<"                               1 - Rest\n";
    	cout<<"                               2 - Move\n";
    	cout<<"                               3 - Fight\n";
    	cout<<"                               9 - Quit\n";
    	
    	cin>>move;                               
    	myAction = Action(move);  //assigns specific action to enum variable
    
    int select; //declaring select as int variable for switch loop
    
    
    string STRANGER;
    string FIGHT;
    
    bool terminate = false;
    	
    while (!terminate) {
    	random_shuffle(foe.begin(), foe.end());    //randomly selects foe to interact with
    	STRANGER = foe[0];
    
    	random_shuffle(foe.begin(), foe.end());    //randomly selects foe to interact with
    	FIGHT = foe[0];
    
    	switch(myAction)    //output of user choice of action
    	{
    		case Rest:
    			cout<<"\nTime to rest and build your strength.";
    			health +=10;
    			power +=15;
    			success=true;
    			cout<<"\n\nWhat would you like to do next?\n";
    			cout<<"\n                                1-Fight";
    			cout<<"\n                                2-Run";
    			cout<<"\n                                3-Quit\n";
    			cin>>select;
    
    			if (select==1)  //displays consequence of user secondary choice of action
    			{
    				cout<<"You have been wounded.\n\n";
    				cout<<"You lose 10 Health points and 5 Power points.";
    				health-=10;
    				power-=5;
    			}
    			else if(select==2)
    					{
    					cout<<"You have escaped to travel on, but you have used a lot of energy.\n\n";
    					cout<<"Your Power points are decreased by 10.";
    					power-=10;
    					}
    					else if(select==3)
    						{
    						cout<<"Your journey has ended.";
    						terminate = true;
    						}
    			break;
    
    		case Move://output of user choice of action
    			cout<<"\nYou see a "<<STRANGER<<" . ";
    			if(STRANGER=="Troll")
    			{
    				cout<<"It blocks your path.\n";
    				cout<<"\n\nWhat would you like to do next?\n\n";
    				cout<<"                                1-Fight\n";
    				cout<<"                                2-Run\n";
    				cout<<"                                3-Quit\n";
    			}
    			else if(STRANGER=="Dragon")
    					{
    					cout<<"It is coming towards you.\n\n";
    					cout<<"\n\nWhat would you like to do next?\n\n";
    					cout<<"                                1-Fight\n";
    					cout<<"                                2-Run\n";
    					cout<<"                                3-Quit\n";
    					}
    					else if(STRANGER=="Worlock")
    						{
    						cout<<"He is picking herbs.\n\n";
    						cout<<"\n\nWhat would you like to do next?\n\n";
    						cout<<"                                1-Fight\n";
    						cout<<"                                2-Run\n";
    						cout<<"                                3-Quit\n";
    						}
    						else if(STRANGER=="Fairy")
    							{
    							cout<<"She is singing a song.\n\n";
    							cout<<"\n\nWhat would you like to do next?\n\n";
    							cout<<"                                1-Fight\n";
    							cout<<"                                2-Run\n";
    							cout<<"                                3-Quit\n";
    							}
    			cin>>select;
    			if (select==1)//displays consequence of user secondary choice of action
    			{
    				cout<<"You have been wounded.\n\n";
    				cout<<"You lose 10 Health points and 5 Power points.";
    				health-=10;
    				power-=5;
    			}
    			else if(select==2)
    				{
    					cout<<"You have escaped to travel on, but you have used a lot of energy.\n\n";
    					cout<<"Your Power points are decreased by 10.";
    					power-=10;
    				}
    				else if(select==3)
    					{
    					cout<<"Your journey has ended.";
    					terminate = true;
    					}
    					success=false;
    			break;
    
    			case Fight://output of user choice of action
    			cout<<"You fought a "<<FIGHT<<" . ";
    			if(FIGHT=="Troll")
    			{
    				cout<<"\n\nYou have been wounded.\n\n";
    				cout<<"You lose 10 Health points and 5 Power points.";
    				health-=10;
    				power-=5;
    			}
    			else if(FIGHT=="Dragon")
    				{
    					cout<<"\n\nYou have slain the Dragon!  You gain 20 Power points!";
    					power+=20;
    				}
    				else if(FIGHT=="Worlock")
    						{
    							cout<<"\n\nYou have acquired a healing potion. You gain 20 Health points.";
    							health+=20;	//NEEDED
    						}
    					else if(FIGHT=="Fairy")
    						{
    							cout<<"\n\nShe put you in a deep sleep. You lose 25 Health points.";
    							health-=25;
    						}
    					success=true;
    
    			cout<<"\n\nWhat would you like to do next?\n\n";
    			cout<<"                                1-Fight\n";
    			cout<<"                                2-Run\n";
    			cout<<"                                3-Quit\n";
    			cin>>select;
    			if (select==1)//displays consequence of user secondary choice of action
    			{
    				cout<<"You have been wounded.\n\n";
    				cout<<"You lose 10 Health points and 5 Power points.";
    				health-=10;
    				power-=5;
    			}
    				else if(select==2)
    					{
    					cout<<"You have escaped to travel on, but you have used a lot of energy.\n\n";
    					cout<<"Your Power points are decreased by 10.";
    					power-=10;
    					}
    					else if(select==3)
    						{
    							cout<<"Your journey has ended.";
    							terminate = true;
    						}
    			break;
    
    		default:
    			success=false;
    			break;
    	}
    	}
    
    	cout<<"\n\nThanks for playing.";   //displays final data to user
    	cout<<"\n\nYour final score:\n";
    	cout<<"Name: "<<name;
    	cout<<"\nAdventurer: "<<player[myPlayer];
    	cout<<"\nWeapon: "<<WEAPON;
    	cout<<"\nHealth: "<<health;
    	cout<<"\nPower: "<<power;
    
    int pause;
    	cin>>pause;
    
    	return 0;
    }
    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)

  3. #3
    Join Date
    Oct 2013
    Posts
    5

    Re: C++ Porgram help

    Thank you 2kaud. You are very helpful.

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