简体   繁体   English

C ++ std :: cin产生额外的输入

[英]C++ std::cin producing extra input

I am new to C++ and attempting to tackle some basic CodeJam problems. 我是C ++的新手,正在尝试解决一些基本的CodeJam问题。 I am working on Reverse Words . 我正在研究反向词 I am running my code (in a unix environment) by piping in and out of the compiled executable: ./compiled.program < input_file.in > output_file.out 我通过在已编译的可执行文件中输入和输出来运行我的代码(在UNIX环境中): ./compiled.program < input_file.in > output_file.out

Here is my input ( input_file.in ): 这是我的输入( input_file.in ):

3
this is a test
foobar
all your base

I would EXPECT the output: 我期望输出:

test a is this
foobar
base your all

However, I get the output ( output_file.out ): 但是,我得到了输出( output_file.out ):


test a is this
foobar

(yes, that space in the beginning was intentional) (是的,开头的空格是有意的)

Here is my source code: 这是我的源代码:

#include <string>                                                                                                           
#include <iostream>

int main()
{
      int number_of_cases;
      std::cin >> number_of_cases; 

      for (int i=1; i<=number_of_cases; i++) {
          std::cerr << i << std::endl;

          std::string input = ""; 
          std::getline(std::cin, input);

          while (true) {
              int pos = input.find_last_of(" ");
              if (pos == -1) {
                    std::cout << input;
                    break;
              }   
              std::cout << input.substr(pos+1)+" ";
              input.resize(pos);
        }   
        std::cout << std::endl;
    }   
    return 0;
}

The problem to me seems to be that another source of input (a blank source of input) is being read from between 3 and this is a test but for the life of me I cannot find out why. 对我来说,问题似乎是正在从3之间读取另一个输入源(空白输入源), this is a test但是对于我而言,我无法找到原因。 So that is my question: Why is this other source of input being read from? 所以这就是我的问题:为什么要读取其他输入源?

Any help is greatly appreciated. 任何帮助是极大的赞赏。 Thank you so much in advance! 提前非常感谢您!

The line 线

std::cin >> number_of_cases; 

reads in 3 but stops there, leaving the newline in the stream. 读入3但停在那里,将换行符留在流中。

So for i == 1 , std::getline(std::cin, input); 所以对于i == 1std::getline(std::cin, input); just reads the newline from the end of the first line. 只是从第一行的末尾读取换行符。 Since this contains no spaces, you trigger std::cout << input; 由于它不包含空格,因此您将触发std::cout << input; and then break down to std::cout << std::endl , generating your blank line. 然后分解为std::cout << std::endl ,生成空白行。

Then the count of 3 runs out before getting to all your base . 然后3的数用完了,直到all your base

To fix this you could do a dummy call to getline before entering the loop (this will have the bonus of consuming any trailing whitespace too). 为了解决这个问题,您可以在进入循环之前对getline进行虚拟调用(这样做也有消耗任何尾随空格的好处)。

In your program and sample input, when you input '3', I exactly input a character 3 and a character '\\n'. 在您的程序和示例输入中,当您输入'3'时,我恰好输入了一个字符3和一个字符'\\ n'。 So cin reads the character of integer only and leaves the '\\n' in input buffer. 因此, cin读取整数字符,并将'\\ n'留在输入缓冲区中。 std::getline reads the '\\n' in the first iteration. std::getline在第一次迭代中读取“ \\ n”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM