function in a class returning junk

this is a piece of code from my program for a game of quiz...this class is supposed to manage the marks of a player. i am passing marks for a question depending on its correctness to the setscore function and i want it to keep on updating the value of m_score so that finally i can return it from the getscore function....with this piece of code i am getting some junk values being returned by the getscore function . can someone help me in figuring this out????
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
class Player
{
private:
	char* m_name;
	int m_score;

public:
	friend class Scoremanager;
	Player()
	{
		m_name = " ";
		m_score = 0;
	}
	void setscore(int score)
	{
		m_score += score;
	}
	void setname(char* name)
	{
		m_name = name;
	}
	int getscore()
	{
		return m_score;
	}
	char* getname()
	{
		return m_name; 
	}
};
Show usage of said class.
it is generating a random no and based on that goes to a file and asks questions out of the file..."marks" is being returned by the o_SciTech.showAnswer statement....
1
2
3
4
5
6
7
8
9
int Q_Rand = (rand()%(max-min + 1)) + min;
				main_quesfname = o_SciTech.quesfilename();
				main_ansfname = o_SciTech.ansfilename();
				o_SciTech.showQuestion(Q_Rand,main_quesfname);
				cout << "what is your answer a, b, c or d " << endl;
				cin >> ans;
				marks = o_SciTech.showAnswer(Q_Rand,ans,main_quesfname,main_ansfname);
				player[main_playerno].setscore(marks);
				cout << "score till now = " << player[main_playerno].getscore() << endl;
Check what does marks equals to just before adding it to player score.
(cout << marks between lines 7 and 8)
i checked it....the value of marks shows 10 (since i have decided +10 and -5)
the above code runs in a loop to continuously ask questions btw...
Last edited on
Try to create minimal sample case: code which compiles, have reproduceable bug you trying to eliminate and does not use any files. Then post it here or on ideone.
i made this code and found the mistake in the other one as this piece of code was working fine..thank u very much...
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
#include "stdafx.h"
#include<iostream>
using namespace std;

class Player
{
private:
	char* m_name;
	int m_score;

public:
	friend class Scoremanager;
	Player()
	{
		m_name = " ";
		m_score = 0;
	}
	void setscore(int score)
	{
		m_score += score;
	}
	void setname(char* name)
	{
		m_name = name;
	}
	int getscore()
	{
		return m_score;
	}
	char* getname()
	{
		return m_name; 
	}
};

int main()
{
	int marks=0;
	Player player ;

	
			for(int i=1; i<10; i++)
			{
				marks = 10;
				cout << " * " << marks;
				player.setscore(marks);
				cout << "score till now = " << player.getscore() << endl;
				//player[p].setscore(pass_total());
			}

			return 0;
}
Topic archived. No new replies allowed.