使用 getline() 时获取垃圾

Getting garbage when using getline()

我正在练习 classes,我正在尝试让用户使用 space 输入其名称。当用户输入 space 时,我的输出是 "garbage"。是否可以将 get.line 函数与 class 一起使用?这就是我所拥有的。

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

改变这个:

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

至:

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

manual 指定。您可以省略 std::,因为您已经在使用命名空间 std.

示例运行:

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

但是请注意,class 的数据成员通常在私有范围内,public 函数可以应用于它们。此外,正如 Thomas Matthews 所说:"This breaks data hiding and encapsulation rules. I highly recommend that the functionality for reading the data member be placed in the class."。

您可能需要查看重载运算符 >>。但是,如果您觉得自己对 classes 的理解还不够,我建议您稍后再看。

你说你想要 return 用户的全名,所以我创建了一个简单的函数,它 return 是使用第一个姓氏的连接值。但是如果你想要,你应该通过 tuple 或 pair 来实现高级功能。

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

我在使用指定答案时也遇到了同样的问题,因为我没有意识到我的文件是用 UCS-2 编码的。这有帮助: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';
    }
}

用十六进制编辑器打开你正在阅读的文件,检查你的可见数据之前是否有一些字节。我最后删除了这些字节,用 20 个十六进制数据覆盖它们,这意味着 space,然后用编辑器清理文件。