I am practicing classes and I am trying to allow the user to enter its name using a space. When the user enters a space, my output is "garbage". Is it possible to use the get.line function with a class? This is what I have.
//Practicing classes.
#include <iostream>
#include <string>
using namespace std;
//Class decleration
class person
{
//Access Specifier
public:
string name; //Member Vairable Decleration
int number; //Member Function Decleration
};
//Main function.
int main()
{
//Object creation from class.
person obj;
//Get input Values for object Variables
cout<<"Enter the Name: \n";
cin>>obj.name;
cin.getline(obj.name);
cout<<"Enter the number:\n";
cin>>obj.number;
//Show the output
cout<< obj.name <<": " << obj.number << endl;
return 0;
}