Veribals in class wont output the value it is suposed too.

Hey got this code and trying to make a "Dungion and Dragons" type of game in the console window. And i cant get the start values to output correct.
Can anyone please help me whit this i whode me most Greatfull.
Here is the code:
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
#include "stdafx.h"
#include <iostream>
#include <cstdlib>
#include <string>
#include <ctime>
#include <cmath>

using namespace std;

class character
{
public:

	character(int Starthp, int Startdamege, int StartAgilety, int StartStreght, int Startintelect, int Startxp)
	{
	int hp = Starthp;
	int damege = Startdamege;
	int Agilety = StartAgilety;
	int Streght = StartStreght;
	int Intelect = Startintelect;
	int xp = Startxp;
	}

	void ShowStats(){
		cout << "Your HP is: " << hp << endl;
		cout << "Your Damege is: " << damege << endl;
		cout << "Your Agilety is: " << Agilety << endl;
		cout << "Your Streght is: " << Streght << endl;
		cout << "Your Intelect is: " << Intelect << endl;
		cout << "Your XP is: " << xp << endl;
	}

private:

	int hp;
	int damege;
	int Agilety;
	int Streght;
	int Intelect;
	int xp;

};

//Meny Funksjon
void Meny()
{
	int StatsMeny = 1;
	int ShopMeny = 2;
	int FightMeny = 3;
	int MenyValg;


	cout << "[1] Show Stats: " << endl;
	cout << "[2] Buy Items: " << endl;
	cout << "[3] Go Fight some Monsters: " << endl;
	cout << "[4] Exit" << endl;

	cout << "Enter a Option: ";
	cin >> MenyValg;

// IF Statments For Meny Valg\Input

	if (MenyValg == StatsMeny){
		cout << "Stats:\n" << endl;
		character User1(10,10,10,10,10,0);
		User1.ShowStats();
	}

	if (MenyValg == ShopMeny){
		cout << "Freeky D`s Shop of Everything:\n" << endl;
	}

	if (MenyValg == FightMeny){
		cout << "Fight!\n" << endl;
	}

} //Slutt På Meny

int main()
{
	system("COLOR 20");
	cout << "Welcome to DUNGION Console!\n\n" << endl;

	
	Meny();

	
	system("PAUSE");
	return 0;
}

And the program outputs:
Welcome to DUNGION Console!


[1] Show Stats:
[2] Buy Items:
[3] Go Fight some Monsters:
[4] Exit
Enter a Option: 1
Stats:

Your HP is: -858993460
Your Damege is: -858993460
Your Agilety is: -858993460
Your Streght is: -858993460
Your Intelect is: -858993460
Your XP is: -858993460
Press any key to continue . . .

Compiler VS C++ Express:
1>------ Build started: Project: tut22, Configuration: Debug Win32 ------
1>Compiling...
1>tut22.cpp
1>Linking...
1>Embedding manifest...
1>Build log was saved at "file://d:\KJELL\KODEING\C++\tut22\tut22\Debug\BuildLog.htm"
1>tut22 - 0 error(s), 0 warning(s)
========== Build: 1 succeeded, 0 failed, 0 up-to-date, 0 skipped ==========


Thanks to everyone that takes the time.
And to thos how botherd reading at all.

I am very sorry about my spelling I try my best.
Last edited on
1
2
3
4
5
6
7
8
9
    character(int Starthp, int Startdamege, int StartAgilety, int StartStreght, int Startintelect, int Startxp)
    {
    int hp = Starthp;
    int damege = Startdamege;
    int Agilety = StartAgilety;
    int Streght = StartStreght;
    int Intelect = Startintelect;
    int xp = Startxp;
    }


You are declaring these variables in function scope. drop the int at the beginning of all of them. Basically the values never get assigned to the private data members so you are just outputting junk.

Edit: Also, character User1(10,10,10,10,10,0); is in function scope as well. Try using it again in main or another function and see what happens.
Last edited on
Thanks for your help return 0 aprichiat it!
So i dont think i understod exacely what you ment but. i did get it to work like this.

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
105
106
107
108
109
// tut22.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <cstdlib>
#include <string>
#include <ctime>
#include <cmath>

using namespace std;

class character
{
public:
    character(int Starthp, int Startdamege, int StartAgilety, int StartStreght, int Startintelect, int Startxp)
    {
    int hp = Starthp;
    int damege = Startdamege;
    int Agilety = StartAgilety;
    int Streght = StartStreght;
    int Intelect = Startintelect;
    int xp = Startxp;

    }

		int AddStats()
	{
		hp = 10;
		damege = 10;
		Agilety = 10;
		Streght = 10;
		Intelect = 10;
		xp = 0;
		return 0;
	}
		void LvLup(){

		}

	void ShowStats(){
		
		AddStats();
		cout << "Your HP is: " << hp << endl;
		cout << "Your Damege is: " << damege << endl;
		cout << "Your Agilety is: " << Agilety << endl;
		cout << "Your Streght is: " << Streght << endl;
		cout << "Your Intelect is: " << Intelect << endl;
		cout << "Your XP is: " << xp << endl;
	}


private:

	int hp;
	int damege;
	int Agilety;
	int Streght;
	int Intelect;
	int xp;

};

//Meny Funksjon
void Meny()
{
	int StatsMeny = 1;
	int ShopMeny = 2;
	int FightMeny = 3;
	int MenyValg;


	cout << "[1] Show Stats: " << endl;
	cout << "[2] Buy Items: " << endl;
	cout << "[3] Go Fight some Monsters: " << endl;
	cout << "[4] Exit" << endl;

	cout << "Enter a Option: ";
	cin >> MenyValg;

// IF Statments For Meny Valg\Input

	if (MenyValg == StatsMeny){
		cout << "Stats:\n" << endl;
		character User1(10,10,10,10,10,0);
		User1.ShowStats();
	}

	if (MenyValg == ShopMeny){
		cout << "Freeky D`s Shop of Everything:\n" << endl;
	}

	if (MenyValg == FightMeny){
		cout << "Fight!\n" << endl;
	}

} //Slutt På Meny

int main()
{
	system("COLOR 20");
	cout << "Welcome to DUNGION Console!\n\n" << endl;
	character User1(10,10,10,10,10,0);
	Meny();

	
	system("PAUSE");
	return 0;
}

But i do not know if this is correct .
No, there are still lots of errors in your code.

Variables declared on lines 18-23 hide your private variables declared on lines 55-60.
That is almost always a bad thing.

Please re-read what Return 0 posted carefully.

If you still do not understand scope, you must read some very basic books on C++ before proceeding. If you don't, I guarantee that you will lose a lot of time debugging strange, seemingly unpredictable behavior.
I thot i did understand scope.

if i got int main(){
And make Veribals in there
It whode NOT be posibol to access anything.
{
If i tryd this in here
}
return 0;
}
And the same goas for functions i whode be abel to access this if i use a class.
But i whode hawe to declare a veribal for the class in the main function and in the inner bracket.
Like this: ClassName Veribal_Name_for_object;
and access it by using Veribal_Name_for_object.WhatFunction(); i whode want to use?

This is correct isent it.
And Thanks for your reply kfmfe04.
Topic archived. No new replies allowed.