Output 3 times more than what i tell it to..

closed account (LAfSLyTq)
Why is this code outputting my level 3 times after the battlePhase ends?

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
46
47
void battlePhase(Player1 &player, Boar1 &boar)
{
cout <<"You have run into a Boar!\nWhat do you want to do?\n1=Attack "<<endl;
cin >> Action;

if ( Action == '1' || Action == 'i' )
{
system("cls");
cout<<name<<" attacked enemy for "<<player.atk<< endl;
boar.health=boar.health-player.atk;
cout<<"enemy took "<<player.atk<<" damage!";
system("pause");

if(boar.health==0)
{
cout<<"The boar has died!"<<endl;
cout<<"you have gained "<<boar.exp<<" exp!"<<endl;
system("pause");
system ("CLS");
player.exp += boar.exp;
return;
}
system("cls");
cout<<"Boar attacked for "<<boar.atk<< endl;
player.health=player.health-boar.atk;
cout<<"you took "<<boar.atk<<" damage!";
system("pause");
battlePhase(player,boar);

if(player.health == 0)
{
cout<<"you have died\nGAME OVER!"<<endl;
return;
system ("pause");
}

if(player.exp>=0 && player.exp<99)        player.level=1;
if(player.exp>=100 && player.exp<199)    player.level=2;
if(player.exp>=200 && player.exp<299)    player.level=3;
if(player.exp>=300 && player.exp<399)    player.level=4;
if(player.exp>=400 && player.exp<499)    player.level=5;
if(player.exp>=500 && player.exp<999)    player.level=6;
if(player.exp>=1000 && player.exp<1999)    player.level=7;
cout<<"You are level "<<player.level<<" at the moment."<<endl; //repeats 3 times?
system ("pause");
}
}
You are sure that there is nowhere else in your program that you are printing "You are level x at the moment."?
closed account (LAfSLyTq)
yes, i am sure.
Can I see the all the code around the places you call battlePhase()?
closed account (LAfSLyTq)
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#include <iostream>
#include <string>
#include <ctime>
#include <cstdlib>
using namespace std;

	char name[10];
	char Action;

class Boar1;
class Player1;
void battlePhase(Player1 &player, Boar1 &boar);

class Player1
{
public:
    int health, exp, atk, level;
    Player1 ()
    {
        health = 20;
        level = 1;
        exp = 0;
        atk = 5;
    }
};


class Boar1
{
public:
    int health, level, atk, exp;
    Boar1 ()
    {
		exp = 75;
        health = 20;
        level = 1;
        atk = 5;
    }
};

class Gunthor
{
public:
	int health, level, atk, exp;
	Gunthor()
	{
		exp = 400;
		health = 140;
		level = 7;
		atk = 35;
	}

};

void battlePhase(Player1 &player, Boar1 &boar)
{
cout <<"You have run into a Boar!\nWhat do you want to do?\n1=Attack "<<endl;
cin >> Action;

if ( Action == '1' || Action == 'i' )
{
system("cls");
cout<<name<<" attacked enemy for "<<player.atk<< endl;
boar.health=boar.health-player.atk;
cout<<"enemy took "<<player.atk<<" damage!";
system("pause");

if(boar.health==0)
{
cout<<"The boar has died!"<<endl;
cout<<"you have gained "<<boar.exp<<" exp!"<<endl;
system("pause");
system ("CLS");
player.exp += boar.exp;
return;
}
system("cls");
cout<<"Boar attacked for "<<boar.atk<< endl;
player.health=player.health-boar.atk;
cout<<"you took "<<boar.atk<<" damage!";
system("pause");
battlePhase(player,boar);

if(player.health == 0)
{
cout<<"you have died\nGAME OVER!"<<endl;
return;
system ("pause");
}

if(player.exp>=0 && player.exp<99)        player.level=1;
if(player.exp>=100 && player.exp<199)    player.level=2;
if(player.exp>=200 && player.exp<299)    player.level=3;
if(player.exp>=300 && player.exp<399)    player.level=4;
if(player.exp>=400 && player.exp<499)    player.level=5;
if(player.exp>=500 && player.exp<999)    player.level=6;
if(player.exp>=1000 && player.exp<1999)    player.level=7;
cout<<"You are level "<<player.level<<" at the moment."<<endl;
system ("pause");
}
}
Er, no, when you say:

1
2
Player1 player;
Boar1 the_boar;
 
battlePhase( player, the_boar );  // this is a "function call" 
It prints three times because you call battlePhase() three times.

In line 82, battlePhase() calls itself.
Last edited on
closed account (LAfSLyTq)
if it doesnt call itself there then the battlePhase wont repeat, and i dont know how to work with loops other than that.
Maybe do this then.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
void battlePhase(Player1 &player, Boar1 &boar, bool printLvlMsg=true)
{

//...

battlePhase(player,boar,false); // message won't be printed in these calls

//...

if(printLvlMsg)
{
    cout<<"You are level "<<player.level<<" at the moment."<<endl;
    system ("pause");
}

}
}


EDIT:

Maybe a better way is to move all the following into the if boar.health==0 branch before return since it makes sense to show it when the enemy dies.

1
2
3
4
5
6
7
8
9
if(player.exp>=0 && player.exp<99)        player.level=1;
if(player.exp>=100 && player.exp<199)    player.level=2;
if(player.exp>=200 && player.exp<299)    player.level=3;
if(player.exp>=300 && player.exp<399)    player.level=4;
if(player.exp>=400 && player.exp<499)    player.level=5;
if(player.exp>=500 && player.exp<999)    player.level=6;
if(player.exp>=1000 && player.exp<1999)    player.level=7;
cout<<"You are level "<<player.level<<" at the moment."<<endl;
system ("pause");


The call to battlePhase() within itself should be moved to after you check player health since no point calling battlePhase() if player is dead. I think the way you do it means player never dies.

Last edited on
Dang, I was looking for that there on line 82 and missed it about 3 times... Goes to show that when you are tired stuff swims in front of you...
closed account (LAfSLyTq)
this is nice and all, but now it repeats the system("pause"); 3 times like this...

"Press any key to continue...
You are level 1 at the moment.
Press any key to continue...
Press any key to continue..."
closed account (LAfSLyTq)
also, i tried moving the level system into the boar.health == 0 part and it doesnt display sooo.. but it does fix the 3x output thing.
If you move the level system into boar.health ==0 part you don't need the other change with the printLvlMsg flag. Make sure the call in battlePhase to battlePhase doesn't have the false argument.

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
46
47
48
49
50
51
52
void battlePhase(Player1 &player, Boar1 &boar)
{
  system("cls");
  cout <<"You have run into a Boar!\nWhat do you want to do?\n1=Attack "<<endl;
  cin >> Action;
  
  if ( Action == '1' || Action == 'i' )
  {
    system("cls");
    cout<<name<<" attacked enemy for "<<player.atk<< endl;
    boar.health=boar.health-player.atk;
    cout<<"enemy took "<<player.atk<<" damage!"<<endl;
    system("pause");
    
    if(boar.health==0)
    {
      system ("CLS");
      cout<<"The boar has died!"<<endl;
      cout<<"you have gained "<<boar.exp<<" exp!"<<endl;
      system("pause");
      player.exp += boar.exp;
      if(player.exp>=0 && player.exp<99)        player.level=1;
      if(player.exp>=100 && player.exp<199)    player.level=2;
      if(player.exp>=200 && player.exp<299)    player.level=3;
      if(player.exp>=300 && player.exp<399)    player.level=4;
      if(player.exp>=400 && player.exp<499)    player.level=5;
      if(player.exp>=500 && player.exp<999)    player.level=6;
      if(player.exp>=1000 && player.exp<1999)    player.level=7;

      system("cls");
      cout<<"You are level "<<player.level<<" at the moment."<<endl; //repeats 3 times?
      system ("pause");
      return;
    }
    system("cls");
    cout<<"Boar attacked for "<<boar.atk<< endl;
    player.health=player.health-boar.atk;
    cout<<"you took "<<boar.atk<<" damage!"<<endl;
    system("pause");
    
    if(player.health == 0)
    {
      system("cls");
      cout<<"you have died\nGAME OVER!"<<endl;
      system ("pause");
      return;
    }
    
    battlePhase(player,boar);

  }
}
Last edited on
Topic archived. No new replies allowed.