Reading from a ".txt" file

anyone here can help me on this ?

Page 1 of 1

2 Replies - 837 Views - Last Post: 02 August 2009 - 07:11 PM Rate Topic: -----

#1 tarmizi_adam2005  Icon User is offline

  • جوروترا

Reputation: 233
  • View blog
  • Posts: 837
  • Joined: 18-April 09

Reading from a ".txt" file

Post icon  Posted 02 August 2009 - 07:30 AM

Hello and good day to everyone. i've been doing this assignment for quite a while now. The questions are as follows :



Lab1 (Review for C++ & DS) List Implementation
EX1: Write C++ Program to (Using List):
- Read Student information (Name, Mat No, Gender, Course Code, Course Name, Qz1 15%, Qz2 15%, MT 20%, Proj 10%, FT 40%, Tot Mark 100%.
- Prepare and print a report1 which includes (Name, Mat No, Course Code, Tot Mark).
- Prepare and print a report2 which includes (Name, Mat No, Avrg for Quizzes).
- Find and print the average for the Tot Mark.

EX2: Write C++ Program to Implement EX1 using Linked List.
--------------------------------------------------------------------------------------


in the EX1 i've managed to do all exept the last part that says "Find and print the average for the Tot Mark"...i've been stuck on this one..


heres my code:



#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;

void outputLine(string, int, string,string, string,int, int, int, int,int);
void totalMark(string, int, string,string, string,int, int, int, int,int);
void avrQuiz(string,int,string,string, string,int, int, int, int,int);
void print();
void report_1();
void report_2();





int main()
{ 
	
	//ifstream myFile ("student.txt");
	cout << left<< setw(10)<<"Name"<<setw(10)<<"Matrix"<<setw(10)<<"Gender"<<setw(10)<<"Code"<<setw(15)<<"Course Name"<<setw(10)<<"Quiz1"<<setw(10)<<"Quiz2"<<setw(10)<<"MidTerm"<<setw(10)<<"Project"<<setw(10)<<"FinalTest"<<endl;
	cout <<"\n";
	
	print();
	
	
	cout <<"\n"<< endl;
	cout <<"\n"<< endl;
	
	cout << left<< setw(10)<<"Name"<<setw(10)<<"Matrix"<<setw(10)<</*"Gender"<<*/setw(10)<<"Code"<<setw(15)<</*"Course Name"<<*/setw(10)<<"Total Marks"<< endl;
	cout <<"\n";
	report_1();
	
	cout <<"\n"<< endl;
	cout <<"\n"<< endl;
	cout << left<< setw(10)<<"Name"<<setw(10)<<"Matrix"<<setw(10)<</*"Gender"<<*/setw(10)<</*"Code"<<setw(15)<</*"Course Name"<<setw(10)<<*/"Average Quiz"<< endl;
	cout <<"\n";
	report_2();
						   
						   
	   
	
	   
	   
	  // myFile.close();
	
	
	
	
	system("pause");
	
	return 0;
	
}

void outputLine(string name, int matrix, string gender, string course, string courseName, int Q1, int Q2, int MT, int proj, int FT)
{
	 
	 cout << left<< setw(10)<< name <<setw(10)<<matrix <<setw(10)<< gender <<setw(10)<< course <<setw(13)<< courseName <<setw(15)<< Q1 <<setw(10)<< Q2 <<setw(10)<< MT <<setw(15)<< proj <<setw(15)<< FT << endl;
	 
}

void totalMark(string name, int matrix, string gender, string course, string courseName, int Q1, int Q2, int MT, int proj, int FT)
{
	 
	 int quiz1	 = Q1;
	 int quiz2	 = Q2;
	 int midTerm   = MT;
	 int project   = proj;
	 int finalTest = FT;
	 int totalMarks = quiz1+quiz2+project+finalTest;
	 cout << left<< setw(10)<< name <<setw(10)<<matrix <<setw(10)<< /*gender <<setw(10)<< */course <<setw(13)<</* courseName <<*/setw(25)<< totalMarks << endl;
	 
}

void avrQuiz(string name, int matrix, string gender, string course, string courseName, int Q1, int Q2, int MT, int proj, int FT)
{
	 int quiz1	 = Q1;
	 int quiz2	 = Q2;
	 double average = (quiz1+quiz2)/2;
	 cout << left<< setw(10)<< name <<setw(10)<<matrix <<setw(10)<< /*gender <<setw(10)<< course <<setw(13)<< courseName <<*/setw(25)<< average << endl;
	 
}

void print()

{   char name[30];
	int  matrix;
	int  Q1;
	int  Q2;
	int  MT;
	int  proj;
	int  FT;
	char gender[10];
	char course[20];
	char courseName[20];
	 
	 
   ifstream myFile ("student.txt");
   if(!myFile)
	{
	   cout << "error" << endl;
	}
	   while(myFile >> name >> matrix >> gender >> course >> courseName >> Q1 >> Q2 >> MT >> proj >> FT)
		  outputLine(name,matrix,gender,course, courseName,Q1,Q2,MT,proj,FT);
}

void report_1()
{
	   
   char name[30];
	int  matrix;
	int  Q1;
	int  Q2;
	int  MT;
	int  proj;
	int  FT;
	char gender[10];
	char course[20];
	char courseName[20];
	int totalMarks;
	
	  
   ifstream myFile ("student.txt");
   if(!myFile)
	{
	   cout << "error" << endl;
	}
	   while(myFile >> name >> matrix >> gender >> course >> courseName >> Q1 >> Q2 >> MT >> proj >> FT)
		  totalMark(name,matrix,gender,course, courseName,Q1,Q2,MT,proj, FT);
}

void report_2()
{
	   
   char name[30];
	int  matrix;
	int  Q1;
	int  Q2;
	int  MT;
	int  proj;
	int  FT;
	char gender[10];
	char course[20];
	char courseName[20];
	double  average;
	
	  
   ifstream myFile ("student.txt");
   if(!myFile)
	{
	   cout << "error" << endl;
	}
	   while(myFile >> name >> matrix >> gender >> course >> courseName >> Q1 >> Q2 >> MT >> proj >> FT)
		  avrQuiz(name,matrix,gender,course, courseName,Q1,Q2,MT,proj, FT);
}


The "student.txt" is as follows:

Tarmizi 0634719 male ECE4123 Electronic3 10 10 20 9 30
Hafiz 0632345 male ECE3232 C++ 12 11 16 4 35
Farhat 0631912 male ECE3101 Lab1 14 14 12 5 25
Ridwan 0322193 male MTH2212 Math4 10 12 15 6 33
John 2378543 male MGT3132 Economics 12 11 13 7 38
Ali 3245435 male MGT3050 EconomicsII 14 15 17 8 32
Aminah 4355634 female GEN4100 Ethics 15 15 19 7 36
Zainab 3256322 female ECE3102 Lab2 11 13 17 6 32
Fatima 4355786 female MTH3112 Math5 12 12 16 6 34




For the "student.txt" the sequence from left to right are Name, Mat No, Gender, Course Code, Course Name, Qz1 15%, Qz2 15%, MT 20%, Proj 10%, FT 40%, Tot Mark 100%. I know my code is quite choppy and without any comments. U can compile it using Dev c++. I just dont have any idea on how to read all and sum up all the total marks(last column) from the students.txt and find their average.
do i need to put all the total marks in some kinda of array or something ? please, any help would be most appreciated...

for the second question EX2, i would love to here any suggestions from you guys out there !

Is This A Good Question/Topic? 0
  • +

Replies To: Reading from a ".txt" file

#2 Plus  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 41
  • View blog
  • Posts: 414
  • Joined: 24-November 08

Re: Reading from a ".txt" file

Posted 02 August 2009 - 10:17 AM


char buffer[32];
char line[256];

ifstream File("File.txt");

while(!File.eof())
{
	  File >> buffer; // read 32 characters each time

	  File.ReadLine(line); // read a single line at time
}


Was This Post Helpful? 0
  • +
  • -

#3 tarmizi_adam2005  Icon User is offline

  • جوروترا

Reputation: 233
  • View blog
  • Posts: 837
  • Joined: 18-April 09

Re: Reading from a ".txt" file

Posted 02 August 2009 - 07:11 PM

i'll try that one out
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1