【发布时间】:2012-08-21 05:18:19
【问题描述】:
好吧,当我遇到以前从未遇到过的问题时,我只是在做一点练习。
#include <iostream>
using namespace std;
int main()
{
string empname = "";
int empage = 0;
char yes = 'y';
cout << "Please enter employee name:" << endl;
cin >> empname;
cin.get();
cout << "Your name is " + empname + " is this correct? (yes/no):" << endl;
if (yes)
{
cout << "good" << endl;
}
else
{
cout << "Please try again" << endl;
}
cout << "Please enter employee age:" << endl;
cin >> empage;
cin.get();
cout << "Your age is " + empname + " is this correct? (yes/no):" << endl;
if (yes)
{
cout << "good" << endl;
}
else
{
cout << "Please try again" << endl;
}
}
这作为控制台程序执行,但在第 11 行 [包括空格] (cout
【问题讨论】:
-
如果
char yes = 'y';,那么yes总是true。我认为这部分尚未实施。 -
它看起来不错,在 gcc 4.6.3 上运行良好。您确定要显示您尝试运行的代码吗?
-
你能在cin之后试试fflush(stdin) >> empname;行。
-
@BharatSharma:
fflush(stdin)是未定义的行为。见:stackoverflow.com/questions/2979209/using-fflushstdin -
@GregHewgill 谢谢我认为你已经给出了正确的答案。实际上,我在大学时就使用过它,并且在该编译器上运行良好。但我知道我们需要清除输入缓冲区。