I have a problem with stoi where I'm using a getline from a csv file and getting the date,country,cases,death then using getline to get the date separated between month ,day, and year. However, I need to convert these dates to int, for some reason stoi breaks the code.

void fileOpener(){
    ifstream file;
    file.open("WHO-COVID-data.csv");
    // stringstream ss;
    string date1;
    string country1;
    string cases1;
    string death1;
    string month;
    string day;
    string year;
    // string tempCountry;
    string line;
    // int num1 = 0;
    // int num2 = 0;
    vector<string> row;
    vector<string> info;
    
    while(!file.eof()){
        getline(file,date1,',');
        getline(file,country1,',');
        getline(file,cases1,',');
        getline(file,death1,'\n');

        stringstream ss = stringstream(date1);
        getline(ss,month,'/');
        getline(ss,day,'/');
        getline(ss,year,'/');
        cout << month << "," << day << "," << year << endl;

        examineString(month);
        cout << ",";
        examineString(day);
        cout << ",";
        examineString(year);
        cout << endl;
        int x = stoi(month);
        cout << x << "," << stoi(day) << "," << stoi(year) << endl;

    }
}

Inside CSV file

1/7/20,Afghanistan,23,4
1/8/20,Afghanistan,1534,345
1/9/20,Afghanistan,234,23
1/10/20,Afghanistan,563,63
1/12/20,Afghanistan,789,78

Expecting that it stoi the month, year, day

1

There are 1 best solutions below

0
Monogeon On

the code that you have there does not line up with the problem you have. I copied the code perfectly and was not able to reproduce your problem. The code i copied:

void fileOpener(){
    ifstream file;
    file.open("WHO-COVID-data.csv");
    // stringstream ss;
    string date1;
    string country1;
    string cases1;
    string death1;
    string month;
    string day;
    string year;
    // string tempCountry;
    string line;
    // int num1 = 0;
    // int num2 = 0;
    vector<string> row;
    vector<string> info;

    while(!file.eof()){
        getline(file,date1,',');
        getline(file,country1,',');
        getline(file,cases1,',');
        getline(file,death1,'\n');

        stringstream ss = stringstream(date1);
        getline(ss,month,'/');
        getline(ss,day,'/');
        getline(ss,year,'/');

        int x = stoi(month);
        cout << x << "," << stoi(day) << "," << stoi(year) << endl;
    }
    file.close();
}

with the csv:

1/7/20,Afghanistan,23,4
1/8/20,Afghanistan,1534,345
1/9/20,Afghanistan,234,23
1/10/20,Afghanistan,563,63
1/12/20,Afghanistan,789,78

gave me the result:

1,7,20
1,8,20
1,9,20
1,10,20
1,12,20

So everything here, as i understand works fine. Now you can probably see i didn't use the 'examinestring()' function. I dont know what that is, and it's probably your own code that you didn't add here to your question, so i skipped it.

Now i understand that you do have a problem and here are a few things you should check/look out for:

  • make sure your file exists. If the code can't find the file, it wont be able to do stoi on the numbers which then will throw stoi exception.

  • make sure that the examinestring() function is not the thing thats throwing the stoi error

  • when opening files make sure to close them after

Also personally i'm not a big fan of that way of gathering data from files. I usually use this way:

void fileOpener(){
    ifstream file;
    file.open("WHO-COVID-data.csv");
    if (file.is_open())
    {
        int pos;
        string line;
        while(getline(file,line))
        {
            pos = line.find(',');
            string date = line.substr(0,pos);
            line = line.substr(pos+1);

            //
            pos = date.find('/');
            int month = stoi(date.substr(0,pos));
            date = date.substr(pos+1);

            pos = date.find('/');
            int day = stoi(date.substr(0,pos));
            date = date.substr(pos+1);

            int year = stoi(date);
            //

            pos = line.find(',');
            string country = line.substr(0,pos);
            line = line.substr(pos+1);

            pos = line.find(',');
            int cases = stoi(line.substr(0,pos));
            line = line.substr(pos+1);

            int deaths = stoi(line.substr(0,pos));


            //now here you have all those variables above for any type of use.
            cout << month << "," << day << "," << year << endl;
            cout << country << " " << cases << " " << deaths << endl;
        }
    }
    file.close();
}

Even though this above code looks a bit intimidating it's just a quick draft, it has a lot of potential for case improvement. Maybe it'll suit you better, maybe not, just throwing it out there.