Help with getter Function
Oct 16, 2016 at 11:14am UTC
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;
}
Oct 16, 2016 at 12:52pm UTC
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.
Oct 16, 2016 at 1:10pm UTC
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;
}
Oct 16, 2016 at 1:17pm UTC
Do you realize that both point and loca are variables that are local to this function, when the function returns those variables are destroyed?
Oct 16, 2016 at 1:19pm UTC
so how do i avoid this , do i declare it outside the method?
Oct 16, 2016 at 1:29pm UTC
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.
Oct 16, 2016 at 1:34pm UTC
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
Oct 16, 2016 at 1:39pm UTC
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?
Oct 16, 2016 at 2:06pm UTC
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.