Thread: NOOB: Need a little help opening file and manipulating data

  1. #1
    Registered User
    Join Date
    Feb 2006
    Posts
    38

    Question NOOB: Need a little help opening file and manipulating data

    Basis for the code:

    Using a .txt file with names, grades (assignments & tests), and attendance, determine average (then pass/fail), and determine pass/fail based on attendance.

    Anything under a 60 = fail.
    Attendance under 30=fail.

    Here's what I have:

    Code:
    //This program averages grades and determines pass/fail.
    //It also determines pass/fail through attendance
    
    #include <iostream>
    #include <fstream>
    using namespace std;
    
    int main()
    {
    	
    
    	ifstream inFile;
    	
    	int first, last, a1, a2, a3, a4, t1, t2, attendance;
    	double test, assignment, average;
    
    	inFile.open("d:\\grades.txt");				//Using d: because I have no a: drive
    	cout <<"Reading Data from file."<<endl;
    	
    	//Average first set of grades
    	inFile >> first;
    	inFile >> last;
    	inFile >> a1;
    	inFile >> a2;
    	inFile >> a3;
    	inFile >> a4;
    	inFile >> t1;
    	inFile >> t2;
    	inFile >> attendance;
    	assignment = (a1+a2+a3+a4)/4;
    	test = (t1+t2)/2;
    	average = (assignment + test)/2;
    	cout <<first<<last<<"average is: "<<average<<endl;
    	if (average < 60)
    		cout<<"\nStudent has failed to meet grade requirements.";
    	if (average >= 60)
    		cout<<"\nStudent has passed the grade requirements.";
    	if (attendance < 30)
    		cout<<"\nStudent has failed due to poor attendance.";
    	if (attendance >=30)
    		cout<<"\nStudent has passed the attendance requirement.";
    
    
    
    //Close the file
    	inFile.close();
    	cout<<"\nDone.\n";
    	return 0;
    }
    Last edited by liquidcourage1; 02-26-2006 at 08:40 PM.

  2. #2
    Registered User
    Join Date
    Feb 2006
    Posts
    38
    I'm getting really weird numbers. I think I have some sort of overflow or data still left in memory. I'm not sure how to fix this.

    Thanks in advance.

  3. #3
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Can I see you text file? From what it looks like First and Last appear to be names, yet you declared them as integers? That would be a problem.
    Sent from my iPadŽ

  4. #4
    Registered User
    Join Date
    Feb 2006
    Posts
    38
    .txt file:

    Barney Jones 95 83 92 85 87 85 29
    Sandra King 99 97 92 91 92 95 32
    Josh Harding 0 0 0 0 31 37 30
    That's probably why I'm getting funky numbers. So how do I declare them in the code AND make sure that they are read in sequence?

  5. #5
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Code:
    char first[15];
    
    inFile.getline(first, 15, ' ');
    Sent from my iPadŽ

  6. #6
    Registered User
    Join Date
    Feb 2006
    Posts
    38
    I think I may have solved part of it... Thanks for the heads up on the int and the names...

    Code:
    //This program averages grades and determines pass/fail.
    //It also determines pass/fail through attendance
    
    #include <iostream>
    #include <fstream>
    #include <iomanip>
    using namespace std;
    
    int main()
    {
    	
    
    	ifstream inFile;
    	
    	char first[13], last[13];
    	int a1, a2, a3, a4, t1, t2, attendance;
    	double test, assignment, average;
    
    	inFile.open("d:\\grades.txt");				//Using d: because I have no a: drive
    	cout <<"Reading Data from file.\n\n"<<endl;
    	
    	//Average first set of grades
    	inFile >> first;
    	inFile >> last;
    	inFile >> a1;
    	inFile >> a2;
    	inFile >> a3;
    	inFile >> a4;
    	inFile >> t1;
    	inFile >> t2;
    	inFile >> attendance;
    	assignment = (a1+a2+a3+a4)/4;
    	test = (t1+t2)/2;
    	average = (assignment + test)/2;
    	cout <<first<<" "<<last<<" average is: "<<average<<endl;
    	if (average < 60)
    		cout<<"\nFAILED: grade requirements.\n";
    	if (average >= 60)
    		cout<<"\nPASS: grade requirements.\n";
    	if (attendance < 30)
    		cout<<"\nFAILED: poor attendance.\n";
    	if (attendance >=30)
    		cout<<"\nPASS: attendance requirement.\n";
    	cout<<setfill('-')<<setw(80)<<"-"<<endl;
    
    	
    
    
    //Close the file
    	inFile.close();
    	cout<<"\nDone.\n\n";
    	return 0;
    }

  7. #7
    Registered User
    Join Date
    Feb 2006
    Posts
    38
    Quote Originally Posted by SlyMaelstrom
    Code:
    char first[15];
    
    inFile.getline(first, 15, ' ');
    a little lost on theis part of the code... what does the:

    ' '

    do? does this give it a maximum of 15, but stop at the first space?

  8. #8
    Registered User
    Join Date
    Feb 2005
    Posts
    59
    It uses a space as the delimiter instead of the newline character.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. manipulating fgetc while reading a file
    By agentsmith in forum C Programming
    Replies: 1
    Last Post: 04-10-2008, 01:52 PM
  2. How To Generate Audio Data From A Wav File
    By mrchu in forum C++ Programming
    Replies: 10
    Last Post: 03-06-2008, 03:00 PM
  3. File I/O Question
    By Achy in forum C Programming
    Replies: 2
    Last Post: 11-18-2005, 12:09 AM
  4. Inserting/Deleting Data Within Large File :: Win32
    By kuphryn in forum C++ Programming
    Replies: 2
    Last Post: 11-22-2003, 11:24 AM
  5. help with reading a data file
    By zipfur in forum C Programming
    Replies: 4
    Last Post: 11-02-2002, 06:50 PM