Classes, functions

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
102
103
104
#include "stdafx.h"
#include <iostream>
#include <string>

using namespace std;
class Goblin
{
public:
	Goblin(unsigned short StartHealth, unsigned short StartStrength, unsigned short Attack, unsigned short Magic);
	void BeAttacked();
	void Attack();
	void Magic();
	int GetHealth();
	void IncreaseHealth();
	void DecreaseHealth(unsigned short damage);
	void NewGame();
	void Rules();
	void Quit();

private:
	int health;
	int strength;
	int magic;
	int attack;
};
int main()
{
	Goblin John(12,10,10,10);
	int menuchoice;
	cout << "Main Menu\n";
	cout << "1. New Game\n";
	cout << "2. Rules\n";
	cout << "3. Quit\n";
	cin >> menuchoice;

	switch (menuchoice)
	{
	case 1:
		John.NewGame();
		break;
	case 2:
		John.Rules();
		break;
	case 3:
		John.Quit();
		break;
	}
	int attack;
	cout << "Attack using magic or strength?:";
	cin >> attack;
	
	switch (attack)
	{
	case 1:
		John.Magic();
		break;
	case 2:
		John.Attack();
		break;
	}
		
}
Goblin::Goblin(unsigned short StartHealth,unsigned short StartStrength,unsigned short Attack, unsigned short Magic):
	 health(StartHealth),
	 strength(StartStrength)
	 {
	 }
	 void Goblin::Attack()
	 {
		 cout << "I'm attacking with " << strength << " strength and " << attack << "attack!\n";
	 }
	 void Goblin::BeAttacked()
	 {
		 cout << "Aaaarggghh\n";
		 DecreaseHealth(2);
	 }
	 void Goblin::DecreaseHealth(unsigned short Damage)
	 {
		 health = health - Damage;
		 cout << "You took " << Damage << "damage!\n";
	 }
	 int Goblin::GetHealth()
	 {
		 return health;
	 }
	 void Goblin::Magic()
	 {
		 cout << "You attacked using " << magic << "magic!\n";
	 }
	 void Goblin::NewGame()
	 {
		 string character;
		 cout << "New game!\n";
		 cin >> character;
		 string * name = &character;
	 }
	 void Goblin::Quit()
	 {
		 cout << "You quit\n";
	 }
	 void Goblin::Rules()
	 {
		 cout << "Rules\n";
	 }



No compiler or runtime errors, however when I run and go to the function NewGame, it does not cout, and only takes the cin.

When I try to attack using magic it gives me a negative integer such as -8456864 for magic level...Same thing for attack level if I attack using strength.

try cout << flush; (need to #include <iomanip>

You don't initialize attack or magic in the constructor, so they have garbage.
Topic archived. No new replies allowed.