0

it's required to design a system that takes all the data relevant to the student such as, name,ID,no.of subjects ,and their marks. it's required that when i search for a specific student to show all the data about him and to calculate his gpa and print the result. i tried but the output wasn't as expected and here's my code: //how the data is read in the txt file`

void enter()
{
    ofstream file1("cairo.txt", ios::app);
    cout << "Enter the student's name : ";
    getline(cin, name);
    file1 << name << "\t";
    cout << "Enter the student's ID : ";
    cin >> std_num;
    file1 << std_num << "\t";
    cout << "Enter your department : ";
    cin.ignore();
    getline(cin, department);
    file1 << department << "\t";
    cout << "Enter the student's number for subjects : ";
    cin >> num_sub;
    file1 << num_sub << "\t";
    for (int i = 0; i < num_sub; i++) {
        cout << "please enter the student's " << i + 1 << " " << "mark \n";
        cin >> grades[i];
        file1 << grades[i] << "\t";
    }
    file1 << "\n";
    file1.close();}
void get_grades()
{
    char name[30], id[10], dep[25], city[15], resd[15], zip[15];
    int nsub,nmarks[10];
    float sum = 0;
    ifstream gfile("students.txt");
    char rq[30];
    cout << "Enter the name of the required student to get his grades : ";
    cin.getline(rq, 25);
    while (!gfile.eof())
    {
        if (strcmp(name, rq) == 0)
        {
            gfile.getline(name, 30, '|');
            gfile.getline(id, 10, '|');
            gfile.getline(dep, 25, '|');
            gfile >> num_sub;
            for (int i=0; i < nsub; i++)
            {
                gfile1>>nmarks[i];
                sum += nmarks[i];
            }
            psum = (sum/(nsub*100));
            if (psum < 0.5)) cout<<" Your Grade is F "<<endl;
            else if (psum >= 0.5 && psum < 0.65) cout<<" Your Grade is D "<<endl;
            else if (psum >= 0.65 && psum < 0.75) cout<<" Your Grade is C "<<endl;
            else if (psum >= 0.75 && psum < 0.85) cout<<" Your Grade is B "<<endl;
            else cout<<" Your Grade is A "<<endl;
        }
    }

}
ahmed ali
  • 1
  • 2
  • What do you want and what do you actually get? – MikeCAT May 16 '20 at 10:46
  • Maybe related: [language agnostic - Is floating point math broken? - Stack Overflow](https://stackoverflow.com/questions/588004/is-floating-point-math-broken) – MikeCAT May 16 '20 at 10:46
  • Your code have this problem: [c++ - Why is iostream::eof inside a loop condition (i.e. `while (!stream.eof())`) considered wrong? - Stack Overflow](https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-i-e-while-stream-eof-cons) – MikeCAT May 16 '20 at 10:48
  • You forgot to initialize `nsub`. Also you may be forgot to initialize `sum` before doing calculation for each students. – MikeCAT May 16 '20 at 10:55
  • nsub is initialized when i pass the data to the file at the begining of the program. this is a part of the code , and sum i initialized it by 0 in it's declaration – ahmed ali May 16 '20 at 12:04
  • Please post a [Minimal, Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example). – MikeCAT May 16 '20 at 12:05
  • Show us how the Text file data is formatted.... – Sayed Muhammad Idrees May 16 '20 at 12:13
  • i modified the question in include how the data in read is the file @SayedMuhammadIdrees – ahmed ali May 16 '20 at 12:39

0 Answers0