Hi
I have just written this code (am just starting out with c++ so took a while :s) Works fine.... untill character 2 is created. When the program is run the character 1 is fine and every time the users is asked "Please enter x" they can and it is stored correctly. However when character 2 is created the questions are ask but the en duser isnt given the opportunity to enter anything and when the content is printed to screen its full of random numbers. From experimenting, i found that it works fine if in main, the strings are done firth then int, if it is done the other way round e.g int string int e.t.c - then the program displays the questions one after the other without letter the user input. I have no idea what at all, i cant find anything like this on the internet.
Any help much appreciated !!
#include <iostream.h>
#include <string.h>
using namespace std;
class characters //name of class
{
private:
int expPoints;
int location;
string charName;
string description;
public:
characters(); //A constructor function - must be same name as class itself
int getPoints (); // An Accessor for expPoints
void setPoints(int expPoints); // A mutator function for expPoints
int getLocation();
void setLocation (int location);
string getName(); // An accessor function for name
void setName(string name); // An mutator function for name
string getDescription(); // Accessor function for description
void setDescription (string chardescription); // A mutator function for description
};
////////////////////////////////////////////////////////////////////////////////
/// constructor function //
characters::characters()
{
int expPoints=0;
cout << "A character has been born!" << endl;
}
/// Accessor Function for expPoints ///
int characters::getPoints()
{
return expPoints;
}
// Mutator function for expPoints ///
void characters::setPoints(int points)
{
expPoints=points;
}
/// Accessor Function for location ///
int characters::getLocation()
{
return location;
}
// Mutator function for location ///
void characters::setLocation(int location)
{
location=location;
}
/// Accessor function for name /// need accessor for when we recall the data later in the "test harness"
string characters::getName()
{
return charName;
}
/// Mutator fuction for name ///
void characters::setName(string name)
{
charName=name;
}
/// Accessor function for description ///
string characters::getDescription()
{
return description;
}
/// Mutator function for description ///
void characters::setDescription (string chardescription)
{
description=chardescription;
}
////////////////////////////////////////////////////////////////////////////////
int main(void)
{
characters character1; // creates object called character1
string mystring;
cout << "Enter the characters name: ";
getline (cin, mystring); // gets entire line entered by end user
character1.setName(mystring);
string description;
cout << "Enter a description for the character: ";
getline (cin,description);
character1.setDescription(description);
int number;
cout << "Enter the experience points of the character: ";
cin >> number;
character1.setPoints(number);
int location;
cout << "Enter the location of the character: ";
cin >> location;
character1.setLocation(location);
/////////////////////////////////////////////////////////////////////////////
cout << "value in name is " << character1.getName() << endl;
cout << "value in expPoints is " << character1.getPoints() << endl;
cout << "Value in description is " << character1.getDescription() <<endl;
cout << "Value in location is " << character1.getLocation() <<endl;
characters character2; // creates object called character1
string mystring2;
cout << "Enter the characters name: ";
getline (cin, mystring2); // gets entire line entered by end user
character1.setName(mystring2);
string description2;
cout << "Enter a description for the character: ";
getline (cin,description2);
character1.setDescription(description2);
int number2;
cout << "Enter the experience points of the character: ";
cin >> number;
character1.setPoints(number2);
int location2;
cout << "Enter the location of the character: ";
cin >> location;
character1.setLocation(location2);
cout << "value in name is " << character2.getName() << endl;
cout << "value in expPoints is " << character2.getPoints() << endl;
cout << "Value in description is " << character2.getDescription() <<endl;
cout << "Value in location is " << character2.getLocation() <<endl;
system("pause");
}