I have an assignment and I'm stuck. The program is supposed to read in names and test scores from a data file, compute the average, and display the letter grade.

This is the code that the teacher gave us in class:

#include <iostream>
#include <cstring>
#include <iomanip>
//
//      This program is designed to find the numerical score.
//      and print the letter grade accordingly a student's
//      name and five examination scores.
//
using namespace std;

int main()
{
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
//      Define all variables used in program:               //
//      name = name of student                              //
//      examn = indicates exam with n being exam number     //
//      score = numerical score                             //
//      letter = letter grade                               //
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//

        string name;
        int exam1, exam2, exam3, exam4, exam5, score;
        char letter;

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
//      Read in student information until                   //
//      end of file encountered                             //
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//

        while(cin>>name)
        {
                cin>> exam1 >> exam2 >> exam3 >> exam4 >> exam5;
                score = (exam1 + exam2 + exam3 + exam4 + exam5) / 5;
//
//              Determine letter grade
//
                switch(score/10)
                {
                        case 10:
                        case 9: letter = 'A';
                                break;
                        case 8: letter = 'B';
                                break;
                        case 7: letter = 'C';
                                break;
                        case 6: letter = 'D';
                                break;
                        case 5:
                        case 4:
                        case 3:
                        case 2:
                        case 1: letter = 'F';
                                break;
                }
                cout << "   " << setw(10) << name << "   "
                     << setw(4) << exam1
                     << setw(4) << exam2
                     << setw(4) << exam3
                     << setw(4) << exam4
                     << setw(4) << exam5
                     << setw(6) << score
                     << setw(6) << letter << endl;
//
//      Get next student record
//
}
}

I'm not sure what is wrong with it, but it's not working

Define "not working". Is this a verbatim copy and paste of what the instructor wrote? What's the question? if "not working" means that it doesn't read from a file, you're right. It doesn't do that. I imagine your job is to change it so it does read from a file.

She never really taught us how to get it to read in from the file. The one other example she gave us was lost when my school computer account stopped working.

We are running everything in terminal, and what I have in my notes is to use the format ./a.out<datafile.dat to insert the code.

My datafile reads:

Taurean
85 90 87 57 86

Mary
52 95 98 25 36

The output when I run the program is this:

Taurean   -4195148   368268-4195248-14286592-4521743     0

I hope this clarifies something. Or please direct me somewhere where I can get more information on datafiles

She never really taught us how to get it to read in from the file. The one other example she gave us was lost when my school computer account stopped working.

We are running everything in terminal, and what I have in my notes is to use the format ./a.out<datafile.dat to insert the code.

My datafile reads:

Taurean
85 90 87 57 86

Mary
52 95 98 25 36

The output when I run the program is this:

Taurean   -4195148   368268-4195248-14286592-4521743     0

I hope this clarifies something. Or please direct me somewhere where I can get more information on datafiles

I get these results:

Taurean 85 90 87 57 86 81 B
Mary 52 95 98 25 36 61 D

Try initializing exam1, exam2, exam3, exam4, exam5 originally to some value like 9999 and see if 9999 shows up on the display. If so, the numbers aren't getting from the file to the program correctly. It worked for me though. Are you sure that's what you're supposed to do though? You're not supposed to use ifstream?

using console redirection is not how to read from a text file, http://www.cplusplus.com/doc/tutorial/files.html

is an example of how to read from a text file although it incourages bad things such as while ( !eof ) and .is_open().

But thats for another day

Chris

I get these results:


Try initializing exam1, exam2, exam3, exam4, exam5 originally to some value like 9999 and see if 9999 shows up on the display. If so, the numbers aren't getting from the file to the program correctly. It worked for me though. Are you sure that's what you're supposed to do though? You're not supposed to use ifstream?

As far as I know thats what I'm supposed to do. She does a horrible job explaining things and she never showed us ifstream. When I just input names and numbers manually I now see that it works. How do I get the program to read the datafile?

Ok I did some searching and I tried to get it working using ifstream, and i get this error

student.cpp:72: error: expected declaration before '}' token

Here is my updated code:

#include <iostream>
#include <cstring>
#include <iomanip>
#include <fstream>
//
//      This program is designed to find the numerical score.
//      and print the letter grade accordingly a student's
//      name and five examination scores.
//
using namespace std;

int main()
{
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
//      Define all variables used in program:               //
//      name = name of student                              //
//      examn = indicates exam with n being exam number     //
//      score = numerical score                             //
//      letter = letter grade                               //
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//

        string name;
        int exam1, exam2, exam3, exam4, exam5, score;
        char letter;

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
//      Read in student information until                   //
//      end of file encountered                             //
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
        ifstream infile;
        infile.open ("student.dat", ifstream::in);

        while(cin>>name)
        {
                cin>> exam1 >> exam2 >> exam3 >> exam4 >> exam5;
                score = (exam1 + exam2 + exam3 + exam4 + exam5) / 5;
//
//              Determine letter grade
//
                switch(score/10)
                {
                        case 10:
                        case 9: letter = 'A';
                                break;
                        case 8: letter = 'B';
                                break;
                        case 7: letter = 'C';
                                break;
                        case 6: letter = 'D';
                                break;
                        case 5:
                        case 4:
                        case 3:
                        case 2:
                        case 1: letter = 'F';
                                break;
                }
                cout << "   " << setw(10) << name << "   "
                     << setw(4) << exam1
                     << setw(4) << exam2
                     << setw(4) << exam3
                     << setw(4) << exam4
                     << setw(4) << exam5
                     << setw(6) << score
                     << setw(6) << letter << endl;
//
//      Get next student record
//
}
        infile.close();
}
}

count your opening and closing brackets

I just fixed that but it's still not reading in from the data file. Did I do the code correctly?

I just fixed that but it's still not reading in from the data file. Did I do the code correctly?

while(cin>>name)
{
cin>> exam1 >> exam2 >> exam3 >> exam4 >> exam5;

Change cin to infile.

Thank you for your help, I got it working!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.