C++ – cin 在没有用户输入的情况下跳过输入

huangapple go评论68阅读模式
英文:

C++ - cin is skipping input without any user input

问题

#include <iostream>
#include <vector>

using namespace std;

int main() {
    int userinput;
    int sums;
    int sum = 0;
    vector<int> nums;
    
    cout << "Enter as many numbers as you'd like." << endl;
    while (cin >> userinput) {
        nums.push_back(userinput);
    }
    
    cout << "Enter the numbers you want to add: ";
    cin >> sums;
    
    
    for (int i = 0; i < sums; ++i) {
        sum += nums[i];
    }
    
    cout << "The sum of the first " << sums << " numbers is: " << sum << "." << endl;
    
    return 0;
}

我的问题是,在接受变量 sums 的输入行上,每当我运行代码时,都会跳过这个输入行,sums 只设置为1,没有我的输入。我尝试过 cin.clear(),因为我听说它可能会修复它,但都没有成功。

英文:
#include &lt;iostream&gt;
#include &lt;vector&gt;

using namespace std;

int main() {
    int userinput;
    int sums;
    int sum = 0;
    vector&lt;int&gt; nums;
    
    cout &lt;&lt; &quot;Enter as many numbers as you&#39;d like.&quot; &lt;&lt; endl;
    while (cin &gt;&gt; userinput) {
        nums.push_back(userinput);
    }
    
    cout &lt;&lt; &quot;Enter the numbers you want to add: &quot;;
    cin &gt;&gt; sums;
    
    
    for (int i = 0; i &lt; sums; ++i) {
        sum += nums[i];
    }
    
    cout &lt;&lt; &quot;The sum of the first &quot; &lt;&lt; sums &lt;&lt; &quot; numbers is: &quot; &lt;&lt; sum &lt;&lt; &quot;.&quot; &lt;&lt; endl;
    
    return 0;
}

My problem is that on the line where I take input for the variable sums, whenever I actually run the code, this input line is skipped over and sums just gets set to 1 without any of my own input. I'm sorry if this is a really stupid question but I have nowhere else to ask as I am trying to learn myself.

I've tried cin.clear() because I've heard that could fix it but none of them worked.

答案1

得分: 0

while (cin &gt;&gt; userinput) 只有在 cin 评估为 false 时才会退出。cin 仅在处于错误状态时评估为 false,这将阻止使用 cin 进行任何进一步的操作。您必须在要求用户的下一个输入之前使用 cin.clear 来清除错误状态:

cout << "输入您要相加的数字:";
cin.clear();
cin >> sums;

正如评论中所提到的,如果用户可以输入某个值来告诉程序他们已经停止输入数字,会更好。例如:

while (cin >> userinput) {
   if (userinput == -1 /* 或您想要的任何值 */) break;
   nums.push_back(userinput);
}

以这种方式中断循环允许退出循环,而无需将 cin 设置为错误状态(这也可以说是更好的设计)。

英文:

The only way that while (cin &gt;&gt; userinput) will be exited is if cin evaluates to false. cin evaluates to false only if it's in an error state, which would prevent any further operations using cin. You have to clear the error state with cin.clear before you ask for the next input from the user:

cout &lt;&lt; &quot;Enter the numbers you want to add: &quot;;
cin.clear();
cin &gt;&gt; sums;

As the comments have said, it would be better if the user could input some value to signal to the program that they've stopped inputting numbers. For example:

while (cin &gt;&gt; userinput) {
   if (userinput == -1 /* or whatever value you want */) break;
   nums.push_back(userinput);
}

Breaking the loop this way allows the loop to be exited without setting cin to be in an error state (it's also arguably better design).

huangapple
  • 本文由 发表于 2023年7月3日 20:35:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/76604809.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定