1

I am trying to create an algorithm where I can enter an input.csv and output.csv. I have remade the code and changed some areas to try to fix the issue.

One issue I am having right now is whenever I convert a string to a double using stod(), it causes core dump issues when the program runs.


    #include <iostream>
    #include <fstream>
    #include <sstream>
    #include <vector>
    #include <algorithm>
    #include <numeric>
    #include <iomanip>
    
    using namespace std;
    
    // Function to split a string by a delimiter
    vector<string> split(const string& s, char delimiter) {
        vector<string> tokens;
        string token;
        istringstream tokenStream(s);
        while (getline(tokenStream, token, delimiter)) {
            tokens.push_back(token);
        }
        return tokens;
    }
    
    // Function to remove the lowest grade from a vector of grades
    void removeLowest(vector<double>& grades)
    {
        auto it = min_element(grades.begin(), grades.end());
        grades.erase(it);
    }
    
    int main(int argc, char* argv[])
    {
        if (argc != 3)
        {
            cerr << "Usage: " << argv[0] << " input_file output_file" << endl;
            return 1;
        }
    
        // Parse command line arguments
        string inputFile(argv[1]);
        string outputFile(argv[2]);
    
        // Open input file
        ifstream inFile(inputFile);
        if (!inFile.is_open())
        {
            cerr << "Error opening input file: " << inputFile << endl;
            return 1;
        }
    
        // Open output file
        ofstream outFile(outputFile);
        if (!outFile.is_open())
        {
            cerr << "Error opening output file: " << outputFile << endl;
            return 1;
        }
    
       // Read header line
    string header;
    if (!getline(inFile, header))
    {
        cerr << "Error reading input file: " << inputFile << endl;
        return 1;
    }
    // Parse maximum possible points for each lab
    string maxPointsLine;
    if (!getline(inFile, maxPointsLine))
    {
        cerr << "Error reading input file: " << inputFile << endl;
        return 1;
    }
    vector<double> maxPoints;
    for (const auto& token : split(maxPointsLine, ','))
    {
        maxPoints.push_back(stod(token));
    }
    
    
        // Write header line to output file
      
    
    
       outFile << "name,points_earned,points_possible" << endl;
    
        // Process each student line in input file
        string line;
        while (getline(inFile, line))
        {
          // Parse student name and grades
    auto tokens = split(line, ',');
    string name = tokens[0];
    vector<double> grades;
    for (size_t i = 1; i < tokens.size(); i++)
    {
        grades.push_back(stod(tokens[i]));
    }
    // Remove lowest grade
    removeLowest(grades);
    // Calculate earned points and possible points for this student
    double pointsEarned = accumulate(grades.begin(), grades.end(), 0.0);
    double pointsPossible = accumulate(maxPoints.begin(), maxPoints.end(), 0.0);
    
      // Write data to output file
    
    outFile << name << ",";
    outFile << fixed << setprecision(2) << pointsEarned << ",";
    outFile << fixed << setprecision(2) << pointsPossible << endl;
        // Close input and output files
        inFile.close();
        outFile.close();
    
        return 0;
    }
    
    }
Witch Kas
  • 11
  • 2
  • 3
    [`std::stod`](https://en.cppreference.com/w/cpp/string/basic_string/stof) can throw [exceptions](https://en.cppreference.com/w/cpp/string/basic_string/stof#Exceptions). Are you sure it's not what's happening? Have you used a [*debugger*](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems) to catch the crash and see when and where in your code it happens, and what kind of crash it is? – Some programmer dude Apr 20 '23 at 06:05
  • 1
    Also, what is the difference between the two programs you show? Why do you show two of them? Please take some time to read [the help pages](http://stackoverflow.com/help), take the SO [tour], read [ask], as well as [how to write the "perfect" question](https://codeblog.jonskeet.uk/2010/08/29/writing-the-perfect-question/), especially its [checklist](https://codeblog.jonskeet.uk/2012/11/24/stack-overflow-question-checklist/). Then [edit] your question to improve it, and give us more details. – Some programmer dude Apr 20 '23 at 06:07
  • You can load the core dump into a debugger and examine what happened. – Jesper Juhl Apr 20 '23 at 06:16
  • What does the input file actually look like? The 2 programs shown are processing the input file in very different ways. The 1st program expects the 1st line to contain only numbers, then the next line to start with a student name followed by numbers, then the next line to contain only numbers, then it exits. The 2nd program expects every line to contain only numbers, no student names. – Remy Lebeau Apr 20 '23 at 06:33
  • 1
    Note that a core dump is the *result*, not the *cause* of a crash. – Friedrich Apr 20 '23 at 06:45
  • 2
    The likely reason for the core dump is that the string you pass to `stod` cannot be converted to a double, for example if the string is empty, or if it is alphabetic. Your first thing to do is print out the strings are passing to `stod` to see if they are correct. – john Apr 20 '23 at 07:04
  • Did you write this code yourself, or did you copy it from somewhere? As noted above it's odd that you have written two programs which do two different things, when attempting to solve the same problem. – john Apr 20 '23 at 07:08
  • @Someprogrammerdude The two programs were a part of me trying to find out what was exactly causing the issue. So I made an entirely different program that seeks to achieve the same result but in a slightly different fashion. – Witch Kas Apr 20 '23 at 18:40
  • @john I wasn't sure how to fix the issue at first so I redid the program multiple times from the start. I am still learning how to do this so I accidentally made a program that did something else with the same error – Witch Kas Apr 20 '23 at 18:43
  • @WitchKas Since you have not posted the file you are trying to read it is impossible to be sure. But my guess would be that your file reading and parsing code is bugged and this causes you to try convert strings that are not really numbers. – john Apr 21 '23 at 07:16
  • 1
    @WitchKas One of the most important skills you will learn is how to debug your own programs. It's admirable that you were prepared to write a whole new program to try and make progress but it doesn't strike me as a very efficient use of your time. It would have been better to first debug your existing code to find out what was wrong with it. When you know that then you are in a better position to decide what to do next. Even the world's greatest programmer cannot write bug free code, debugging is an essential part of the process. – john Apr 21 '23 at 07:19

0 Answers0