mardi 22 février 2022

output returns a "terminate called after throwing an instance of 'std::invalid_argument' what(): stoi" [closed]

Unrelated to my homework but when I run my codes, my output returns "terminate called after throwing an instance of 'std::invalid_argument' what(): stoi" at the end of the run. Wondering if any experts can explain why such thing is happening?

found out that if my getline(infile,test); is inside the while loop, I would not get the what() stoi error but my date will get totally messed up. tried to do some research on google but couldnt get a proper answer why that is happening

#include <iostream>
#include <string>
#include <sstream>
#include <fstream>
#include "Date.h"
#include "Time.h"
#include "Vector.h"

using namespace std;

typedef struct {
    Date d;
    Time t;
    float speed;
} WindLogType;

    ostream & operator <<( ostream & os, const WindLogType& wl );
    istream & operator >>( istream& input, WindLogType& wl );

int main()
{   Vector<WindLogType> windLogs;
    string day,month,year,hours,minss,strInput;
    string test;
    WindLogType windlogs2;
    ifstream infile("test.csv");
    if(!infile) return -1;

    infile >> windlogs2;
    getline(infile,test);
    ofstream outfile("outputFile.txt.");
    while (!infile.eof()){

    getline(infile,day, '/');
    windlogs2.d.SetDay(stoi(day));
    getline(infile,month, '/');
    windlogs2.d.SetMonth(stoi(month));
    getline(infile,year, ' ');
    windlogs2.d.SetYear(stoi(year));
    getline(infile,hours, ':');
    windlogs2.t.SetHours(stod(hours));
    getline(infile,minss,',');
    windlogs2.t.SetMins(stod(minss));
    getline(infile,strInput,',');
    getline(infile,strInput,',');
    getline(infile,strInput,',');
    getline(infile,strInput,',');
    getline(infile,strInput,',');
    getline(infile,strInput,',');
    getline(infile,strInput,',');
    getline(infile,strInput,',');
    getline(infile,strInput,',');
    getline(infile,strInput,',');
    windlogs2.speed = (stof(strInput));
    getline(infile,strInput,',');
    getline(infile,strInput,',');
    getline(infile,strInput,',');
    getline(infile,strInput,',');
    getline(infile,strInput,',');
    getline(infile,strInput,',');
    getline(infile,strInput);

    outfile << windlogs2 << endl;
    cout << windlogs2 << endl;
  }
    return 0;
}

    ostream & operator <<( ostream & os, const WindLogType& wl )
    {
        os << wl.d << wl.t << " Speed: " << wl.speed << '\n';
        return os;
    }
    istream & operator >>( istream& input, WindLogType& wl )
    {
        input >> wl.d >> wl.t >> wl.speed;
        return input;
    }

Sample of the codes used to run with this codes

https://i.stack.imgur.com/nESvO.png

Aucun commentaire:

Enregistrer un commentaire