Help with getter Function

My get function is returning 0 instead of what i set through user prompt. Is it my set function i coded is in the wrong format?

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
#include <iostream>
#include <string>
#include <sstream>
#include "PointTwoD.h"

using namespace std;

LocationData locationData;
PointTwoD p;
	int xcoord,ycoord,numPlanets,numMoons;
	string sType;
	float partDensity,plasDensity;
PointTwoD::PointTwoD()
{
	set_x(0);
	set_y(0);
	set_civIndex(0.0f);
	
}

PointTwoD::PointTwoD(int coordX,int coordY,float index)
{
	set_x(coordX);
	set_y(coordY);
	set_civIndex(index);
}

PointTwoD::storage()
{

	cout<<p.get_x();
	cout<<p.get_y();
	cout<< locationData.get_sunType();
	cout<< locationData.get_noOfEarthLikePlanets();
	cout<< locationData.get_noOfEarthLikeMoons();
	cout<< locationData.get_aveParticulateDensity();
	cout<< locationData.get_avePlasmaDensity();
	//cout<< xcoord <<" "<<ycoord<<" "<<sType<<" "<<numPlanets<<" "<<numMoons<<" "<<partDensity<<" "<<plasDensity;
}
void PointTwoD::set_x(int cX)
{
	x = cX;
	cout<<x<<endl;
}

void PointTwoD::set_y(int cY)
{
	y = cY;
	cout<<y<<endl;
}

void PointTwoD::set_civIndex(float index)
{
	civIndex = index;
		
}

int PointTwoD::get_x()
{
	cout<<x<<endl;
	return x;
}

int PointTwoD::get_y()
{
	cout<<y<<endl;
	return y;
}

float PointTwoD::get_civIndex()
{
	return civIndex;
}
What's with all those global variables?

How and where is civIndex defined?

Where are you calling your setter function? Perhaps you should show a small complete program so we can see what you're really doing instead of guessing.

Inputdata() is where the user inputs the required data. the code requiring civIndex is not done but it's defined in another .cpp.

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
#include <iostream>
#include <string>

#include "MissionPlan.h"

#include <algorithm>
#include <cctype>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
using namespace std;


MissionPlan::InputData()
{
	PointTwoD point;
	LocationData loca;
	
	int xcoord,ycoord,numPlanets,numMoons;
	float partDensity,plasDensity;
	string sType;
	cout << "[Input statistical data]" << endl;
	cout << "Please enter x-ordinate : " << endl;
	cin>>xcoord;
	
	cout << "Please enter y-ordinate : " << endl;
	cin>>ycoord;
	
	cout << "Please enter sun type : " << endl;
	cin.ignore();
	getline(cin,sType);
	sType.erase(remove_if(sType.begin(),sType.end(),::isspace),sType.end());
	
	cout << "Please enter no.of earth-like planets : " << endl;
	cin>>numPlanets;
	
	cout << "Please enter no.of earth-like moons : " << endl;
	cin>>numMoons;
	
	cout << "Please enter ave. particulate density (%-tage) : " << endl;
	cin>>partDensity;
	
	cout << "Please enter ave. plasma density (%-tage) : " << endl;
	cin>>plasDensity;
	
	point.set_x(xcoord);
	point.set_y(ycoord);
	loca.set_sunType(sType);
	loca.set_noOfEarthLikePlanets(numPlanets);
	loca.set_noOfEarthLikeMoons(numMoons);
	loca.set_aveParticulateDensity(partDensity);
	loca.set_avePlasmaDensity(plasDensity);
	PointTwoD::storage();
	//cout<<xcoord<<ycoord<<sType<<numPlanets<<numMoons<<partDensity<<plasDensity<<endl;
}
Do you realize that both point and loca are variables that are local to this function, when the function returns those variables are destroyed?

so how do i avoid this , do i declare it outside the method?
Yes they will need to be declared outside of this method, probably as MissionPlan member variables.

By the way you haven't answered my questions from my first post.

regarding the first question, is having too many global variables bad? and if you are asking about all the variables on top i moved it back as a local variable i undo too far
regarding the first question, is having too many global variables bad?

Yes, global variables should be avoided whenever possible. And IMO one global variable would probably be too much for this program.

and if you are asking about all the variables on top i moved it back as a local variable i undo too far

Moved back to where?

i moved it into my storage method and use the variables to contain the return values of my get functions
Topic archived. No new replies allowed.