Can some one check my function Please!

Hi I made a function called inventory for my RPG and it is not working the way I want it to when the user selects a potion to drink. it just out puts "cout<<"Type item name to equip or drink then type 'quit' to leave the inventory:"<<endl; cin>>choice;" 3 times!

Please Help me!
Thank you in advance!

Here are my declarations:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <iostream>
#include <iomanip>
#include <Windows.h>
#include <cstdlib>
#include <ctime>
#include <cmath>
#include <string>

using namespace std;

int go_counter, generate_fighting_phrase,generate_enemy, health_potion_amount, weapon_choice;

int level_char=1;

int human_damage,enemy_damage;

string option,fight;

string name;

string fight_phrase[6]={"you wil regret this!","DIE!","you are no match for me.","you picked the wrong opponent to mess with","today is not your lucky day.","move out of my way!"};

string enemy[10]={"Goblin","Ninja","Black Knight","Evil Rock","Bandet","Baby Dragon","Black Dragon","Mutated Rat","Steel Worm","Evil Midget"};

string weapons[10]={"fist"," "," "," "," "," ", " "," "," "," "};

string potion[10]={"Large Health Potion","Medium Health Potion","Small Health Potion"," "," "," "," "," "," "," "};
int potion_amount[10]={1,0,0};
int level_max_hp[6]={10,15,20,25,30,40};

int enemy_hp[10]={10,15,20,30,40,50,60,70,80,90};

int human_hp;


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
void inventory(){
string choice;
	cout<<"                     Inventory                 "<<endl;
	cout<<"------------------------------------------------------"<<endl;
	cout<<"Available weapons:"<<setw(30)<<"Available Potions:"<< endl;
	for(int counter=0;counter<10;counter++){
		cout<<weapons[counter]<<setw(50)<<potion[counter]<<" x"<<potion_amount[counter]<<endl;
	}

	cout<<"Type item name to equip or drink then type 'quit' to leave the inventory:"<<endl;
	cin>>choice;

	while(choice.compare("quit")!=0){

	    for(int c=0;c<10;c++){
			if(choice.compare(weapons[c])==0){
				weapon_choice=c;
				cout<<weapons[c]<<" was selected."<<endl;
				break;
			}
			else if(choice.compare(potion[c])==0 && potion_amount[c]!=0 ){
			human_hp= level_max_hp[level_char-1];
			cout<<"Health was restored to maximum: "<<level_max_hp[level_char-1];
			potion_amount[c]-=1;
			break;
			
		}

		}
		system("pause");
		system("CLS");
	 cout<<"                     Inventory                 "<<endl;
	cout<<"------------------------------------------------------"<<endl;
	cout<<"Available weapons:"<<setw(30)<<"Available Potions:"<< endl;
	for(int counter=0;counter<10;counter++){
		cout<<weapons[counter]<<setw(50)<<potion[counter]<<" x"<<potion_amount[counter]<<endl;
	}
	cout<<"Type item name to equip or drink then type 'quit' to leave the inventory:"<<endl;
	cin>>choice;
}
	system("CLS");
}
Last edited on
bumb.
As elements of array portion can consist from several words as for example "Large Health Potion" you should use function std::getline instead of operator >>

1
2
	cout<<"Type item name to equip or drink then type 'quit' to leave the inventory:"<<endl;
	getline( cin, choice );


Also it would be much better if the menu were displayed only in the loop.
Oh wow, I completely forgot about that! Thank you very much vlad!
Topic archived. No new replies allowed.