getline 语句未获取输入
getline statement is not getting the input
//class
class student
{
public:
int rno;
string name;
int marks;
int ran;
void getinfo()
{ a:
cout<<"t tenter the roll number"<<endl;
cin>>rno;
cout<<"t tenter the name"<<endl;
getline(cin,name);
cout<<"t tenter the marks"<<endl;
cin>>marks;
}
void showinfo()
{
cout<<"t"<<ran<<"t "<<rno<<" tt"<<name<<" tt"<<marks<<endl<<endl;
}
};
当我在为卷号输入后在控制台中获取对象的输入时,它正在打印"输入名称",然后没有任何机会给出输入,它正在显示下一个打印语句"输入标记"。 Getline 语句没有从控制台获取输入有什么原因吗?
cin
将在缓冲区中保留新行。 因此,当您从cin
获取rno
时,cin
缓冲区中实际上会剩下一个n
。 当你去读名字时,它只是抓住n
并立即返回。在第一个cin
之后执行类似 cin.ignore();
的操作应该会清除缓冲区并允许您正确读取用户输入。
相关文章:
- 如何使用 < 和 > 命令获取 c++ 中的输入和输出?
- 如何从用户那里获取输入并将其存储在向量中?
- 获取通过重定向传入的输入文件的名称?
- Cryptopp:获取密码输入的填充字符串
- 从格式化输入操作中获取读取字符数
- 如何获取用户输入的算术表达式,如 5-8+7*4-8+9 或 1+5?
- 我无法从 C# 获取 PInvoke 的输入参数以C++ DLL 以用作 IntPtr 的输出
- 我需要帮助在一个类中输入两个字符串并获取其输出
- 用于获取枚举值的用户输入
- C++如何使用表达式获取用户输入?
- 从 cin 获取 c++ 中结构中多个枚举的输入
- 代码在执行之前终止,并且没有从用户那里获取输入
- 适用于 macOS 的 Xcode 应用程序。这就是我设置从USB麦克风输入获取音频的方式。一年前工作,现在没有了。为什么
- 推荐一种在未输入获取行分隔符时引发异常的方法?
- 输入获取线的结尾
- 线程不刷新数据,无法从标准输入获取所有数据
- 如何从同一输入获取两个输入 (C++)
- 在C++中静态地将用户输入获取到类中
- 在使用C++字符串时对特定输入获取bad_alloc
- 使用MPI程序,所有进程都从用户输入获取数据