使用getline()获取垃圾

Getting garbage when using getline()

本文关键字:获取 getline 使用      更新时间:2023-10-16

我正在练习课程,我试图允许用户使用空格输入其名称。当用户输入一个空格时,我的输出是"垃圾"。是否可以使用get。行函数与类?这是我有的。

//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);
手册中指定的

。您可以省略std::,因为您已经使用了命名空间std。

示例运行:

Enter the Name: 
samaras
Enter the number:
1
samaras: 1
但是请注意,类的数据成员通常位于私有作用域,公共函数可以应用于它们。此外,正如Thomas Matthews所说:"这打破了数据隐藏和封装规则。我强烈建议将读取数据成员的功能放在类中。"

您可能需要查看重载操作符>>。但是,如果您觉得还没有完全理解这些类,我建议您以后再做。

您说要返回用户的全名,所以我创建了一个简单的函数,它使用姓的名字返回一个连接的值。但如果你想的话,你应该越过元组或pair来创建advance函数。

 #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编码的。这有助于:如何读取UCS-2文件?

宽流使用宽流缓冲区来访问文件。宽阔的流缓冲区从文件中读取字节,并使用它的编解码面将这些字节转换为宽字符。默认的编码解码面是Std::codecvt在wchar_t和char的本地字符集(如mbstowcs())) .

您没有使用本机字符字符集,所以您想要的是acodecvt方面,读取UCS-2作为多字节序列并转换它

#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十六进制数据覆盖它们,这意味着空间,然后用编辑器清理文件。