0

'a program that will calculate numerical and letter grades for a student and output a report from given txt file to the program.'

student.txt file is

a b 3 100 100 100 0.15 3 75 80 90 0.40 3 60 70 70 0.45
c d 4 100 88 100 92 0.15 3 75 80 90 0.40 2 100 89 0.45
e f 3 79 82 77 0.15 3 80 82 93 0.40 3 86 90 76 0.45
g h 3 100 99 98 0.15 3 98 97 93 0.40 3 88 90 99 0.45
#include <string>
#include <fstream>
using namespace std;

int main()
{
    int i;
    const int maxCount = 10;

    string filename;

    cout << "which file do you want to open?:  ";
    cin >> filename;

    ifstream in(filename.c_str());

    if (!in) {
        cerr << "File can't be opened! " << endl;
        exit(1);
    }

    int count = 0;
    string line;

    while (getline(in, line)) //count the number of lines in file.
        count++;

    int SIZE = count;
    cout << "Size: " << SIZE;

    struct Records {
        string firstname;
        string secondname;

        int homework_grades;
        int homework_value[maxCount];
        float homework_total_grade;
        int program_grades;
        int program_values[maxCount];
        float program_total_grade;
        int exa_ms_grades;
        int exa_ms_values[maxCount];
        float exa_ms_total_grades;

    } record[SIZE];

    cout << "Test 1 :\n ";

    //read the all  records from file
    for (int i = 0; i < SIZE; i++) {
        in >> record[i].firstname >> record[i].secondname >> record[i].homework_grades;

        for (int j = 0; j < record[i].homework_grades; j++) {
            in >> record[i].homework_value[j];
        }

        in >> record[i].homework_total_grade >> record[i].program_grades;

        for (int j = 0; j < record[i].program_grades; j++) {
            in >> record[i].program_values[j];
        }

        in >> record[i].program_total_grade >> record[i].exa_ms_grades;

        for (int j = 0; j < record[i].exa_ms_grades; j++) {
            in >> record[i].exa_ms_values[j];
        }

        in >> record[i].exa_ms_total_grades;

        float homework_earned = 0;
        float total_homework_point = 0;

        float program_earned = 0;
        float total_program_point = 0;

        float exa_ms_earned = 0;
        float total_exa_ms_point = 0;

        for (int j = 0; j < record[i].homework_grades; j++) {
            homework_earned += record[i].homework_value[j];
            total_homework_point += 100;
        }

        for (int j = 0; j < record[i].program_grades; j++) {
            program_earned += record[i].program_values[j];
            total_program_point += 100;
        }

        for (int j = 0; j < record[i].exa_ms_grades; j++) {
            exa_ms_earned += record[i].exa_ms_values[j];
            total_exa_ms_point += 100;
        }
        //upto here program run perfectly...............................

        float final_grade = 0.0;
        final_grade = (((homework_earned / total_homework_point) * record[i].homework_total_grade) + ((program_earned / total_program_point) * record[i].program_total_grade) + ((exa_ms_earned / total_exa_ms_point) * record[i].exa_ms_total_grades));

        final_grade = final_grade * 100;
        char letter_grade = 'N';

        if (final_grade >= 90 or final_grade <= 100) {
            letter_grade = 'A';
        }
        else if (final_grade >= 80 or final_grade <= 89) {
            letter_grade = 'B';
        }
        else if (final_grade >= 70 or final_grade <= 79) {
            letter_grade = 'C';
        }
        else if (final_grade >= 60 or final_grade <= 69) {
            letter_grade = 'D';
        }
        else {
            letter_grade = 'F';
        }

        cout << record[i].firstname << " ";
        cout << record[i].secondname << " ";
        cout << final_grade << " ";
        cout << letter_grade << " ";
        string witty = "";
        switch (letter_grade) {
        case 'A':
            witty = "Most excellent work";
            break;
        case 'B':
            witty = "good work";
            break;
        case 'C':
            witty = "Average Work";
            break;
        case 'D':
            witty = "You are Pass";
            break;
        default:
            witty = "You are Failed";
            break;
        }
        cout << witty << "\n";
    }
    cout << "Test 3 :\n ";
    in.close();

    return 0;
}

error page

Yksisarvinen
  • 18,008
  • 2
  • 24
  • 52
B G
  • 29
  • 5
  • Please apply consistent indentation. – Yunnosch Oct 23 '20 at 09:00
  • Please debug your code until you're able to locate the problem and reduce your code to a [mcve]. Don't dump your whole code and expect others to debug it for you. – Thomas Sablik Oct 23 '20 at 09:02
  • You read (and ignore) all of the file for counting lines. Then you try continuing reading but do not check anything for success... – Yunnosch Oct 23 '20 at 09:02
  • 2
    `while (getline(in, line))//count the number of lines in file.` reads to the end of the file. Trying to read more later won't work. You either need to close and open it again or seek back to the beginning and clear any error flags on the stream. – Retired Ninja Oct 23 '20 at 09:03
  • Variable length arrays are still a C thing and not a C++ one despite gcc accepting them. If you want to write conformant and portable code do not use them and use vectors instead because that can lead to stupid bugs on stricter compilers. – Serge Ballesta Oct 23 '20 at 09:10
  • Moreover, using `std::vector` and `push_back()` instead of array would solve your problem - you wouldn't have to count lines. – Yksisarvinen Oct 23 '20 at 09:13

0 Answers0