此代码的工作,但不是我的本意。每次我想按1
在命令提示符下输出会变成这个样子,进入新的工资:
Comic books : USD Input error! Salary must be in positive integer.
的代码应该在cout<<"\n\nComic books\t\t: USD ";
停在第4行,但它只是与内部while循环执行。这是代码:
double multiplePay =0;
cout<<"\n\nEnter employee pay for each job";
while (1){
cout<<"\n\nComic books\t\t: USD ";
//cin.get(); if enable, the first user input will be 0. this is not working.
std::string comic_string;
double comic_double;
while (std::getline(std::cin, comic_string))
{
std::stringstream ss(comic_string); // check for integer value
if (ss >> comic_double)
{
if (ss.eof())
{ // Success so get out
break;
}
}
std::cout << "Input error! Salary must be in positive integer.\n" << std::endl;
cout<<"Employee salary\t: ";
}
comic = strtod(comic_string.c_str(), NULL);
multiplePay = comic + multiplePay; // update previous salary with new user input
cout << multiplePay;
cout << "Add other pay?"; // add new salary again?
int y;
cin >> y;
if (y == 1){
cout << multiplePay;
}
else{
break;
}
} // while
cout << multiplePay; //the sum of all salary
使用cin.get()
就能解决问题,但第一个用户输入的薪水将成为0
,只有下一个输入将被计算。请帮助我。提前致谢。
是的,这是问题所在。在最后的'if语句'中添加'cin.get()'解决了这个问题。再次感谢。 – sg552