cin.getline()的奇怪行为
cin.getline() strange behavior
下面是一本书中练习类继承的练习。但问题出在客户端,而不是类设计。(BaseCore、baseDMA、lacksDMA和hasDMA是BTW类)。
// usedma.cpp -- polymorphic example (compile with dma.cpp)
#include <iostream>
#include "dma.h" // includes <iostream>
const int ELEMENTS = 1;
const int LENGTH = 30;
int main()
{
using std::cin;
using std::cout;
BaseCore *pArr[ELEMENTS];
char tempDate[LENGTH];
char kind;
for (int i = 0; i < ELEMENTS; i++)
{
cout << "nEntering data for element #" << i + 1 << "nn";
cout << "Enter the date it was created: ";
cin.getline(tempDate, LENGTH - 1);
cout << "Enter 1 for baseDMA, 2 for lacksDMA, or 3 for hasDMA: ";
while (cin >> kind && kind != '1' && kind != '2' && kind != '3')
cout <<"Wrong data. Please, try again: ";
while (cin.get() != 'n')
continue;
char tempLabel[LENGTH];
int tempRating;
cout << "Enter the label: ";
cin.getline(tempLabel, LENGTH - 1);
cout << "Enter the rating: ";
cin >> tempRating;
if (kind == '1') // baseDMA
pArr[i] = new baseDMA(tempDate, tempLabel, tempRating);
if (kind == '2') // lacksDMA
{
char tempColor[LENGTH];
cout << "Enter the color: ";
cin.getline(tempColor, LENGTH - 1);
pArr[i] = new lacksDMA(tempDate, tempLabel, tempColor, tempRating);
}
if (kind == '3') // hasDMA
{
char tempStyle[LENGTH];
cout << "Enter the style: ";
cin.getline(tempStyle, LENGTH - 1);
pArr[i] = new hasDMA(tempDate, tempLabel, tempStyle, tempRating);
}
while (cin.get() != 'n')
continue;
}
cout << "n";
for (int i = 0; i < ELEMENTS; i++)
{
pArr[i]->View();
cout << "n";
}
cout << "Done.n";
std::cin.get();
return 0;
}
示例执行:
输入元素#1 的数据
输入创建日期:2012.01.01
为baseDMA输入1,为lacksDMA输入2,或为hasDMA:2 输入3
输入标签:lacksDMA
输入评级:15
输入颜色:蓝色
创建日期:2012.01.01
标签:lacksDMA
评级:15
颜色:
完成。
看起来Color成员被分配了空字符。这种行为发生在if (kind == '2')
和if (kind == '3')
语句内部(在本例中为样式成员)。
如果我把一个cin.get();
放在cin.getline()之前,它可以正常工作,但我必须按下一个额外的键才能让程序请求输入。
为什么会发生这种情况?如果输入队列中有一个"\n"挂起,cin.getline()会丢弃它,并将"\0"放入变量中,我可以理解这一点。但是程序要求我输入颜色,让我正常输入。此外,如果我放了一个cin.get(),那么程序不应该在执行中等待一个额外的键笔划,它只应该去掉那个额外的'\n'。我在这里错过了什么?
cout << "Enter the rating: ";
cin >> tempRating;
与istream::getline()
不同,operator>>
在流中留下尾随的n
。它会导致在一个if语句中对getline
的下一次调用获得空输入。
当控制流在for循环结束时到达while (cin.get() != 'n')
语句时,流是空的——它正在等待输入,并且看起来好像您仍在输入颜色。
之后立即致电cin.ignore()
,它就会工作。
请注意,如果在颜色的输入语句后面加上"debugging cout",那么这种错误就会立即显现出来。获取tempRating
的方式还有一个问题。如果输入无效的输入,比如"xy",则错误标志将在cin
上设置,程序将进入无限循环。始终检查输入操作是否成功。
如果我放一个cin.get();就在cin.getline()之前,它工作得很好,但我必须按下一个额外的键才能让程序请求输入。
在我看来,当你不放cin.get()时,你的getline会得到一个空字符。然后,当你放cin.get时,你会得到一个空字符,你的getline工作得很好。。
但您绝对应该进行调试,看看到底发生了什么!
- Getline(cin,str)的奇怪输出
- 使用 getline(cin, var) 两次在进行字符串比较时会产生错误 (==)
- C++ getline(cin,变量)行为不端.网站上没有任何解决方案有帮助
- 由于 getline(cin,s) 导致的 SIGABRT 错误
- 为什么我们需要在getline(cin,string)之前使用cin.ignore()
- 在C 中使用两个GetLine(CIN,S)
- 使用 getline(cin,n) 输入;没有打印第一个输入,我没有使用 CIN >> 在任何地方输入
- 为什么必须键入 getline(cin,字符串)两次
- 无法删除 txt 文件中 getline(cin , (变量)) 之后的下一行
- getline(cin,_string);
- 当使用getline(cin,string)时,CIN会自动获取其值而不询问
- 我的程序跳过getline(cin,CustInfo),进入CustomerInfo函数中的City.我该怎么修
- Using cin AFTER getline(cin, string)
- 如何正确使用"getline(cin, input);"
- 关于getline(cin,string)的C++快速问题
- getline(cin,s1)没有提示我输入
- 为什么 cin.ignore() 后面的 getline(cin, var) 不读取字符串的第一个字符?
- 为什么getline(cin,string_name)在我第一次在循环中使用它时会创建一个空字符串
- getline(cin,aString)接收输入而不输入另一个值
- getline(cin,string)与cin.ignore()一起不工作