I wrote my code last night. It is a program that reads from the following text file:
"Grades_2008.txt"
Smith Mary J. 79 94 81
Johnson Paul L. 88 76 83
Kerry John W. 92 86 89
Mitchell Susan K. 91 97 96
Kitchens Joe P. 90 76 81
Denver Mike U. 88 71 80
Peterson Kathy M. 85 84 91
Houston Maria K. 62 79 89
Riley Peter R. 53 66 61
McCann Patty M. 77 71 89
Kinston Bob T. 88 61 82
Benson Sarah F. 74 85 96
Wesley Pam K. 83 94 86
Cannon David P. 85 92 78
The program should read and display each name, and calculate and display the student's average grade. It should also display the final class average at the end.
Here's my code to start out with:
Code:
// File: Grade_book2008.cpp
// Author: ----------------
// Class: TR 5pm
// Date: 03/27/08
// Purpose: To calculate the average grade for each student
// and the final class average.
#include <fstream>
using namespace std;
int main ()
{
float student_grade1,
student_grade2,
student_grade3,
student_average,
total_class_average,
class_average = "0.0";
int num_of_students = 0;
string student_name;
ifstream in_file;
in_file.open("Grades_2008.txt", ios::in);
in_file >> student_name;
in_file >> student_grade1 >> student_grade2 >> student_grade3;
while(!in_file.eof())
{
num_of_students = num_of_students + 1;
student_average = student_grade1 + student_grade2 + student_grade3 / 3;
total_class_average = total_class_average + student_average;
cout << "Student" << "\t" << "Final" << "\n";
cout << "-------" << "\t" << "-----" << "\n";
cout << student_name << "\t" << student_average << "\n";
in_file >> student_name;
in_file >> student_grade1 >> student_grade2 >> student_grade3;
}
class_average = total_class_average / num_of_students;
cout << "Final Class Average is: " << class_average;
in_file.close();
return 0;
}
And this is the error message I am getting;
Borland C++ 5.5.1 for Win32 Copyright (c) 1993, 2000 Borland
C:\Users\Customer\Desktop\Spring 2008 documents\ITCS 1214\Grade_book2008.cpp:
Error E2034 Grade_book2008.cpp 18: Cannot convert 'char *' to 'float' in function main()
*** 1 errors in Compile ***
I guess it is not reading the file properly but I am not sure. What I am trying to do is display it as:
Student Final
---------- ----------
name XX.XX
name XX.XX
.
.
Final Class Average is: XX.XX
I would really appreciate your help to find out where I went wrong. I'm basing this code on a template; an example we did in class. So I don't know if I transitioned it properly. Thanks in advance.
edit- by the way, the error was reffering to the class_average up at the top