【问题标题】:C++ using getline and stringstream with multiple classes and overloaded stream operatorC++ 使用带有多个类和重载流运算符的 getline 和 stringstream
【发布时间】:2018-04-26 17:02:54
【问题描述】:

我正在完成一项大学作业,从文件中读取数据,我真的需要一些帮助!

数据文件包含天气数据,如下所示:

31/03/2016 9:00,14.6,175,17,0,1013.4,1016.9,1017,0,68.2,6,512,22.7,24.1,25.5

作业需要一个类来读取和存储日期、时间和所有 int/double 值。我必须使用日期类来读取日期,使用时间类来读取时间,而 WeatherData 类使用重载的流运算符将它们组合在一起。

我正在使用包含日期、时间和一个双变量的数据文件对 WeatherData 类(将在 WeatherData 类数组中使用)进行单元测试:31/03/2016 9:00,14.6

我得到的输出是:31/3/2016 0:0 0

我对 Date 和 Time 类进行了单元测试,它们按预期工作。我不明白为什么它在日期之后失败。任何帮助将不胜感激!

TestWeatherData.cpp

#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <cstdlib>
#include "../Practice/date.h"
#include "../Practice/time.h"
#include "../Practice/weatherdata.h"

using namespace std;

istream & operator >>( istream & input, Date & D )
{
  string temp;
  string convertToInt;
  int tempDay;
  int tempMonth;
  int tempYear;

  getline(input, temp);
  stringstream ss(temp);
  getline(ss, convertToInt, '/');
  tempDay = atoi(convertToInt.c_str());
  D.SetDay(tempDay);
  getline(ss, convertToInt, '/');
  tempMonth = atoi(convertToInt.c_str());
  D.SetMonth(tempMonth);
  getline(ss, convertToInt);
  tempYear = atoi(convertToInt.c_str());
  D.SetYear(tempYear);

  return input;
}

ostream & operator <<( ostream & os, const Date & D )
{

  os << D.GetDay() << "/" << D.GetMonth()
     << "/" << D.GetYear();

  return os;
}

istream & operator >>( istream & input, Time & T )
{
  string temp;
  string convertToInt;
  int tempHours;
  int tempMinutes;

  getline(input, temp);
  stringstream ss(temp);
  getline(ss, convertToInt, ':');
  tempHours = atoi(convertToInt.c_str());
  T.SetHours(tempHours);
  getline(ss, convertToInt);
  tempMinutes = atoi(convertToInt.c_str());
  T.SetMinutes(tempMinutes);

  return input;
}

ostream & operator <<( ostream & os, const Time & T )
{

  os << T.GetHours() << ":" << T.GetMinutes();

  return os;
}

istream & operator >>( istream & input, WeatherData & W )
{
  string temp;
  string convertToDouble;
  Date tempDate;
  Time tempTime;
  double tempDP;

  input >> tempDate >> tempTime;
  W.setDate(tempDate);
  W.setTime(tempTime);
  getline(input, temp);
  stringstream ss(temp);
  getline(ss, convertToDouble);
  tempDP = atof(convertToDouble.c_str());
  W.setDP(tempDP);

  return input;
}

ostream & operator <<( ostream & os, const WeatherData & W )
{

  os << W.getDate() << ' ' << W.getTime() << ' ' << W.getDP();

  return os;
}

int main()
{
    ifstream infile( "data.txt" );
    if( !infile ) return -1;

    WeatherData W;

    infile >> W;

    cout << W;

    return 0;
}

【问题讨论】:

  • 通过一个重要提示:DateTime 类的每个 operator&gt;&gt; 重载在输入流上调用 std::getline - 但两者的数据实际上都在同一行。
  • 我不确定你的意思是 G.M.对不起,我对此很陌生!你的意思是文本文件中的数据吗?如果是这样,我不允许更改文本文件的结构。
  • 一旦获得日期,整行已被读取且无法再次使用,因此时间没有可读取的内容。每行只能在输入上使用一次 getline。

标签: c++ getline stringstream


【解决方案1】:

感谢 Jerry Jeremiah 和 G.M!我现在可以看到我正在阅读 Date 类中的整行,所以在第一个 getline 我添加了一个空格字符的分隔符。在第一个 getline 的 Time 类中,我添加了逗号分隔符。现在它按预期工作!

  getline(input, temp, ' '); // added delimiter here
  stringstream ss(temp);
  getline(ss, convertToInt, '/');
  tempDay = atoi(convertToInt.c_str());
  D.SetDay(tempDay);
  getline(ss, convertToInt, '/');
  tempMonth = atoi(convertToInt.c_str());
  D.SetMonth(tempMonth);
  getline(ss, convertToInt);
  tempYear = atoi(convertToInt.c_str());
  D.SetYear(tempYear);
  return input;

  getline(input, temp, ','); // added delimiter here
  stringstream ss(temp);
  getline(ss, convertToInt, ':');
  tempHours = atoi(convertToInt.c_str());
  T.SetHours(tempHours);
  getline(ss, convertToInt);
  tempMinutes = atoi(convertToInt.c_str());
  T.SetMinutes(tempMinutes);
  return input;

【讨论】:

    猜你喜欢
    • 2013-04-28
    • 1970-01-01
    • 2020-06-18
    • 1970-01-01
    • 1970-01-01
    • 2010-11-27
    • 2016-02-19
    • 1970-01-01
    相关资源
    最近更新 更多