为什么iostream会用某些词语切掉第一个字母?

时间:2017-06-22 01:15:13

标签: c++ iostream

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

我有一个简单的程序,它从文件或控制台读取名称和一些等级到结构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 

返回

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,但它适用于GCC

3 个答案:

答案 0 :(得分:8)

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

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

另一方面,只要明确转换失败,gcclibstdc++)就会停止阅读。因此,如果输入包含Carl,则转换将在第一个字符处停止(并保留在流中),因为十六进制浮点数不能以C开头(它必须以{{开头) 1}})。

我不会自愿提出关于哪种实施方式正确无误的意见。无论它是什么,正常的代码应该避开语言的微妙和神秘的角落。如果学生记录是一行,那么代码应该读取行。如果学生记录被定义为“一个姓名和一系列数字,直到下一个名字”,则停止并阅读this article

答案 1 :(得分:1)

我认为问题在于使用end of linedouble解析输入。 我找到了两种解决方法:

  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. 如果您知道输入是整数

    ,请不要将其解析为double
    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;
    }
    

答案 2 :(得分:1)

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

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

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

    // 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处的工作版本。

相关问题