程序不等待 cin

Program not waiting for cin(程序不等待 cin)

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

问题描述

int x=0;
string fullname = "";
float salary;
float payincrease;
float newsal;
float monthlysal;
float retroactive;
while(x<3){
    cout << "
 What is your full name?";
    cin >> fullname;
    cout << "
 What is your current salary? 	";
    cin >> salary;
    cout << "
 What is your pay increase? 	";
    cin >> payincrease;
    newsal = (salary*payincrease)+salary;
    monthlysal = newsal/12.00;
    retroactive = (monthlysal*6)-(salary/2);
    cout << "
" << fullname << "'s SALARY INFORMATION";
    cout << "
 New Salary 	 Monthly Salary 	 Retroactive Pay";
    cout << "
 	" << newsal << "	" << monthlysal << "	" << retroactive;
    x++;
}

每次询问 cin 时,我的循环似乎都不会停止,而是立即自行执行循环 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 << "
 What is your current salary? 	";

运行程序,然后输入 Bob Smith.当您到达断点时,将光标悬停在您的字符串全名上.你会看到它只存储Bob"而不是Bob Smith".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 << "

";
    return 0;
}

如果您输入Bob Smith",即使您调用 cin 两次,它也只会输入一次.但是,您会看到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 和 Smith 之间的空格时,它会停止填充您的字符串全名.在您下次调用 cin 时,缓冲区仍然包含Smith",因此它不会从用户那里获取更多输入,而是尝试用Smith"填充您的可变薪水.显然这不是你想要做的.您可以在每次使用 cin 之前在 cin 上调用 flush 和 ignore 以清除缓冲区,或者您可以修复您的逻辑并使用 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,'
');

其次,您正在使用 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.

顺便说一句,您还可以编写微小的输入验证循环,以帮助您调试或避免尝试将无效输入放入变量中(例如Smith"放入浮点数).像这样的东西可以工作:

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,'
');
}

请注意 cin 返回一个值,因此您可以在 if 语句中使用它.如果它得到有效的输入,它将返回 true.如果不是,它将返回 false.为了使其更明确,您也可以只使用不带 if 语句的正常调用 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:程序不等待 cin

基础教程推荐