【问题标题】:While loop takes input of first and second cinWhile 循环接受第一个和第二个 cin 的输入
【发布时间】:2019-08-11 01:46:36
【问题描述】:

我对这些代码行的意图是创建一个程序,该程序将显示用户键入的短语有多少个字符。我完成了我的代码行还有一项任务,即为用户创建一个提示来结束程序或循环它。为此,我创建了一个 while 循环。一切都很顺利,直到我被提示“再去一次?”。无论我输入什么输入,它都会自动为我提供第一个输入的输出,这意味着它也会为我的第一个 cin 输入输入。作为参考,这是我的代码:

#include <iostream>
#include <string.h>
#include <stdlib.h>

using namespace std;

int main()
{
char again = 'y';
string phrase;
while (again == 'y' || again == 'Y')
{

cout << "Enter your phrase to find out how many characters it is: ";
getline(cin,phrase);
cout << phrase.length() << endl;
cout << "Go again? (y/n) " ;

cin >> again;

}
cout << "The end." << endl;

system ("pause");
}

对不起,如果我的问题含糊不清或使用了错误的术语,我刚刚开始学习 c++。

谢谢。

【问题讨论】:

    标签: c++ while-loop cin


    【解决方案1】:

    您应该在cin&gt;&gt;again 之后添加std::cin.ignore()

    #include <iostream>
    #include <string.h>
    #include <stdlib.h>
    
    using namespace std;
    
    int main()
    {
    char again = 'y';
    string phrase;
    while (again == 'y' || again == 'Y')
    {
    
    cout << "Enter your phrase to find out how many characters it is: ";
    getline(cin,phrase);
    cout << phrase.length() << endl;
    cout << "Go again? (y/n) " ;
    
    cin >> again;
    cin.ignore();
    }
    cout << "The end." << endl;
    
    system ("pause");
    }
    

    问题在于std::cin 之后新行仍将保留在输入缓冲区中,getline() 将读取该换行符。 std::ignore() 只会从输入缓冲区中抓取一个字符并将其丢弃。

    更多信息请参考http://www.augustcouncil.com/~tgibson/tutorial/iotips.html#problems

    【讨论】:

      【解决方案2】:

      std::cin &lt;&lt; varname 存在一些问题。

      当用户在输入变量后键入“enter”时,cin 将只读取该变量,并将“enter”留给下一次读取。

      当您将cingetline() 混合在一起时,您有时会吃掉“输入”,有时则不会。

      一种解决方案是在cin 调用之后添加一个ignore 调用来吃掉“进入”

      cin >> again;
      std::cin.ignore();
      

      第二种解决方案是使用 std::getline()。将 getline() 与 std::istringstream 结合起来通常是一个好习惯。这是使用 getline() 和 istringstream 解决此问题的另一种方法。解释见代码中的cmets。

      #include <iostream>
      #include <string.h>
      #include <sstream>
      
      int main()
      {
        std::string line;
        std::string input;
        std::istringstream instream;
      
        do {
          instream.clear(); // clear out old errors
      
          std::cout << "Enter your phrase to find out how many characters it is: "
                    << std::endl;
      
          std::getline(std::cin, line);
          instream.str(line); // Use line as the source for istringstream object
      
          instream >> input;  // Note, if more than one word was entered, this will
                              // only get the first word
      
          if(instream.eof()) {
            std::cout << "Length of input word:  " << input.length() << std::endl;
      
          } else {
            std::cout << "Length of first inputted word:  " << input.length() << std::endl;
      
          }
          std::cout << "Go again? (y/n) " << std::endl;
        // The getline(std::cin, line) casts to bool when used in an `if` or `while` statement.
        // Here, getline() will return true for valid inputs. 
        // Then, using '&&', we can check
        // what was read to see if it was a "y" or "Y".
        // Note, that line is a string, so we use double quotes around the "y"
        }  while (getline(std::cin, line) && (line == "y" || line == "Y"));
      
        std::cout << "The end." << std::endl;
      
        std::cin >> input; // pause program until last input.
      }
      

      【讨论】:

        猜你喜欢
        • 2020-11-09
        • 2021-08-17
        • 1970-01-01
        • 1970-01-01
        • 2021-12-02
        • 1970-01-01
        • 2023-04-02
        • 2016-10-22
        • 1970-01-01
        相关资源
        最近更新 更多