0

I am trying to calculate students grades from a file. When entering the if statement the debugger skips all my functions and ends the program. I think it has something to do with the file not being inputted well.

int main()
{
    string temp, out = "";
    ifstream RF("TestAnswers(5).txt");
    while (getline(RF, temp))
    {
        out += temp;
    }
    GradeCalc gCalc;
    gCalc.gradeAspects(out);
    gCalc.gradeCalculation();
    gCalc.grade();
}
class GradeCalc
{
private:
#define DELIM ',' 

    struct GradeAspects
    {
        string studentN;
        string answers;
        string firstname, lastname;
        double scores;
        int rsize;
        string percent;
        string readFile; // int read;
    };
    vector<GradeAspects> read;
    GradeAspects readFile[25];

public:
    void gradeAspects(string filename);
    void gradeCalculation();
    void grade();
};

void GradeCalc::gradeAspects(string filename)
{
    ifstream input;
    string line;
    int rnum = 0;
    char delim;
    int rsize;
    input.open(filename);
    if (input)
    {
        while (!input.eof())
        {
            getline(input, line);
            delim = line.find(DELIM);
            readFile[rnum].studentN = line.substr(0, delim);
            line = line.substr(delim + 1, line.length() - 1);
            delim = line.find(DELIM); // moving on to firstname by finding delim.
            readFile[rnum].firstname = line.substr(0, delim);
            line = line.substr(delim + 1, line.length() - 1);
            delim = line.find(DELIM);                        // moving on to lastname by finding delim.
            readFile[rnum].lastname = line.substr(0, delim); // last name
            line = line.substr(delim + 1, line.length() - 1);
            delim = line.find(DELIM);
            readFile[rnum].answers = line; // line = line.substr(delim + 1, line.length() - 1);
            rnum++;
        }
    }
    rsize = rnum;
}
void GradeCalc::gradeCalculation()
{ // once done with reads move to calculations of how many answers were right etc.
    int rnum = 0;
    int rsize = 0;
    string answers = readFile[0].answers;
    int aSize = answers.size();
    for (int i = 0; i < rsize; i++)
    {
        int score = 0;
        for (int t = 0; t < aSize; t++)
        {
            if (answers[t] == readFile[i].answers[i])
                score++;
            {
                answers[i] = score;
            }
        }
    }
}
void GradeCalc::grade()
{
    string line;
    int rnum = 0;
    int aSize = line.length();
    int choices{};
    int rsize = 0;
    int grade{};
    for (int i = 0; i < rsize; i++)
    {
        int score = 0;
        readFile[i].answers = choices;
        readFile[i].percent = grade;
        readFile[i].percent = (choices / aSize * 100);
        if (grade >= 90)
            readFile[rnum].firstname = "A";
        else if (grade > 80 && grade < 90)
            readFile[rnum].firstname = "B";
        else if (grade > 70 && grade < 80)
            readFile[rnum].firstname = "C";
        else if (grade < 70)
            readFile[rnum].firstname = "F";
        cout << "student" << readFile[rnum].firstname << " " << readFile[rnum].lastname << " had "
             << readFile[i].answers << " out of " << aSize << "and has a grade of" << grade << endl;
    }
}
Aconcagua
  • 24,880
  • 4
  • 34
  • 59
Colin
  • 1
  • 1
  • 6
    Can you [edit] the question, fix the formatting of the code, it is corrupted, and make sure that it meets all requirements for a [mre], as explained in Stackoverflow's [help]. Can you also be more precise as to which `if` statement you're referring to. There's more than one. See [ask] for more information. – Sam Varshavchik Jan 24 '23 at 15:44
  • 1
    Usually debuggers have separate commands for entering a function and skipping a function. These are usually called something like 'step into' and 'step over', so maybe you are just issueing the wrong command. – john Jan 24 '23 at 15:46
  • 1
    Please format your question well, otherwise readability suffers far too much. Adjusted the main function for you already (note how you can format code blocks by enclosing them within a pair of triple back-ticks `\`\`\`` – shorter code blocks you can indent with four spaces to get the same effect, too), please remove the rest of surplus back-ticks yourself. – Aconcagua Jan 24 '23 at 15:46
  • Looking at the code, most likely problem is that one of your `substr` calls is failing because it's going out of bounds on the string it's operating on. – john Jan 24 '23 at 15:48
  • Side note: About [`using namespace std`](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice)... – Aconcagua Jan 24 '23 at 15:49
  • Probably the input file cannot be found. – Sebastian Jan 24 '23 at 15:51
  • 2
    Do not use: macros!!! Use `const` or `constexpr` instead. – Marek R Jan 24 '23 at 15:53
  • [OT]: `if (condA) {/*..*/} else if (condB && !condA) {/*..*/}` can be simplified to `if (condA) {/*..*/} else if (condB) {/*..*/}`, i.e `&& grade < 90` can be removed. (also apply to 80 and 70 ;-) ) – Jarod42 Jan 24 '23 at 15:53
  • A lot of this makes little sense, and `GradeCalc::grade()` looks like it was put together without much thought about what it means – variables that are unused, a loop that can't be entered, assigning a grade letter to someone's fist name, ... – molbdnilo Jan 24 '23 at 15:59
  • Extending on @Jarod42: For the case that a grade is >= 90 then you end up in the very first `if` – and the `else` cannot get met any more – so *if* you get there you *know* already `grade` being `< 90`, so this condition is always true anyway. Thus you can leave it out. You can replay in thoughts the same with the general case (`condA` and `condB`)... – Aconcagua Jan 24 '23 at 16:00
  • `while (!input.eof())` --> `while (getline(input, line))`. Also, if the format of the file is comma-delimited, using `std::istringstream` is a whole lot easier than using `substr` looking for a comma. – PaulMcKenzie Jan 24 '23 at 16:20
  • I'm surprised this even compiles without a single `#include`. – Ted Lyngmo Jan 24 '23 at 16:41

0 Answers0