rpg

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
int main(){
	int c;
	srand(time(0));					//h   //s  //d  //x
	
	Character player1("Warrior",      90,  45,  15, 40);
	Character player2("Tank",         100, 20,  20, 30);
	Character player3("Marksman",     50,  50,  2,  80);
	Character player4("Mage",         60,  35,  10, 60);
	Character player5("Demon Hunter", 80,  30,  18, 75);
	Character player6("Speedster",    70,  25,  5,  100);
	
	
	do{
		cout << "(1)\n***************\nThe Warrior:\n attack - 45\n defense - 15\n health - 90\n speed - 40\n***************" << endl << endl;
		cout << "(2)\n***************\nThe Tank:\n attack - 20\n defense - 20\n health - 100\n speed - 30\n***************" << endl << endl;
		cout << "(3)\n***************\nThe Marksman:\n attack - 50\n defense - 2\n health - 50\n speed - 80\n***************" << endl << endl;
		cout << "(4)\n***************\nThe Mage:\n attack - 35\n defense - 10\n health - 60\n speed - 60\n***************" << endl << endl;
		cout << "(5)\n***************\nThe Demon Hunter:\n attack - 30\n defense - 18\n health - 80\n speed - 75\n***************" << endl << endl;
		cout << "(6)\n***************\nThe Speedster:\n attack - 25\n defense - 5\n health - 70\n speed - 100\n***************" << endl << endl;
		cout << "Which Class would you like to be?: ";
		cin >> c;
		if(c> 6)
		{
			system("cls");
			cout << "You must enter an integer between 1 and 6!!!!\n\n";
			system("pause");
			system("cls");
		}
		
	}while(c > 6);
	
	
	cout<<endl;
	
	if (c==1)	
		cout<<"You have selected The Warrior class.";	
	if (c==2)	
		cout<<"You have selected The Tank class.";
	if (c==3)	
		cout<<"You have selected The Marksman class.";
	if (c==4)	
		cout<<"You have selected The Mage class.";
	if (c==5)	
		cout<<"You have selected The Demon Hunter class.";
	if (c==6)	
		cout<<"You have selected The Speedster class.";
		
	cout<<endl;
		

	
	while(player1.getHealth() > 0 and player2.getHealth() > 0) {
	player2.takeDamage(player1.attack());
	        viewBattleStats(player1, player2);
	
	        do {
	            cout << '\n' << "Press the Enter key to continue.";
	        } while (cin.get() != '\n');
	
	        player1.takeDamage(player2.attack());
	        viewBattleStats(player1, player2);
	
	        do {
	            cout << '\n' << "Press the Enter key to continue.";
	        } while (cin.get() != '\n');
	    }



}

instead of while(player1.getHealth() > 0 and player2.getHealth() > 0) how do i get player1.getHealth changed into the user choice? (1-6)
assume classes are defined
Last edited on
Can you make the question more detailed? What do you mean change it in the user choice? If you want it to do lines 52 - 66 in those if statements you have, then you can make it into a function and call it:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
void attack()
{
while(player1.getHealth() > 0 and player2.getHealth() > 0) {
	player2.takeDamage(player1.attack());
	        viewBattleStats(player1, player2);
	
	        do {
	            cout << '\n' << "Press the Enter key to continue.";
	        } while (cin.get() != '\n');
	
	        player1.takeDamage(player2.attack());
	        viewBattleStats(player1, player2);
	
	        do {
	            cout << '\n' << "Press the Enter key to continue.";
	        } while (cin.get() != '\n');
	    }
}


1
2
3
4
5
	if (c==(whatever))
       {
		cout<<"You have selected The _______ class.";
                attack();
       }


Obviously this code wont work without some tweaks, but its just to show you the basic idea if this is what you meant
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
while(player1.getHealth() > 0 and player2.getHealth() > 0) {
	player2.takeDamage(player1.attack());
	        viewBattleStats(player1, player2);
	
	        do {
	            cout << '\n' << "Press the Enter key to continue.";
	        } while (cin.get() != '\n');
	
	        player1.takeDamage(player2.attack());
	        viewBattleStats(player1, player2);
	
	        do {
	            cout << '\n' << "Press the Enter key to continue.";
	        } while (cin.get() != '\n');
	    }


This is the code for a battle sequence. This code defaults to warrior and tank beacause warrior is 1 and tank is 2. I want the 1st line to have the userchoice in it, which is variuable c.
But if i do while(cgetHealth() > 0 and player2.getHealth() > 0) that obviusly doesnt work.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
	Character player;
	if (c==1)	
	{
		cout<<"You have selected The Warrior class.";	
		player = player1;
	}
	if (c==2)	
	{
		cout<<"You have selected The Tank class.";
		player = player2;
	}
	if (c==3)
	{
		cout<<"You have selected The Marksman class.";
		player = player3;
	}
	...

	while(player.getHealth() > 0 and player2.getHealth() > 0) {
		 ...
	}


Like this?
You need to look into arrays (or better yet std::vector).

Your collection of characters could be represented as follows. Note, indexes in an array range from 0 to (size - 1). So, player[6] is out of range in this case. Also note that I did not try to compile this, so there might be a typo or two.

1
2
3
4
5
6
7
	Character player[6];
	player[0] = Character("Warrior",      90,  45,  15, 40);
	player[1] = Character("Tank",         100, 20,  20, 30);
	player[2] = Character("Marksman",     50,  50,  2,  80);
	player[3] = Character("Mage",         60,  35,  10, 60);
	player[4] = Character("Demon Hunter", 80,  30,  18, 75);
	player[5] = Character("Speedster",    70,  25,  5,  100);


Now, whenever you want to access the "Tank", you can use player[2] instead of your variable "player3".
Much better than my workaround.
I am required to use classes :(
Thanks though.
If I did use arrays for the heroes, it can also be
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <cstdlib>
#include <time.h>
using namespace std;

int main(){
string l[]={"a","b","c"};
string choicename;

cout<<"choose a hero :a b c"<<endl;
cin>>choicename;

string lname,xname,fname;
lname = l[0];
xname = l[1];
fname = l[2];
if (choicename == lname)
cout<<"yes";
else 
cout<<"no";
}
Last edited on
Another problem:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
	do{
	cout << "(1)\n***************\nThe Warrior:\n attack - 45\n defense - 15\n health - 90\n speed - 40\n***************" << endl << endl;
	cout << "(2)\n***************\nThe Tank:\n attack - 20\n defense - 20\n health - 100\n speed - 30\n***************" << endl << endl;
	cout << "(3)\n***************\nThe Marksman:\n attack - 50\n defense - 2\n health - 50\n speed - 80\n***************" << endl << endl;
	cout << "(4)\n***************\nThe Mage:\n attack - 35\n defense - 10\n health - 60\n speed - 60\n***************" << endl << endl;
	cout << "(5)\n***************\nThe Demon Hunter:\n attack - 30\n defense - 18\n health - 80\n speed - 75\n***************" << endl << endl;
	cout << "(6)\n***************\nThe Speedster:\n attack - 25\n defense - 5\n health - 70\n speed - 100\n***************" << endl << endl;
	cout << "Which Class would you like to be?: ";
	cin >> c;
	if(c> 6)
	{
	system("cls");
	cout << "You must enter an integer between 1 and 6!!!!\n\n";
	system("pause");
	system("cls");
	}
	
	}while(c > 6);


When the user enters a word or letter, the program gets stuck in an infinite loop. This works fine with numbers though. How to prevent the user from entering a word and breaking the program?
@doug4
cant use arrays because health, attacking, taking damage are class functions.
@ganado
id returned 1 exit status
error!
Is "Character" not a class? player in his example is an array of Character.
You can use class functions with class objects in arrays...
cant use arrays because health, attacking, taking damage are class functions.


To add to what @Ganado said... Huh????

In my post, player is an array of Character objects, and Character is a class. Arrays and classes are 2 different things that are not mutually exclusive. It would even be possible, if you wanted to (although not likely efficient), to have another class that contains an array of Character objects.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
If I did use arrays for the heroes, it can also be

#include <iostream>
#include <cstdlib>
#include <time.h>
using namespace std;

int main(){
string l[]={"a","b","c"};
string choicename;

cout<<"choose a hero :a b c"<<endl;
cin>>choicename;

string lname,xname,fname;
lname = l[0];
xname = l[1];
fname = l[2];
if (choicename == lname)
cout<<"yes";
else 
cout<<"no";
}


I'm not sure what point you are trying to make. What (mis)understanding of arrays are you trying to demonstrate here?
- Arrays can only be used for strings?
- Arrays can only be used to contain names for a specific hero?
- Arrays can only be used for "simple" data types? (note: string is actually a class and not a simple data type)
- Arrays can only be used to define fields that you didn't mention in the initial question?

Instead of telling us that arrays can't be used, why don't you explain to us why you think there is a problem and we can help you figure out how they can be applied. Do you think it's possible for an experienced programmer to have a better understanding of how the language works than a beginner who is confused by a class assignment? We want to help you learn C++, but you have to trust that we have some idea what we are talking about (especially in the basic, foundational stuff).

When the user enters a word or letter, the program gets stuck in an infinite loop. This works fine with numbers though. How to prevent the user from entering a word and breaking the program?


When cin >> c; is executed, because c is an integer, anything not looking like an integer remains in the input stream. So next time through the loop, the same input is there, and nothing is read (returning '0'). You need to perform an ignore on the input stream to flush all of the content. Try something like cin.ignore(256, '\n'); This will ignore up to 256 characters or until it reaches a new line character. You can bullet-proof your code by using max int or max cin buffer size if you want to, but some sufficiently large number will work for your current class project.
Topic archived. No new replies allowed.