If statement not working as intended

In the third sections the ifs are intended to select a monster depending on the modulo of rng, but if d1=1 and d2=1 it selects Goblin instead of Rat.

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
27
28
29
30
31
32
#include <iostream>
#include <string>//char in stringhe
#include "classi.cpp"
#include "enemy.cpp"
using namespace std;

int main()
{
	int Hp,Atk,Def,eHp,eAtk,eDef,D1,D2,Rng;
	string Nome,Classe,eName;
	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<<"Rogue:     50Hp,12Atk,10Def"<<endl;
	cout<<"Mago:      40Hp,18Atk,7Def"<<endl;
	
	Classi://per default in classi.cpp
	ClassiScelta(Hp,Atk,Def,Scelta,Classe);
	cout<<Nome<<"|"<<Classe<<"|Hp"<<Hp<<"|Atk"<<Atk<<"|Def"<<Def<<endl;
	
	EnemySpawn(eHp,eAtk,eDef,eName,D1,D2,Rng);
	cout<<eName<<"|Hp"<<eHp<<"|Atk"<<eAtk<<"|Def"<<eDef<<endl;
	
	system("PAUSE");
}


player
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="Rogue";
				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");
	
	
}



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

void EnemySpawn(int &eHp,int &eAtk,int &eDef,
                string &eName,int &D1,int &D2,int &Rng)
            
{
	cout<<"Lancia due dadi da 6 e insrisci i risultati"<<endl;
	cin>>D1>>D2;
	Rng=D1+D2;	
		
	if(Rng<=4)
	{
		eName="Rat";
		eHp=20.0;
		eAtk=3.0;
		eDef=2.0;
	}
			
	if((Rng<=8)&&(Rng>4))	
	{
		eName="Goblin";
		eHp=40.0;
		eAtk=6.0;
		eDef=5.0;
	}
			
	if(Rng>8)
	{
		eName="Mutant";
		eHp=50.0;
		eAtk=8.0;
		eDef=7.0;
	}

		
}
Last edited on
closed account (E0p9LyTq)
Changing srand() to std::srand() and rand() to std::rand() fixes the errors in Visual Studio 2017.

Also recommended to include <cstdlib> when using std::srand() and std::rand().
Last edited on
Didn't work, thanks anyways.
You are generating a random int (line 12) taking values 1, 2, 3

Your switch block is using char, taking values '1', '2', '3'.

A char is not an int.

Still not working, but thanks for the heads up.
Sorry you are right, posted full code
You are still basing your switch cases in "enemies" on '1', '2', '3', not 1,2,3 - they are different things.

You appear to have variables called srand and rand - these are standard library functions: anything could happen (none of it likely to be good).

You are including .cpp files - somebody warned you about this in a previous thread.

All sorts of things require you to include <cstdlib>.

Resolved all issues, thread closed :).
Topic archived. No new replies allowed.