【发布时间】:2012-08-29 15:10:21
【问题描述】:
int x=0;
string fullname = "";
float salary;
float payincrease;
float newsal;
float monthlysal;
float retroactive;
while(x<3){
cout << "\n What is your full name?";
cin >> fullname;
cout << "\n What is your current salary? \t";
cin >> salary;
cout << "\n What is your pay increase? \t";
cin >> payincrease;
newsal = (salary*payincrease)+salary;
monthlysal = newsal/12.00;
retroactive = (monthlysal*6)-(salary/2);
cout << "\n" << fullname << "'s SALARY INFORMATION";
cout << "\n New Salary \t Monthly Salary \t Retroactive Pay";
cout << "\n \t" << newsal << "\t" << monthlysal << "\t" << retroactive;
x++;
}
每次询问 cin 时,我的循环似乎都不会停止,而是立即自行执行循环 3 次。当询问输入时如何让它停止?
【问题讨论】:
-
cin停在空格处。如果您一次输入一个单词,它将正常工作。否则,使用std::getline。 -
您绝不能编写未立即在布尔上下文中捕获的输入操作。否则,无法说明您的程序将如何进行。没有必要再看下去了。
-
@chris,
cin本身并不会专门在空格处“停止”;operator>>(istream&,string&)默认情况下会这样做。 -
@eq-,我很欣赏你的迂腐。你是对的,当然。
cin只是一个对象。 -
@chris *pedantry(因为你很欣赏它)——无法抗拒。