Getline和cin依次跳过下一个输入
getline and cin used one after another skips next inputs
我正在编写一个c++程序来演示聚合概念,作为书中的示例。代码由4个类(在一个文件中)和一个main函数(在另一个文件中)组成。类如下
#include <iostream>
#include <string>
/*#define SYNC_CLEAR {
cin.clear();
cin.sync();
}
*/
using std::string;
using std::cout;
using std::cin;
class Student
{
private:
string school;
string degree;
public:
void input_data()
{
cout<<"Enter School name and degree name n";
getline(cin,school);
getline(cin,degree);
}
void show_data() const
{
cout<<"School :"<<school<<",Degree: "<<degree<<"n";
}
};
class Employee
{
private:
string name;
unsigned int number;
public:
void input_data()
{
string str;
cout<<"Enter Name and number n";
getline(cin,name);
cin>>number;
}
void show_data() const
{
cout<<"Name :"<<name<<",number: "<<number<<"n";
}
};
/************************Aggregation starts from here**********************************/
class Manager
{
private:
string title;
double dues;
Employee emp; // object of one class as an attribute to other class
Student stu; // object of one class as an attribute to other class
public:
**void input_data()
{
emp.input_data(); // call by object of Employee class
cout<<"Enter Title and Dues n";
/*********Problem comes here*****************/
getline(cin,title);
cin>>dues;
/**************************/
stu.input_data(); // call by object of Student class
}**
void show_data() const
{
emp.show_data();
cout<<"Title :"<<title<<",Dues: "<<dues<<"n";
stu.show_data();
}
};
class Scientist
{
private:
int pubs;
Employee emp; // object of one class as an attribute to other class
Student stu; // object of one class as an attribute to other class
public:
void input_data()
{
emp.input_data(); // call by object of Employee class
cout<<"Enter no of pubs n";
cin>>pubs;
stu.input_data(); // call by object of Student class
}
void show_data() const
{
emp.show_data();
cout<<" pubs :"<<pubs<<"n";
stu.show_data();
}
};
主要功能如下:
#include <iostream>
#include "aggregation.h"
using std::cout;
using std::cin;
int main(int argc, char const *argv[])
{
Manager m1;
Scientist s1,s2;
cout<<"Data for Managern";
m1.input_data();
cout<<"Data for Sci 1n";
s1.input_data();
cout<<"Data for Sci 2n";
s2.input_data();
cout<<"Data for Managern";
m1.show_data();
cout<<"Data for Sci 1n";
s1.show_data();
cout<<"Data for Sci 2n";
s2.show_data();
return 0;
}
问题出现在input_data()函数中的类管理器中(以粗体突出显示)。我在控制台附加输出
hduser@M-1939:~/Dropbox/c++/lafore/inhertiance$ g++ -g aggregation.cpp -o aggregation
hduser@M-1939:~/Dropbox/c++/lafore/inhertiance$ ./aggregation
Data for Manager
Enter Name and number
vivek
100
***Enter Title and Dues
sr manager***
Enter School name and degree name
Data for Sci 1
Enter Name and number
Enter no of pubs
Enter School name and degree name
Data for Sci 2
Enter Name and number
Enter no of pubs
Enter School name and degree name
Data for Manager
Name :vivek,number: 100
Title :,Dues: 0
School :,Degree:
Data for Sci 1
Name :,number: 6299824
pubs :6299824
School :,Degree:
Data for Sci 2
Name :,number: 2092888488
pubs :6299112
School :,Degree:
我看了一些链接告诉使用cin.clear()和cin.sync(),但它没有工作得很好。有问题,不像这样,不同但仍然是类似的问题。领导吗?
EDIT 1: 我使用将输入序列从1:getline 2: cin更改为1:cin 2: cin.ignore() 3: getline,它正在工作。但有人能告诉我怎么做吗?
编辑2:我忘记在雇员类的input_data()中把cin>>number后面加上cin.ignore()。它能正常工作吗?虽然是个愚蠢的错误使用说明:
cin.ignore(256,'n');
每次调用cin后清空缓冲区。
256是要忽略的字符数,'n'是分隔字符。
出于安全考虑,可以使用
std::numeric_limits<std::streamsize>::max()
作为字符的限制数量;我只是喜欢256。
http://www.cplusplus.com/reference/istream/istream/ignore/你需要做的是cin.ignore()
在cin
之后。
结果如下:
当您使用std::getline
时,它将获得该行上的任何内容,直到换行符,但是cin
在执行读取后实际上并没有删除换行符,因此您的std::getline
只获得空字符串。
相关文章:
- 重新定位图像时如何前进到下一个内存块
- 为什么C++对链表中的下一个节点使用指针,而像 C# 或 Java 这样的语言只使用类 Node 的名称?
- 用于在 C++ 中使用 while 循环查找下一个素数的简单函数
- CIN 仅在输入非数字值时跳过下一个 CIN
- 查找最小的下一个更大的元素
- 转到基于范围的 for 循环中的下一个迭代器
- 如何检查流中文件的下一个单词是否为 alpha?
- 如何打印下一个字母直到 Z 并继续到 A?
- 有没有办法在使用 GLFW 按下按键后只处理一个输入事件?
- 当我使用c++生成器时,如何在输入完成后自动跳转到下一个"TEdit"
- 使用getline()将字符串输入到程序中会导致下一个输入出现某种溢出
- 从输入流中获取下一个字符而无需存储
- 输入"cobra"到输出"dpcsb" 将 1 个字符转换为下一个 c++
- 根据某些输入文件预测文本生成'random'下一个字符
- 如何忽略标准输入中的下一个int
- 如何在获得下一个字符后正确刷新输入缓冲区,而无需按两次回车键!C++
- Input With Spaces正在阻塞下一个输入(使用cin)
- Getline和cin依次跳过下一个输入
- 如何解除对RenderTarget纹理的绑定,这样它就可以被用作下一个通道的输入
- 使用c++,在制作控制台应用程序时,是否有一种方法可以让cin接受输入的下一个字符并在不按回车键的情况下继续