Reading and writing file

I am having trouble figuring out how to go about this. This is the prompt.


C++ program that will read from a file, "studentInfo.txt", the user ID for a student (first letter of their first name connected to their last name i.e. jSmith). Next it will need to read three integer values that will represent the 3 exam scores the student got for the semester. Once the values are read and stored in descriptive variables it will then need to calculate a weighted course average for that student.
Below is an example of what the calculation would be for the program.
score1 * .3 + score2 * .3 + score3 * .4

Inside the main function definition, declare your variables being sure to give them a descriptive name where it is easy for the reader of the code to know what is stored in each. Along with variables for the values you will be reading out of the file you will also need one set up to use with the input file and the output file. These also need to be descriptive so it is obvious as to which file is being used.

In this program the user will not be entering in any data as it will all come to from the file. Likewise, all output will be written to a file so the user will not see anything come to the screen. To keep from the user being confused as if anything happened or not it is always good to give them a ending statement i.e. Something like the following:
Program has completed and the output can be found in 'studentGrade.txt'
Save the contents of the file

This is what I have so far.
And this is the studentinfo.txt
ID SCORES
2437 61 92 79
9065 82 63 95
9816 71 92 59
4607 83 91 95
9747 69 99 61
2481 92 86 59
3893 62 70 71
5735 65 91 68
3735 98 77 68
8085 90 63 97
3247 72 96 89
6548 56 52 56
5859 100 99 68
4740 89 80 97
4424 72 72 69
5494 67 87 70
6114 71 87 96
5700 100 64 79
9890 93 92 92
6786 73 79 97

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
 #include <iostream>
#include <fstream>

using namespace std;

int main (){

ifstream input;

input.open("StudentInfo.txt");

int score1, score2, score3;

input>>score1;
input>>score2;
input>>score3;

cout << "Total score is " << ((score1*.3) + (score2*.3) + (score3*.4)) << endl;

input.close();

cout << "Done" << endl; 

ouput.open("Studentgrade.txt");
output << 

return 0;
}
1
2
3
4
5
6
7
input.open("StudentInfo.txt");

int score1, score2, score3;

input>>score1;
input>>score2;
input>>score3;


Well that's a problem. The first two entries in the file are "ID SCORES". Those are not int values. You're trying to read ID into score1, and SCORES into score2.
Last edited on
No that part is not in there. Its just to help explain the difference between the scores and ID
@soccer53 -- do you have full control over the formatting of input and output files? As Repeater said, if indeed the header looks like that, your current parsing code would choke.

So there's another slight issue with the input file matching up with the description -- wasn't the id supposed to look like "jsmith" or "bobama", or "dtrump" ? If indeed you have the freedom to design your input file as you want, completely remove that header and build it like so:

input file example
jsmith 61 92 79
bobama 82 63 95
dtrump 71 92 59
nportman 83 91 95


Similarly, you've omitted exactly how the output file should look -- would it just have the same id and then weighted average, one per line?

possible output?
jsmith 77.5
bobama 81.5
dtrump 72.5
nportman 90.2


Be specific and then you can code it exactly to spec.
in any case, just do a small example with cout to see if things appear to be correct
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

double CalculateGrade(int a, int b, int c)
{
    return a*0.3 + b*0.3 + c*0.4;
}

int main()
{
    ifstream ifs("StudentInfo.txt");
    string id;
    int a, b, c;
    while (ifs >> id >> a >> b >> c)
    {
        cout << "\"" << id << "\" => " << CalculateGrade(a,b,c) << endl;
    }
    return 0;
}
Last edited on
Ok,

then how would the output work?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

double CalculateGrade(int a, int b, int c)
{
    return a*0.3 + b*0.3 + c*0.4;
}

int main()
{
    ifstream ifs("StudentInfo.txt");
    string id;
    int a, b, c;
    while (ifs >> id >> a >> b >> c)
    {
        cout << "\"" << id << "\" => " << CalculateGrade(a,b,c) << endl;
    }
    
    
    


ofstream myfile ("StudentGrade.txt");
if (StudentInfo.is_open())

      //If statements in order to calculate letter grades.
      if (avg >= 90)
         letter = "A";
      else if (avg >= 80)
         letter = "B";
      else if (avg >= 70)
         letter = "C";
      else if (avg >= 60)
         letter = "D";
      else if (avg < 60)
         letter = "F";
   
      
    return 0;
    
}



This is what I have so far...
Last edited on
You're still using cout for output, but the problem statement is telling you to use a file.
Move your line 27 (ofstream line) to before your while loop, then replace "cout" with "myfile".

You're not telling us what you're output should look like. icy1 was guessing in his previous post.

Be specific

You tell us, what should the output look like? We can try to help you once we know.
Is the output:
<Id number 1> <letter grade 1>
<Id number 2> <letter grade 2>
<Id number 3> <letter grade 3>
...?

Perhaps you want to do something like this
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
double CalculateGrade(int a, int b, int c)
{
    return a*0.3 + b*0.3 + c*0.4;
}

char CalculateGradeLetter(double avg_grade)
{
    char letter;
      //If statements in order to calculate letter grades.
      if (avg_grade>= 90)
         letter = "A";
      else if (avg_grade >= 80)
         letter = "B";
      else if (avg_grade >= 70)
         letter = "C";
      else if (avg_grade >= 60)
         letter = "D";
      else
         letter = "F";

    return letter;
}

// call it somewhere like
myfile << "\"" << id << "\" => " << CalculateGradeLetter(CalculateGrade(a,b,c)) << endl

Last edited on
ID Number Letter Grade
Do I declare myfile?
Okay, then, going off what I wrote earlier along with what icy1 wrote, you might be able to something like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
int main()
{
    ifstream ifs("StudentInfo.txt");
    if (!ifs)
        std::cout << "Error opening input file" << std::endl;

    ofstream myfile ("StudentGrade.txt");

    string id;
    int a, b, c;
    while (ifs >> id >> a >> b >> c)
    {
        double number = CalculateGrade(a,b,c);
        char letter = CalculateGradeLetter(number);
        myfile << "\"" << id << "\" => " << number << " " << letter << endl;
    }
}


Fill in the gaps and post your current work if you need more help.
Last edited on
Wait I am really confused now
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <iostream>
#include <fstream>
#include <string>

using namespace std;
int avg;
int letter;


double CalculateGrade(int a, int b, int c)
{
   return a*0.3 + b*0.3 + c*0.4;
}


int main()
{
    ifstream ifs("StudentInfo.txt");
    if (!ifs)
        std::cout << "Error opening input file" << std::endl;

    ofstream myfile ("StudentGrade.txt");

    string id;
    int a, b, c;
    while (ifs >> id >> a >> b >> c)
    {
        double number = CalculateGrade(a,b,c);
        char letter = CalculateGradeLetter(number);
        myfile << "\"" << id << "\" => " << number << " " << letter << endl;
    }
}


This is what I have so far
Just confused on where all the code goes.

I keep getting errors
Last edited on
In function 'int main()': 29:50: error: 'CalculateGradeLetter' was not declared in this scope

How can you use a function that is not there ?
soccer53, I feel like you're just copying things without having any clue as to what they actually mean. I defined my CalculateGradeLetter in a similar style to how icy1 defined the CalculateGrade function, in an earlier post. Copy that code. Or better yet, understand how functions work in general.
Read: http://www.cplusplus.com/doc/tutorial/functions/
Topic archived. No new replies allowed.