简体   繁体   English

std :: getline卡在管道输入的末尾

[英]std::getline stuck on end of piped input

I am failing to parse this file: 我无法解析此文件:

1

3
John Doe
Jane Smith
Jane Austen
1 2 3
2 1 3
2 3 1
1 2 3
3 1 2

With the following program: 使用以下程序:

#include <iostream>
#include <sstream>

int main( int argc, char * argv[] )
{

    int cases, candidates_number;
    char candidates[20][80];
    int votes[20][1000];
    int votes_count[20];

    std::string line;

    std::cin >> cases;
    std::cin.get();
    std::cin.get();

    for (int i=0; i<cases; i++) {

        std::cin >> candidates_number;
        std::cin.get();

        for (int j=0; j<candidates_number; j++) {
            std::cin.getline(candidates[j], sizeof(candidates[j]), '\n');
        }

        int votes_number = 0;

        while (std::getline(std::cin, line)) {
            if (line.empty()) {
                break;
            }
            std::cout << line;
            votes_number++;
        }

    }
    return 0;
}

In particular, the process gets stuck in the while loop after reading the last line of the file. 特别是,在读取文件的最后一行之后,该过程陷入了while循环中。 I am invoking my program through the "Standard input" feature in Eclipse. 我正在通过Eclipse中的“标准输入”功能来调用程序。

Why isn't std::getline failing and causing the program to finish? 为什么std::getline不会失败并导致程序完成?


Problem requirements: 问题要求:

Australian ballots require that voters rank all the candidates in order of choice. 澳大利亚的选票要求选民按照选择的顺序对所有候选人进行排名。 Initially only the first choices are counted, and if one candidate receives more than 50% of the vote then that candidate is elected. 最初只计算第一选择,如果一个候选人获得超过50%的选票,则当选该候选人。 However, if no candidate receives more than 50%, all candidates tied for the lowest number of votes are eliminated. 但是,如果没有候选人获得超过50%的选票,则所有票数最少的候选人将被淘汰。 Ballots ranking these candidates first are recounted in favor of their highest-ranked non-eliminated candidate. 优先考虑将这些候选人排名第一的选票,以支持其排名最高的未淘汰候选人。 This process of eliminating the weakest candidates and counting their ballots in favor of the preferred non-eliminated candidate continues until one candidate receives more than 50% of the vote, or until all remaining candidates are tied. 淘汰最弱的候选人并计算其票数以偏爱未被淘汰的候选人的过程一直持续到一名候选人获得超过50%的选票或所有其余候选人并列为止。

Input 输入项

The input begins with a single positive integer on a line by itself indicating the number of cases following, each as described below. 输入以一行上的单个正整数开头,指示其后的情况数,每种情况如下所述。 This line is followed by a blank line. 该行后跟空白行。 There is also a blank line between two consecutive inputs. 在两个连续的输入之间也有一个空白行。

The first line of each case is an integer n$ \\le$20 indicating the number of candidates. 每个案例的第一行是一个整数n $ \\ le $ 20,表示候选者的数量。 The next n lines consist of the names of the candidates in order, each up to 80 characters in length and containing any printable characters. 接下来的n行按顺序包含候选名称,每行的长度最多80个字符,并包含任何可打印的字符。 Up to 1,000 lines follow, each containing the contents of a ballot. 随后最多1,000行,每行包含一个选票的内容。 Each ballot contains the numbers from 1 to n in some order. 每个选票都按一定顺序包含从1到n的数字。 The first number indicates the candidate of first choice; 第一个数字表示首选候选人; the second number indicates candidate of second choice, and so on. 第二个数字表示第二选择的候选者,依此类推。

Output 输出量

The output of each test case consists of either a single line containing the name of the winner or several lines containing the names of all candidates who are tied. 每个测试用例的输出由包含获胜者姓名的一行或包含并列所有候选人名称的几行组成。 The output of each two consecutive cases are separated by a blank line. 每两个连续案例的输出由空白行分隔。

The program was actually correct, but I was feeding the input file through Eclipse in the wrong way (it was treated as interactive input): 该程序实际上是正确的,但是我以错误的方式通过Eclipse提供了输入文件(它被视为交互式输入):

在此处输入图片说明

Using a program argument < input instead works as expected: 使用程序参数< input可以按预期工作:

在此处输入图片说明

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

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