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");
}

Hi Em!
Lines 114,119,124,129 - should they be character2.<function> ?
Nick

yes they should well spotted, but it still goes crazy when i swap the order about, i put the character 2 bits quickly on the end to see what affect it had - thanks for your input Nick!

This is a classic problem that comes up almost every day on this forum. The reason for this problem is that when you do a getline, it will get the string up to the first newline (enter stroke) and will delete that newline from the input buffer. But, when you get a numeric variable (int or char or double) it will get the first valid value (ignoring newlines and spaces before) and it will leave the spaces or newlines that are after the numeric value on the input buffer. So after you got a numeric value input, there will be a dangling newline or space character on the input buffer. This will not affect subsequent numeric inputs because they will be ignored and skipped, but it will affect the next string inputs (getline) because the getline will see the newline character on the input buffer and output the string before it (usually empty, or just spaces). To solve the problem, you should add, right after any numeric value input, the line:

cin.ignore(); //this will flush out any dangling newlines on the input buffer.

That will solve the problem for sure.

i just made the corrections. it does the character perfectly but then it goes "Please enter character name: Please enter character description" rather than doing the 2 separately.

Mike
Yes thats it!! Thank you soo much - will definitely keep note of this for future reference! Quite surprised its not mentioned in any of the tutorials i have been reading - i think i did something of with a similar theory when doing C
Thanks again!
Emily

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.