Output data to file

I'm trying to output data to a file. I half killed myself trying to get the program to work , everything works all data is working. Heres my code
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
// Week 13: In Class Exercise - 5 Solution
    #include <iostream>
    #include <string>
    #include <iomanip>
    #include <fstream>
    using namespace std;
    // Structure definition
    struct record
    {
    string lastName;
    float quiz1;
    float quiz2;
    double midterm, midterm_one;
    double final, final_one;
    double quiz_average;
    char final_grade;
    double total_grade;
    };
    const int NUMBER_SCORES = 5;
    // Function prototypes
    void programDescription();
    void writerecord(ofstream&, record);
    void readrecord(ifstream&, record&);
    void gradeAverage(record);
    int main()
    {
    // Program description
    programDescription();
    // Declare variables
    record stu1;
    ofstream outFile;
    ifstream inFile;
    // open file for input
    inFile.open("gradesIn.txt");
    if(inFile.fail())
    {
    cout << "Error opening file" << endl;
    exit(1);
    }
    // open file for output
    outFile.open("gradesOut.txt");
    if(outFile.fail())
    {
    cout << "Error opening file" << endl;
    exit(1);
    }
    // Process data as read and display in tabular format
    //Display header line
    cout << endl << endl
    << "Last Quiz Quiz Mid-Term Final" << endl
    << "Name 1 2 1 Exam" << endl
    << "------ ----- ----- -------- ------" << endl;
    //Loop to read in data
    //Loop to display array
    while (inFile >> stu1.lastName && !inFile.eof())
    {
    readrecord(inFile, stu1);
    // Display structure data
    cout << fixed << setprecision(2);
    cout << left << setw(14);
    cout << stu1.lastName << setw(9) << stu1.quiz1 << setw(10)
    << stu1.quiz2 << setw(12) << stu1.midterm << setw(15)
    << stu1.final << endl;
    writerecord(outFile, stu1);
    gradeAverage(stu1);
    }
    // Close data files
    inFile.close();
    outFile.close();
    return 0;
    }
    void programDescription()
    {
    cout << "Welcome to the program student record";
    cout << endl << "this program takes in 2quizes, a final and midyear";
    cout << endl << "and then calculates the average by what they are worth by";
    cout << endl << "the quizes together are worth 25% and midyear counts for 25%";
    cout << endl << "and the final counts for 50%";
    cout << endl;
    }
    // Modify function to write contents of entire array of structures to file
    void readrecord(ifstream& inFile, record& stu1)
    {
    inFile >> stu1.quiz1 >> stu1.quiz2 >> stu1.midterm >> stu1.final;
    }
    void writerecord(ofstream& outFile, record stu1)
    {
    outFile << left << setw(13) << stu1.lastName << setw(13)
    << stu1.quiz1 << setw(13) << stu1.quiz2 << setw(13) << stu1.midterm << setw(13)
    << stu1.final << stu1.final_grade << stu1.total_grade;
    }
    void gradeAverage(record stu1)
    {
    stu1.quiz_average = ((stu1.quiz1/10.) + (stu1.quiz2/10.))/2.*.25;
    stu1.final_one = (stu1.final/100)*.5;
    stu1.midterm_one = (stu1.midterm/100)*.25;
    stu1.total_grade = (stu1.quiz_average + stu1.final_one + stu1.midterm_one)*100;
    cout << endl << endl;
    cout << "Quiz 1: " << (stu1.quiz1/10)*100 << "%";
    cout << "Quiz 2: " << (stu1.quiz2/10)*100 << "%";
    cout << endl;
    cout << "Mid-Term Exam: " << stu1.midterm << "/100";
    cout << endl;
    cout << "Final Exam: " << stu1.final << "/100";
    cout << endl;
    cout << "Final Grade: " << stu1.total_grade << "%";
    cout << endl;
    cout << "Letter Grade: ";
    if (stu1.total_grade >= 90)
    cout << "A" << endl;
    else if (stu1.total_grade >= 80)
    cout << "B" << endl;
    else if (stu1.total_grade >=70)
    cout << "C" << endl;
    else if (stu1.total_grade >=60)
    cout << "D" << endl;
    else
    cout << "F" << endl;
    }




I am trying to output this section to the writerecord function

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
stu1.quiz_average = ((stu1.quiz1/10.) + (stu1.quiz2/10.))/2.*.25;
    stu1.final_one = (stu1.final/100)*.5;
    stu1.midterm_one = (stu1.midterm/100)*.25;
    stu1.total_grade = (stu1.quiz_average + stu1.final_one + stu1.midterm_one)*100;
    cout << endl << endl;
    cout << "Quiz 1: " << (stu1.quiz1/10)*100 << "%";
    cout << "Quiz 2: " << (stu1.quiz2/10)*100 << "%";
    cout << endl;
    cout << "Mid-Term Exam: " << stu1.midterm << "/100";
    cout << endl;
    cout << "Final Exam: " << stu1.final << "/100";
    cout << endl;
    cout << "Final Grade: " << stu1.total_grade << "%";
    cout << endl;
    cout << "Letter Grade: ";
    if (stu1.total_grade >= 90)
    cout << "A" << endl;
    else if (stu1.total_grade >= 80)
    cout << "B" << endl;
    else if (stu1.total_grade >=70)
    cout << "C" << endl;
    else if (stu1.total_grade >=60)
    cout << "D" << endl;
    else
    cout << "F" << endl;


Have not tried your code but this looks wrong

outFile << left << setw(13) << stu1.lastName << setw(13) << stu1.quiz1 << setw(13) << stu1.quiz2 << setw(13) << stu1.midterm << setw(13) << stu1.final << stu1.final_grade << stu1.total_grade;

try using
outfile >> Value;

If that doesn't work, change outfile to cout and make sure all the values are correct.
Last edited on
Topic archived. No new replies allowed.