使用getline()获取垃圾
Getting garbage when using getline()
我正在练习课程,我试图允许用户使用空格输入其名称。当用户输入一个空格时,我的输出是"垃圾"。是否可以使用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十六进制数据覆盖它们,这意味着空间,然后用编辑器清理文件。
相关文章:
- 当 getline 用于获取整个字符串时,程序崩溃
- 有没有办法将 getline() 与外部文本文件一起使用,然后重新获得 cin 的控制权以从控制台获取输入?
- C++ 获取线错误。如何避免此错误?"没有重载函数"getline"的实例
- 在getline()之后获取字符串
- getline 无法获取文件的第一行
- 填充字符数组的最佳方法(获取与cin.getline)
- 看到' - '字符时我无法结束 getline,有人可以帮助我在 c++ 中获取 getline() ifstream
- 如何阻止 cin.getline() 导致控制台在找不到分隔符时重复获取用户输入?
- 我在使用 cin.getline() 从用户那里获取 3 个字符串并使用指针数组来存储它们时遇到问题
- 使用 getline 函数获取分段错误
- C++ 如何从 ifstream 和 getline() 获取子字符串
- getline 语句未获取输入
- 使用 getline 从文件中获取多行并将它们输入到不同的数组中
- 如何使用嵌套的getline()函数来获取字符串中的特殊字符和标点符号
- 当使用getline(cin,string)时,CIN会自动获取其值而不询问
- cout在获取行输入(getline)后无法在c++中工作
- istream::getline() 未获取最后一个字符
- getline() 获取不正确的字符
- cin.getline() 等效于从函数获取字符时
- Getline不能在cin.clear后获取某些字符