程序不等待cin [英] Program not waiting for cin

查看:128
本文介绍了程序不等待cin的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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++;
}

我的循环似乎并不停止,而是立即自行执行循环3次。

My loop doesn't seem to stop for every time cin is asked, and instead instantly executes the loop 3 times on its own. How do I get it to stop when input is asked?

推荐答案

如果在调用cin时输入流不为空,然后cin使用已经在缓冲区中的数据,而不是等待用户的更多。您正在使用提取运算符,因此当cin向您的变量发送值时,它会跳过缓冲区中的前导空格,并在下一个空格处停止。

If the input stream isn't empty when you call cin, then cin uses the data already in the buffer instead of waiting for more from the user. You're using the extraction operator, so when cin is sending values to your variables, it skips leading whitespace in the buffer and stops on the next whitespace.

放置断点在此行上:

cout << "\n What is your current salary? \t";

运行程序,输入Bob Smith。当您点击断点时,将光标悬停在您的字符串fullname上。你会看到它只存储鲍勃而不是鲍勃·史密斯。 Bob Smith被放入缓冲区,但是当你使用提取操作符使用cin时,它会跳过任何前导空格,将下一个值发送到变量中,然后停在下一个空格。要演示这一点,请尝试运行此操作:

Run the program, and enter Bob Smith. When you hit the break point, hover your cursor over your string fullname. You'll see it stores only "Bob" not "Bob Smith". "Bob Smith" got put into the buffer, but when you use cin with the extraction operator, it skips any leading whitespace, puts the next value it finds into your variable, then stops on the next whitespace. To demonstrate this, try running this:

#include <iostream>
#include <string>
using namespace std;
int main()
{
    string str1,str2;
    cin >> str1;
    cin >> str2;
    cout << str1 << " " << str2 << "\n\n";
    return 0;
}

如果输入Bob Smith,它将只输入一个时间,即使你打电话两次。但是,你会看到Bob和Smith都在字符串str1和str2中被捕获。

If you type in "Bob Smith", it will take your input only one time, even though you call cin twice. However, you'll see that both "Bob" and "Smith" got captured in the strings str1 and str2.

因此,你可以得出结论,cin停止填充你的字符串当它到达Bob和史密斯之间的空间时的全名。在您下次调用cin时,缓冲区仍包含Smith,因此,不是从用户获取更多输入,它会尝试用Smith填充您的变量salary。显然这不是你想要做的。你可以调用flush并忽略cin在每次使用cin之前擦除缓冲区,或者你可以修复你的逻辑,并使用getline获取全名,包括空格。

Therefore, you can conclude that cin stops populating your string fullname when it gets to the space between Bob and Smith. On your next call to cin, the buffer still contains "Smith", so instead of taking more input from the user, it attempts to fill your variable salary with "Smith". Obviously this isn't want you want to do. You can call flush and ignore on cin to wipe out the buffer before every time you use cin, or instead you could fix your logic and use getline to take in the full name, including spaces.

要解决你的问题,你需要做的是使用getline而不是cin >>,所以替换这行:

To fix your problem, all you need to do is use getline instead of cin >>, so replace this line:

cin >> fullname;

getline(cin,fullname,'\n');

其次,你使用一个while循环执行一组动作一定次数。这通常是你使用的for循环。

Secondly, you're using a while loop to execute a set of actions a specific number of times. That's typically something you'd use a for loop for.

另外,你也可以编写小的输入验证循环,可以帮助你调试或以其他方式避免尝试无效的输入到你的变量(如史密斯成一个浮动)。这样的东西可以工作:

As an aside, you could also write tiny input validation loops that can help you debug or otherwise avoid attempting to put invalid input into your variables (such as "Smith" into a float). Something like this could work:

for(;;)
{
    if(cin >> salary)
        break;
    cin.clear();
    cin.ignore(INT_MAX,'\n');
}

请注意,cin返回一个值,因此可以在if语句。如果它获得有效的输入,它将返回true。如果不是,它将返回false。为了使它更加明确,你也可以只使用一个正常的调用cin没有if语句,然后检查是否cin.good(),基本上相同的净效果。如果您没有使用Visual Studio并收到关于INT_MAX的错误,您可能需要#include limits.h来解决它。

Note that cin returns a value, so you can use it in an if statement. If it gets valid input, it will return true. If not, it will return false. To make it more explicit, you could also just use a normal call to cin without the if statement, and then check if cin.good(), which amounts to basically the same net effect. If you're not using Visual Studio and get an error about INT_MAX, you might need to #include limits.h to resolve it.

这篇关于程序不等待cin的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆