Palermox
Palermox

Reputation: 63

Getting garbage when using getline()

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

Upvotes: 2

Views: 1843

Answers (4)

Zac
Zac

Reputation: 4695

Open the file you are reading with an hex editor and check if there are some bytes before your visible data. I ended up by removing these bytes overwriting them with 20 hex data which means space, and then cleaning the file with the editor.

Upvotes: 0

MGamsby
MGamsby

Reputation: 406

I also had the same problem using the specified answers, because I didn't realize my file was encoded in UCS-2. This helped : How to read a UCS-2 file?

Wide streams use a wide stream buffer to access the file. The Wide stream buffer reads bytes from the file and uses its codecvt facet to convert these bytes to wide characters. The default codecvt facet is std::codecvt which converts between the native character sets for wchar_t and char (i.e., like mbstowcs() does).

You're not using the native char character set, so what you want is a codecvt facet that reads UCS-2 as a multibyte sequence and converts it to wide characters.

#include <fstream>
#include <string>
#include <codecvt>
#include <iostream>

int main(int argc, char *argv[])
{
    wifstream fin("en.rc", std::ios::binary); // You need to open the file in binary mode

    // Imbue the file stream with a codecvt facet that uses UTF-16 as the external multibyte encoding
    fin.imbue(std::locale(fin.getloc(),
              new std::codecvt_utf16<wchar_t, 0xffff, consume_header>));

    // ^ We set 0xFFFF as the maxcode because that's the largest that will fit in a single wchar_t
    //   We use consume_header to detect and use the UTF-16 'BOM'

    // The following is not really the correct way to write Unicode output, but it's easy
    std::wstring sLine;
    std::wstring_convert<std::codecvt_utf8<wchar_t>, wchar_t> convert;
    while (getline(fin, sLine))
    {
        std::cout << convert.to_bytes(sLine) << '\n';
    }
}

Upvotes: 0

gsamaras
gsamaras

Reputation: 73366

Change this:

cin>>obj.name;
cin.getline(obj.name);

to:

std::getline (std::cin, obj.name);

as specified by the manual. You can omit std::, since you are already using namespace std.

Example run:

Enter the Name: 
samaras
Enter the number:
1
samaras: 1

However notice that the data members of a class are usually on private scope and public functions can apply to them. Also, as Thomas Matthews put it: "This breaks data hiding and encapsulation rules. I highly recommend that the functionality for reading the data member be placed in the class.".

You may want to look into overloading operator >>. However, if you feel like you are not done with understanding the classes, I would suggest to leave that for later.

Upvotes: 2

Lamour
Lamour

Reputation: 3030

You said that you want to return the full name of the user, So I create a simple function that returns a concatenated value using the first last name. but if you want you should go over tuple or pair to make advance function.

 #include  <iostream>
 #include <string>

using namespace std;
 class person
 {
    //Access Specifier
    public: 
    string firstname;
    string lastname;    //Member Vairable Decleration
    int number;     //Member Function Decleration
 };

string returnCompleteName(string firstname, string lastname)
  {
   return firstname + " " + lastname;
  }

 //Main function.
int main()
{
//Object creation from class.
person obj;

//Get input Values for object Variables
cout<<"Enter the FirstName: \n";
cin>>obj.firstname;

cout<<"Enter the LastName: \n";
cin>>obj.lastname;

cout<<"Enter`enter code here` the number:\n";
cin>>obj.number;

//Show the output
cout<< returnCompleteName(obj.firstname,obj.lastname) <<": " << obj.number << endl;

return 0;
}

Upvotes: 0

Related Questions