Need a little help with my Hw, involving reading from files

This is a discussion on Need a little help with my Hw, involving reading from files within the C++ Programming forums, part of the General Programming Boards category; I wrote my code last night. It is a program that reads from the following text file: "Grades_2008.txt" Smith Mary ...

  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    18

    Need a little help with my Hw, involving reading from files

    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
    Last edited by green11420; 03-27-2008 at 10:06 AM. Reason: left something out.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,717
    class_average = "0.0" should be class_average = 0.0f since "0.0" is a string literal, but you want a float literal.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    My guess would be that line 18 is this one:
    Code:
    class_average = "0.0";
    Perhaps removing the " would solve that.

    Until you fix the compiler error, it won't read the file at all, since it's not producing an executable file....

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    Registered User
    Join Date
    Mar 2008
    Posts
    18
    I've tried both of those and I get this message

    Warning W8004 Grade_book2008.cpp 18: 'class_average' is assigned a value that is never used in function main()

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by green11420 View Post
    I've tried both of those and I get this message

    Warning W8004 Grade_book2008.cpp 18: 'class_average' is assigned a value that is never used in function main()
    I guess that means that the variable isn't being used, so can be removed. What do you think it means?

    And since you are using total_class_average to accumulate the averages for the class, you may want to initialize that to zero.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,717
    The compiler is just informing you that you initialised class_average with 0.0 or 0.0f, but the first time you use it:
    Code:
    class_average = total_class_average / num_of_students;
    the 0.0 or 0.0f is replaced by another value, so setting class_average to 0.0 or 0.0f before that was rather useless.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Mar 2008
    Posts
    18
    class_average = total_class_average / num_of_students;

    If i do remove it, it calls class_average and "undefined symbol...

  8. #8
    Registered User
    Join Date
    Mar 2008
    Posts
    18
    Quote Originally Posted by laserlight View Post
    The compiler is just informing you that you initialised class_average with 0.0 or 0.0f, but the first time you use it:
    Code:
    class_average = total_class_average / num_of_students;
    the 0.0 or 0.0f is replaced by another value, so setting class_average to 0.0 or 0.0f before that was rather useless.
    ok I got it. Thanks.

  9. #9
    Registered User
    Join Date
    Mar 2008
    Posts
    18
    Well it compiles fine now, but when I run it, it just keeps on looping it seems.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linking header files, Source files and main program(Accel. C++)
    By Daniel Primed in forum C++ Programming
    Replies: 3
    Last Post: 01-17-2006, 10:46 AM
  2. reading files
    By hiya in forum C++ Programming
    Replies: 7
    Last Post: 05-21-2005, 11:40 AM
  3. Reading data from consecutively named files
    By a1pro in forum C Programming
    Replies: 10
    Last Post: 04-15-2005, 01:48 AM
  4. Files (reading from)
    By zbap in forum C Programming
    Replies: 1
    Last Post: 04-04-2003, 09:43 AM
  5. Reading files with an unkown amount of characters
    By Zahl in forum C++ Programming
    Replies: 13
    Last Post: 10-10-2002, 02:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21