Last piece, looking for statement help

I'm so close. I have everything working up until my input. Code first, explanation to follow:

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
//Battle with Samer
#include <iostream>
#include <string>
#include <sstream>
#include <ctime>
#include <cstdlib>
using namespace std;

struct Opponent{
	string name;
	int health;
	int attack;
};

struct Mystats {
	string name;
	int health;
	int attack;
};

Opponent samer[6] = {
	"Charizard", 20, 10,
	"Umbreon", 15, 10,
	"Mew", 25, 15,
	"Arcanine", 15, 15,
	"Espurr", 10, 15,
	"Luxaray", 10, 10
};

Mystats trainer[3] = {
	"Charizard", 20, 10,
	"Slowking", 30, 6,
	"Weavile", 15, 15
};

int main()
{
	string mystr;
	string Samer;
	string Trainer;
	int samer_choice;
	int player_choice;

	cout << "Musician Samer wants to battle! \n";
	cout << "Go, ";
	srand((unsigned)time(0));
	samer_choice = rand() % 6;
	cout << samer[samer_choice].name << "!" << "\n";
	cout << "Please choose your Pokemon: \n";
	cout << "Press 1 for Charizard \n";
	cout << "Press 2 for Slowking \n";
	cout << "Press 3 for Weavile \n";
	getline(cin, mystr);
	stringstream(mystr) >> player_choice;
	cout << trainer[player_choice-=1].name<< ", I choose you!" << endl ;
	cout << "Would you like to attack? \n";
	cout << "yes or no? \n";
	getline(cin, mystr);
	while (samer[samer_choice].health > 0 && trainer[player_choice].health > 0) {
		//both player alive at this point
		samer[samer_choice].health -= trainer[player_choice].attack;
		trainer[player_choice].health -= samer[samer_choice].attack;
	};

	system("PAUSE");
}


I'm not sure if I need to do an if/else statement (or if i even need the getline). I would like it to be something to the effect of "If (mystr==yes)", run the attack simulation. If not, run it anyway.

Also, i can't seem to figure out how to display the winner of that battle?

Any sort of guidance would be greatly appreciated.
Ok, found out some on my own, having new (related) issue.

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
//Battle with Samer
#include <iostream>
#include <string>
#include <sstream>
#include <ctime>
#include <cstdlib>
using namespace std;

struct Opponent{
	string name;
	int health;
	int attack;
};

struct Mystats {
	string name;
	int health;
	int attack;
};

Opponent samer[6] = {
	"Charizard", 40, 10,
	"Umbreon", 30, 10,
	"Mew", 50, 15,
	"Arcanine", 30, 15,
	"Espurr", 20, 15,
	"Luxaray", 20, 10
};

Mystats trainer[3] = {
	"Charizard", 20, 10,
	"Slowking", 30, 6,
	"Weavile", 15, 15
};

int main()
{
	string mystr;
	string Samer;
	string Trainer;
	int samer_choice;
	int player_choice;

	cout << "Musician Samer wants to battle! \n";
	cout << "Go, ";
	srand((unsigned)time(0));
	samer_choice = rand() % 6;
	cout << samer[samer_choice].name << "!" << "\n";
	cout << "Please choose your Pokemon: \n";
	cout << "Press 1 for Charizard \n";
	cout << "Press 2 for Slowking \n";
	cout << "Press 3 for Weavile \n";
	getline(cin, mystr);
	stringstream(mystr) >> player_choice;
	cout << trainer[player_choice -= 1].name << ", I choose you!" << endl;
	cout << "Would you like to attack? \n";
	cout << "yes or no? \n";
	getline(cin, mystr);
	while (samer[samer_choice].health > 0 || trainer[player_choice].health > 0) {
		//both player alive at this point
		samer[samer_choice].health -= trainer[player_choice].attack;
		trainer[player_choice].health -= samer[samer_choice].attack;
	};
	if (samer[samer_choice].health <= 0) {
		cout << "You are victorious! \n";
	}
	else {
		cout << "You lose! Please play again! \n";
	};

	system("PAUSE");
}


I need the while to loop.......I"m looking back at the statements and flow control section, should I be doing a Do/While loop? Also, maybe it's wrong, but I changed the code (From the first one) to use ||, as opposed to &&.

My understanding was && means when both =0, and || means one or the other.
1
2
while (samer[samer_choice].health > 0 || trainer[player_choice].health > 0) {
		//both player alive at this point 


If you want both players to be greater than 0 (alive), then that needs to be &&. Otherwise one player could have positive health while the other was 0 or negative and the loop would still run.


The difference between do ... while and while is that with do ... while, the statements within the loop are guaranteed to run at least once. With while, if the condition isn't met, the loop won't run at all.
Ok, glad i understood that first part. I want while to happen until one person hits health of <=0.

Is there a piece that I'm missing to get it to run a second time?
I just put in some additional output statements for the health of each player.
I just picked Charizard and chose to attack.

Who is "you" in the You lose! Please play again! message?

Musician Samer wants to battle! 
Go, Mew!
Please choose your Pokemon: 
Press 1 for Charizard 
Press 2 for Slowking 
Press 3 for Weavile 
1
Charizard, I choose you!
Would you like to attack? 
yes or no? 
yes
samer health is 50 trainer health is 20 // before while loop
samer health is 30 trainer health is -10 // after while loop
You lose! Please play again! 
I still can't get samer[samer_choice].name to be victorious (I should lose). What am i missing??!?!?!?!?!?!?!?

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
//Battle with Samer
#include <iostream>
#include <string>
#include <sstream>
#include <ctime>
#include <cstdlib>
using namespace std;

struct Opponent{
	string name;
	int health;
	int attack;
};

struct Mystats {
	string name;
	int health;
	int attack;
};

Opponent samer[6] = {
	"Charizard", 40, 10,
	"Umbreon", 30, 10,
	"Mew", 50, 15,
	"Arcanine", 30, 15,
	"Espurr", 20, 15,
	"Luxaray", 20, 10
};

Mystats trainer[3] = {
	"Charizard", 20, 10,
	"Slowking", 30, 6,
	"Weavile", 15, 15
};

int main()
{
	string mystr;
	string Samer;
	string Trainer;
	int samer_choice;
	int player_choice;

	cout << "Musician Samer wants to battle! \n";
	cout << "Go, ";
	srand((unsigned)time(0));
	samer_choice = rand() % 6;
	cout << samer[samer_choice].name << "!" << "\n";
	cout << "Please choose your Pokemon: \n";
	cout << "Press 1 for Charizard \n";
	cout << "Press 2 for Slowking \n";
	cout << "Press 3 for Weavile \n";
	getline(cin, mystr);
	stringstream(mystr) >> player_choice;
	cout << trainer[player_choice -= 1].name << ", I choose you!" << endl;
	cout << "Enemy Health:" << samer[samer_choice].health << endl;
	cout << "My Health:" << trainer[player_choice].health << endl;
	cout << "Would you like to attack? \n";
	cout << "yes or no? \n";
	getline(cin, mystr);	
	do {
		samer[samer_choice].health -= trainer[player_choice].attack;
		trainer[player_choice].health -= samer[samer_choice].attack;
	}	
	while (samer[samer_choice].health > 0 || trainer[player_choice].health > 0);

	if (samer[samer_choice].health <= 0) {
		cout << samer[samer_choice].name << " has feinted! You are victorious!";
	}
	else {
		cout << trainer[player_choice].name << " has feinted! You are out of useable pokemone!  You blacked out!";
	};

	system("PAUSE");
}
The problem is your while statement at line 65. You want an AND condition here, not an OR condition. You want to continue attacking only as long as BOTH players have health points.

As you have it with an or condition, you continue attacking even if one player is dead. It appears that the user is always victorious because by the time you get to the if statement at line 67, both players are dead and you happen to check if samer is dead first.
......................that was it..........for some reason, was mentally reading it backwards.......................thank you for all of your guidance!!!
Topic archived. No new replies allowed.