为什么 iostream 会切断某些单词的第一个字母?

Why is iostream cutting off the first letter in certain words?

注意:请在回答前阅读评论。这个问题似乎是特定于编译器的。

我有一个简单的程序,它从文件或控制台读取名称和一些成绩到结构 Student_info,然后通过重载 << 和 >> 运算符打印出一些数据。但是,该程序正在截断部分甚至整个单词并移动数据。例如,输入

Eunice 29 87 42 33 18 13 
Mary 71 24 3 96 70 14 
Carl 61 12 10 44 82 36 
Debbie 25 42 53 63 34 95 

returns

Eunice: 42 33 18 13 
Mary: 3 96 70 14 
rl: 10 44 82 36 
25: 63 34 95 

表明流以某种方式忽略了 Carl 的前两个字母,然后将整个流左移 1 个单词。我一直在尝试调试它一个小时的大部分时间,但它似乎很武断。对于不同的名字,不同的词被截断,没有明显的规律。

#include <iostream>
#include <fstream>
#include <string>
#include <vector>

struct Student_info {
    friend std::ostream &operator<<( std::ostream &output,
                                    const Student_info &S ) { // overloads operator to print name and grades
        output << S.name << ": ";
        for (auto it = S.homework.begin(); it != S.homework.end(); ++it)
            std::cout << *it << ' ';
        return output;
    }

    friend std::istream &operator>>( std::istream &input, Student_info &S ) { // overloads operator to read into Student_info object
        input >> S.name >> S.midterm >> S.final;
        double x;
        if (input) {
            S.homework.clear();
            while (input >> x) {
                S.homework.push_back(x);
            }
            input.clear();
        }
        return input;
    }

    std::string name; // student name
    double midterm, final; // student exam scores
    std::vector<double> homework; // student homework total score (mean or median)

};


int main() {
    //std::ifstream myfile ("/Users/.../Documents/C++/example_short.txt");
    Student_info student; // temp object for receiving data from istream
    std::vector<Student_info> student_list; // list of students and their test grades
    while (std::cin >> student) { // or myfile >> student
        student_list.push_back(student);
        student.homework.clear();
    }
    for (auto it = student_list.begin(); it != student_list.end(); ++it) {
        std::cout << *it << '\n';
    }
    return 0;
}

编辑:添加换行符。

如您所见,它不适用于 clang, but it does with GCC

我认为问题出在用 end of linedouble 解析输入时。 我找到了 2 种解决方法:

  1. 阅读作业直到行尾。

    friend std::istream &operator>>(std::istream &input, Student_info &S)
    { // overloads operator to read into Student_info object
      input >> S.name >> S.midterm >> S.final;
      double x;
      if (input)
      {
        S.homework.clear();
        while ((input.peek()!='\n') && input >> x)
        {
            //std::cout << x << "\n";
            S.homework.push_back(x);
        }
        input.clear();
      }
    
      return input;
    }
    
  2. 如果您知道输入将是整数,请不要将其解析为双精度

    friend std::istream &operator>>(std::istream &input, Student_info &S)
    { // overloads operator to read into Student_info object
      input >> S.name >> S.midterm >> S.final;
      int x;
      if (input)
      {
        S.homework.clear();
        while (input >> x)
        {
            //std::cout << x << "\n";
            S.homework.push_back(x);
        }
        input.clear();
      }
    
      return input;
    }
    

当您知道 Student_Info 对象的信息总是在一行中时,最好使用以下策略。

  1. 阅读一行文字。
  2. 从文本行构造一个std::istringstream
  3. std::istringstream.
  4. 中提取Student_Info对应的数据

它只是让解析代码更简单,更不容易出错。

// overloads operator to read into Student_info object
friend std::istream &operator>>( std::istream &input, Student_info &S )
{
   std::string line;
   if ( !getline(input, line) )
   {
      // Problem reading a line of text.
      return input;
   }

   std::istringstream str(line);

   str >> S.name >> S.midterm >> S.final;
   S.homework.clear();
   double x;
   while ( str >> x)
   {
      S.homework.push_back(x);
   }
   return input;
}

FWIW,我无法复制您遇到的问题。在 http://ideone.com/13wHLa.

查看工作版本

当涉及到失败的浮点格式输入时,它们如何实现标准的实现之间存在不一致。

clang(或更准确地说,libc++)读取并丢弃有效浮点表示可能包含的所有字符,即使它不能在此位置包含它们并且转换必然失败。这些字符包括CA(大写和小写,因为它们是十六进制数字,标准实际上允许十六进制浮点数表示法)。因此,当尝试读取 double 并且输入包含 Carl 时,字符 CA 将被读取并丢弃,即使浮点数不能以任何一个开头这些字符。

另一方面,gcc (libstdc++) 在明确转换将失败后立即停止读取。因此,如果输入包含 Carl,则转换在第一个字符处停止(并保留在流中),因为十六进制浮点数不能以 C 开头(必须以 [= 开头) 22=]).

我不会自愿就哪种实施在形式上是正确的提出意见。不管它是什么,正常的代码都应该避开语言的微妙和神秘的角落。如果学生记录是一行,那么代码应该读取行。如果一个学生记录被定义为"a name and a sequence of numbers that last until the next name",那么停下来阅读this article.