Thread: File Input/Output ... Problems

  1. #1
    Registered User
    Join Date
    Nov 2003
    Posts
    2

    File Input/Output ... Problems

    My intention is to test for a data type token in the
    studentdata.txt file, either "HWK," "EXM," or "END."
    If it is HWK then the next number is added to a homework
    score array; if it is EXM, then the next number is added to
    an exam score array. After END is parsed, the inside while
    loop breaks and finishes the calculations for the particular
    student, and then outputs the information in an organized
    manner.

    The studentdata.txt file is set up like this:

    John Doe HWK 67 HWK 43 EXM 88 HWK 76 HWK 49 EXM 95 HWK 28 HWK 64 EXM 80 END
    Adam Rest HWK 48 HWK 82 EXM 98 HWK 73 HWK 55 EXM 100 HWK 68 HWK 54 EXM 70 END

    Et cetera. 6 homework scores, 3 exam scores. If you need the
    actual file, ask.

    No values are being added into the homework array because dataType == "HWK" and dataType == "END" are NEVER being true. Although, for testing, I printed out dataType and it printed out as "HWK," so I don't know why it is not working. Also, the while (dataType != "END") is never ending, because it seems as though "END" is never being put into dataType.

    I'd be grateful for any help. I'm new at C++, and my teacher isn't
    much help because he totally doesn't care, so if there is some
    stupid mistake, go easy on me. I'd appreciate any help beyond
    the span of human comprehension.

    Code:
    #include<iostream>
    #include<fstream>
    
    using std::cout;
    using std::cin;
    using std::endl;
    using std::ios;
    using std::ifstream;
    using std::ofstream;
    
    int main() {
    
    	ifstream fin;
    	ofstream fout;
    
    	char firstName[30]; //student's first and
    	char lastName[30];  //last name
    	char dataType[4];	//holds type of following score
    	int homework[6] = {0}; //array of student's homework scores
    	int exam[4] = {0};     //array of student's exam scores
    
    	float totalAvg = 0;	//Total weighted average
    	float examTotal = 0;//Exam scores totaled
    	float examAvg = 0;  //Exam average
    	float homeworkTotal = 0; //Homework scores totaled
    	float homeworkAvg = 0;   //Homework average
    	int score = 0;
    
    	int a = 0; //holds homework index
    	int	b = 0; //holds exam index
    
    	//Opens the data file
    	fin.open("studentdata.txt", ios::in);
    	fout.open("studentGrades.txt", ios::out);
    
    	//Tests to see if the input file were opened.
    	//If opened correctly, we move on.
    	if (!fin) {
    		cout << "File could not be opened for input.";
    		exit(1);
    	}
    
    	//Tests to see if the output file were opened/created.
    	//If opened/created correctly, we move on.
    	if (!fout) {
    		cout << "File could not be opened/created for output.";
    		exit(1);
    	}
    
    	//Outputs headings to output file.
    	fout << "Student Name" << "\t"
    		 << "Overall" << "\t"
    		 << "Homework Scores:" << "\t"
    		 << "Hwk Avg" << "\t"
    		 << "Exam Scores:" << "\t"
    		 << "Exam Avg" << "\t\n";
    
    	//Continue loop until end of file
    	while (!fin.eof()) {
    
    		//Takes student's name
    		fin >> firstName
    			>> lastName;
    
    		while (dataType != "END") {
    
    		fin >> dataType;
    
    		//Determines where to place score
    		if ( dataType  == "HWK" ) 
    		{
    			fin >> homework[a];
    			a++;
    		}
    		else if ( dataType == "EXM" ) 
    		{
    			fin >> exam[b];
    			b++;
    		}
    		}
    
    		//Determines homework total
    		for (int e = 0; e <= 5; e++) {
    			homeworkTotal += homework[e];
    		}
    		//Determines homework average
    		homeworkAvg = homeworkTotal / 6;
    
    		//Determines exam total
    		for (int f = 0; f <= 2; f++) {
    			examTotal += exam[f];
    		}
    		//Determines exam average
    		examAvg = examAvg / 3;
    
    		//Determines total weighted average
    		totalAvg = (examAvg * .6) + (homeworkAvg * .4);
    
    
    
    		//Outputs student's name to the output file
    		fout << firstName << " "
    			 << lastName << "\t";
    
    		//Outputs the student's total weighted average
    		fout << totalAvg << "\t";
    
    		//Outputs the student's homework scores
    		for (int c = 0; c <= 5; c++) {
    			fout << homework[c] << " ";
    		}
    
    		//Outputs the student's homework average
    		fout << "\t\t" << homeworkAvg << "\t";
    
    		//Outputs the student's exam scores
    		for (int d = 0; d <= 2; d++) {
    			fout << exam[d] << " ";
    		}
    
    		//Outputs the student's exam average
    		fout << "\t\t" << examAvg << "\n";
    
    	}
    
    
    
    	fin.close();
    	fout.close();
    
    	return 0;
    
    }
    Last edited by FAMOUS; 11-09-2003 at 09:36 PM.

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Code:
    		while (dataType != "END") {
    
    		fin >> dataType;
    
    		//Determines where to place score
    		if ( dataType  == "HWK" ) 
    		{
    			fin >> homework[a];
    			a++;
    		}
    		else if ( dataType == "EXM" )
    You can compare C++ style strings your way, but not char arrays. Use strcmp() for char arrays:
    Code:
    #include <cstring>
    .
    .
    .
    		while ( strcmp( dataType,"END" ) != 0 ) {
    
    		fin >> dataType;
    
    		//Determines where to place score
    		if ( strcmp( dataType, "HWK" ) == 0 ) 
    		{
    			fin >> homework[a];
    			a++;
    		}
    		else if ( strcmp( dataType,"EXM" ) == 0 )

  3. #3
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    To elaborate on WHY that fails, remember that arrays and pointers are essentially the same thing, and that a pointer is an ADDRESS.

    So, for example, this:
    if (dataType == "HWK")

    Really means "If the array named datatype begins at the same memory location as the string literal "HWK" begins at..."

    And this will never, ever return true, because dataType is allocated on the stack, and "HWK" exists somewhere with other constants and string literals.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

  4. #4
    Registered User
    Join Date
    Nov 2003
    Posts
    2
    Thanks, Swoopy, Cat, for the help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Basic text file encoder
    By Abda92 in forum C Programming
    Replies: 15
    Last Post: 05-22-2007, 01:19 PM
  2. segmentaion fault with File Input/Output
    By sara.stanley in forum C Programming
    Replies: 7
    Last Post: 04-04-2006, 03:57 AM
  3. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM
  4. File I/O problems!!! Help!!!
    By Unregistered in forum C Programming
    Replies: 4
    Last Post: 05-17-2002, 08:09 PM