Help with custom function

Why does the Classe string at line 23 output | ?

main

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
#include <iostream>
#include <string>//char in stringhe
#include "Classi.cpp"
using namespace std;

main()
{
	int Hp,Atk,Def;
	string Nome,Classe;
	char Scelta;
	
	system("COLOR 0A");
	cout<<"Gioco Rpg con solo testo"<<endl;
	cout<<"Inserisci il nome del tuo personaggio:";
	cin>>Nome;
	cout<<"Scegli una classe:"<<endl;
	cout<<"Guerriero: 60Hp,15Atk,13Def"<<endl;
	cout<<"Rouge:     50Hp,12Atk,10Def"<<endl;
	cout<<"Mago:      40Hp,18Atk,7Def"<<endl;
	
	Classi:
	ClassiScelta(Hp,Atk,Def,Scelta,Classe);
	cout<<Nome<<"|"<<Classe<<"|Hp"<<Hp<<"|Atk"<<Atk<<"|Def"<<Def<<endl;
	
	system("PAUSE");
}


function

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
43
44
45
#include <iostream>
#include <string>//char in stringhe
#include <ctype.h>//non case sensitive
using namespace std;

void ClassiScelta(int &Hp,int &Atk,int &Def,
				  char Scelta,string Classe)
{
		Classi:cin>>Scelta;
		switch(toupper(Scelta))
	{
		case 'G':
			{
				Classe="Guerriero";
				Hp=60.0;
				Atk=15.0;
				Def=13.0;
			}
		break;
		
		case 'R':
			{
				Classe="Rouge";
				Hp=50.0;
				Atk=12.0;
				Def=10.0;
			}
		break;
		
		case 'M':
			{
				Classe="Mago";
				Hp=40.0;
				Atk=18.0;
				Def=7.0;
			}
		break;
		
		default:cout<<"Scegli una classe valida!"<<endl;		
		goto Classi;	
	}
	system("CLS");
	
	
}
Last edited on
> Why does the Classe string at line 23 output | ?
void ClassiScelta(int &Hp,int &Atk,int &Def, char Scelta,string Classe)
¿why do you pass `Hp', `Atk', `Def', by reference?
Forgot to remove them while I was trying to find a solution, sounds stupid I know XD.
¿remove them? you do need to pass them by reference because you want the function to modify them, as you also want to modify `Classe'
so you should pass `Classe' by refernce.
oid ClassiScelta(int &Hp,int &Atk,int &Def, char Scelta, string &Classe)


by the way, you shouldn't include *.cpp files.
instead include the .h where there are the prototypes and link the .cpp to your project
http://www.cplusplus.com/forum/general/140198/
http://www.cplusplus.com/forum/general/113904/#msg622055
Thanks for the help, now it works perfectly :).
Topic archived. No new replies allowed.