I am doing a program to get a sample infile.txt & ouput to outfile.txt
I encountered alot of probs which I can't solve now. I am having issues reading from infile.txt & display the correct result in outfile.txt Pls enlighten me further. The program have to submit this Tue.

My code:

#include <iostream>
#include <fstream>
#include <cstdlib>

using namespace std;

const int MAX = 50;

enum SEX {Male, Female};

struct MaleStudent
{
	char name;
	int mark1;
	int mark2;
	int mark3;
};

struct FemaleStudent
{
	char name;
	int mark1;
	int mark2;
	int mark3;
	int mark4;
};

union Student
{
	MaleStudent m;
	FemaleStudent f;
};

struct UOWSIM
{
	SEX gen;
	Student st;
	int result;
	char grade [MAX];	 
};


int FileToArray (fstream&, char [], UOWSIM []);
void arrayToOutfile (fstream&, char [], UOWSIM [], int);

int main ()
{
	fstream afile;
	UOWSIM p [MAX];
	
	
	UOWSIM stu;
	
	
	int size = FileToArray (afile, "infile.txt", p);
	arrayToOutfile (afile, "outfile.txt", p, size);
}

int FileToArray (fstream& afile, char fileName [], UOWSIM p [])
{
	afile.open (fileName, ios::in);
	
	if (!afile.good ())
	{
		cout << "File opening for writing failed" << endl;
		return 1;
	}
	
	afile.close ();
	
}

int findGrade (int marks)
{
	if (marks < 50) 
	{
    	cout << "Fail";
	}
	else if (marks <= 64) 
	{ 
    	cout << "Pass";
	}
	else if (marks <= 74)
	{
    	cout << "Credit";
	}
	else if (marks <= 84) 
	{
    	cout << "Dist";
	}
	else 
	{
    	cout << "HDist";
	}
}
					
void arrayToOutfile (fstream& afile, char fileName [], UOWSIM p [], int size)
{
	afile.open (fileName, ios::out);
	
	for (int i = 0; i < size; i++)
	{
	
		int result;
		int mark1, mark2, mark3, mark4;
		
	 	result = (mark1 + mark2 + mark3) / 3;  
           
			 
		afile << "Student " << i + 1 << endl;
		
		if (p[i].gen == Male)
		{
			afile << "Name: " << p [i].st.m.name << endl;
			afile << "Sex: Male" << endl;
			afile << "Assignment 1 " << p [i].st.m.mark1 << endl;
			afile << "Assignment 2 " << p [i].st.m.mark2 << endl;
			afile << "Assignment 3 " << p [i].st.m.mark3 << endl;
			afile << "Result " << p [i].result.m.MaleStudent << endl;// Have a problem with the result in outfile.txt
			afile << "Grade " << endl; // Have a problem with the grade display in outfile.txt
		}
		else
		{
			afile << "Name: " << p [i].st.f.name << endl;
			afile << "Sex: Female" << endl;
			afile << "Assignment 1 " << p [i].st.f.mark1 << endl;
			afile << "Assignment 2 " << p [i].st.f.mark2 << endl;
			afile << "Assignment 3 " << p [i].st.f.mark3 << endl;
			afile << "Assignment 4 " << p [i].st.f.mark4 << endl;
			afile << "Result " << p [i].result.f.FemaleStudent << endl; // Have a problem with the result in outfile.txt
			afile << "Grade " << endl; // Have a problem with the grade display in outfile.txt
		}
		
		afile << "------------------------------------------------" << endl;
	}
	
	afile.close ();
}

sample infile.txt info
[output]
F Mary 70 60 89 77
M Andy 70 89 65
M David 89 88 56
M Michael 70 60 89
F Sally 69 75 66 80
[/output]

Outfile.txt should have the following info:
[output]
Student 1
Name: AAAAAAA
Sex: Male
Assignment 1 67
Assignment 2 88
Assignment 3 78
Result: 78 // Add the 3 assignments / 3
Grade: Dist
------------------------------------------------
Student 2
Name: BBBB
Sex: Female
Assignment 1 89
Assignment 2 70
Assignment 3 60
Assignment 4 77
Result: 74 // Add the 4 assignments / 4
Grade: Credit
------------------------------------------------

[/output]

1) line 13 and 21 only reserve one character for name.

2) function FileToArray() does nothing but open and close the input file. What good is that??? I expected to see some code that read the file into the structure members.

1) line 13 and 21 only reserve one character for name.

2) function FileToArray() does nothing but open and close the input file. What good is that??? I expected to see some code that read the file into the structure members.

1) Can elaborate further?

2) Here is my code I can think of:

int FileToArray (fstream& afile, char fileName [], UOWSIM p [])
{
	afile.open (fileName, ios::in);
	
	int n;
	int i = 0;
	
	while (afile >> n)
	{
		switch (n)
		{
			case 0: p [i].gen = Male; 
					afile >> p [i].st.m.name;
					afile >> p [i].st.m.mark1 << endl;
					afile >> p [i].st.m.mark2 << endl;
					afile >> p [i].st.m.mark3 << endl;
					break;
			case 1: p [i].gen = Female;
					afile >> p [i].st.f.name;
					afile >> p [i].st.f.mark1 << endl;
					afile >> p [i].st.f.mark2 << endl;
					afile >> p [i].st.f.mark3 << endl;
					afile >> p [i].st.f.mark4 << endl;
		
		++i;
	}
	   
	afile.close ();
	return i;

1) To get more than one character for name you have to declare an array of characters, like the code below

struct MaleStudent
{
	char name[40];
	int mark1;
	int mark2;
	int mark3;
};

2) that is not what you posted in your original post.

The first character in the file is either the letter F or M, not an integer, so the operation on line 8 will fail and that function will do nothing. You will have to read the first byte as data type char not int then switch on either 'F' or 'M' (lines 12 and 18)

lines 14, 15, 16, etc: you can not use endl on an input file. delete "<< endl" statements on those lines and others like them.

Edited 6 Years Ago by Ancient Dragon: n/a

1) To get more than one character for name you have to declare an array of characters, like the code below

struct MaleStudent
{
	char name[40];
	int mark1;
	int mark2;
	int mark3;
};

2) that is not what you posted in your original post.

The first character in the file is either the letter F or M, not an integer, so the operation on line 8 will fail and that function will do nothing. You will have to read the first byte as data type char not int then switch on either 'F' or 'M' (lines 12 and 18)

lines 14, 15, 16, etc: you can not use endl on an input file. delete "<< endl" statements on those lines and others like them.

Tks for the 1). For 2) I still can't do it. Any more hint? I hav no experience using char in switch.

Didn't your mommy ever tell you that can't never did anything???

Post your most recent attempt.

>>I hav no experience using char in switch.
Well, this will give you a chance to get the experience

char n;
infil >> n;
switch(n)
{
    case 'F':
      //blabla
}

Edited 6 Years Ago by Ancient Dragon: n/a

Didn't your mommy ever tell you that can't never did anything???

Post your most recent attempt.

>>I hav no experience using char in switch.
Well, this will give you a chance to get the experience

char n;
infil >> n;
switch(n)
{
    case 'F':
      //blabla
}

This is my attempt,

int FileToArray (fstream& afile, char fileName [], UOWSIM p [])
{
	afile.open (fileName, ios::in);
	
	
	char n;
	char i = 0;
	
	while (afile >> i)
	{
		switch (n)
		{
			case 'M': p [i].gen = Male; 
					afile >> p [i].st.m.name;
					afile >> p [i].st.m.mark1;
					afile >> p [i].st.m.mark2;
					afile >> p [i].st.m.mark3;
					break;
			case 'F': p [i].gen = Female;
					afile >> p [i].st.f.name;
					afile >> p [i].st.f.mark1;
					afile >> p [i].st.f.mark2;
					afile >> p [i].st.f.mark3;
					afile >> p [i].st.f.mark4;
		
		++i;
	}
	   
	afile.close ();
	return i;
}

Any hints for displaying the Result & grade ?

line 7: variable i should be int, not char because you want to use it as an index into an array. Although char might work, but int is more common to use for that purpose.

line 9: you used the wrong variable -- use varuale n, not i. afile >> n

Edited 6 Years Ago by Ancient Dragon: n/a

line 7: variable i should be int, not char because you want to use it as an index into an array. Although char might work, but int is more common to use for that purpose.

line 9: you used the wrong variable -- use varuale n, not i. afile >> n

Corrected all. But I still can't compile the program. :(

Attached code. Using Quincy 2005

#include <iostream>
#include <fstream>
#include <cstdlib>

using namespace std;

const int MAX = 50;

enum SEX {Male, Female};

struct MaleStudent
{
	char name [50];
	int mark1;
	int mark2;
	int mark3;
};

struct FemaleStudent
{
	char name [50];
	int mark1;
	int mark2;
	int mark3;
	int mark4;
};

union Student
{
	MaleStudent m;
	FemaleStudent f;
};

struct UOWSIM
{
	SEX gen;
	Student st;
	int result;
	char grade [MAX];	 
};


int FileToArray (fstream&, char [], UOWSIM []);
void arrayToOutfile (fstream&, char [], UOWSIM [], int);

int main ()
{
	fstream afile;
	UOWSIM p [MAX];
	
	
	UOWSIM stu;
	
	
	int size = FileToArray (afile, "infile.txt", p);
	arrayToOutfile (afile, "outfile.txt", p, size);
}

int FileToArray (fstream& afile, char fileName [], UOWSIM p [])
{
	afile.open (fileName, ios::in);
	
	
	char n;
	int i = 0;
	
	while (afile >> n)
	{
		switch (n)
		{
			case 'M': p [i].gen = Male; 
					afile >> p [i].st.m.name;
					afile >> p [i].st.m.mark1;
					afile >> p [i].st.m.mark2;
					afile >> p [i].st.m.mark3;
					break;
			case 'F': p [i].gen = Female;
					afile >> p [i].st.f.name;
					afile >> p [i].st.f.mark1;
					afile >> p [i].st.f.mark2;
					afile >> p [i].st.f.mark3;
					afile >> p [i].st.f.mark4;
		
		++i;
	}
	   
	afile.close ();
	return i;
}

int findGrade (int marks)
{
	if (marks < 50) 
	{
    	cout << "Fail";
	}
	else if (marks <= 64) 
	{ 
    	cout << "Pass";
	}
	else if (marks <= 74)
	{
    	cout << "Credit";
	}
	else if (marks <= 84) 
	{
    	cout << "Dist";
	}
	else 
	{
    	cout << "HDist";
	}
}
					
void arrayToOutfile (fstream& afile, char fileName [], UOWSIM p [], int size)
{
	afile.open (fileName, ios::out);
	
	for (int i = 0; i < size; i++)
	{
	
		int result;
		int mark1, mark2, mark3, mark4;
		
	 	result = (mark1 + mark2 + mark3) / 3;  
           
			 
		afile << "Student " << i + 1 << endl;
		
		if (p[i].gen == Male)
		{
			afile << "Name: " << p [i].st.m.name << endl;
			afile << "Sex: Male" << endl;
			afile << "Assignment 1 " << p [i].st.m.mark1 << endl;
			afile << "Assignment 2 " << p [i].st.m.mark2 << endl;
			afile << "Assignment 3 " << p [i].st.m.mark3 << endl;
			//afile << "Result " << p [i].result.m.MaleStudent << endl;// Have a problem with the result in outfile.txt
			afile << "Grade " << endl; // Have a problem with the grade display in outfile.txt
		}
		else
		{
			afile << "Name: " << p [i].st.f.name << endl;
			afile << "Sex: Female" << endl;
			afile << "Assignment 1 " << p [i].st.f.mark1 << endl;
			afile << "Assignment 2 " << p [i].st.f.mark2 << endl;
			afile << "Assignment 3 " << p [i].st.f.mark3 << endl;
			afile << "Assignment 4 " << p [i].st.f.mark4 << endl;
			//afile << "Result " << p [i].result.f.FemaleStudent << endl; // Have a problem with the result in outfile.txt
			afile << "Grade " << endl; // Have a problem with the grade display in outfile.txt
		}
		
		afile << "------------------------------------------------" << endl;
	}
	
	afile.close ();
}

Hi AncientDragon, I am off to bed. Still got work tomorrow morning. Hopefully, I will be able to solve my program with the help of fourmers.

function FileToArray is missing closing } for the switch statement.

Tks! Got that!

Can you provide me some hints to display the result & grade?
As I feel that the function, int findGrade (int marks) is incorrect....
I'll need to use make use of the struct & union right?

Hi Ancient Dragon

Are you online?

I am left with last task which is to find the grade, to display "Pass", "Dist"...etc Hope you can guide me along.

makan007

I am now left with the grade function which I still can't figure out yet. I used string copy & not sure how to call the function. I got compile error at line 141.

#include <iostream>
#include <fstream>
#include <cstdlib>
#include <cstring>
using namespace std;

const int MAX = 30;

enum SEX {Male, Female};

struct MaleStudent
{
	char name [30];
	int mark1;
	int mark2;
	int mark3;
};

struct FemaleStudent
{
	char name [30];
	int mark1;
	int mark2;
	int mark3;
	int mark4;
};

union Student
{
	MaleStudent m;
	FemaleStudent f;
};

struct UOWSIM
{
	SEX gen;
	Student st;
	int result;
	char grade [MAX];	 
};


int FileToArray (fstream&, char [], UOWSIM []);
void arrayToOutfile (fstream&, char [], UOWSIM [], int);
void findGrade (int, char [MAX]);


int main ()
{
	fstream afile;
	UOWSIM p [MAX];	   
	
	int size = FileToArray (afile, "infile.txt", p);
	arrayToOutfile (afile, "outfile.txt", p, size);
}

//Transfer information from infile.txt to array
int FileToArray (fstream& afile, char fileName [], UOWSIM p [])
{
	afile.open (fileName, ios::in);
	
	
	char n;
	int i = 0;
	
	while (afile >> n)
	{
		switch (n)
		{
			case 'M': p [i].gen = Male; 
					afile >> p [i].st.m.name;
					afile >> p [i].st.m.mark1;
					afile >> p [i].st.m.mark2;
					afile >> p [i].st.m.mark3;
					break;
					
			case 'F': p [i].gen = Female;
					afile >> p [i].st.f.name;
					afile >> p [i].st.f.mark1;
					afile >> p [i].st.f.mark2;
					afile >> p [i].st.f.mark3;
					afile >> p [i].st.f.mark4;
		}
		
		++i;
	}
	   
	afile.close ();
	return i;
}


//Find the grade computation
void findGrade (int marks, char grade [MAX])
{
	if (marks < 50) 
	{
    	strcpy (grade, "Fail"); 
	}
	else if (marks <= 64) 
	{ 
    	strcpy (grade, "Pass");
	}
	else if (marks <= 74)
	{
    	strcpy (grade, "Credit");
	}
	else if (marks <= 84) 
	{
    	strcpy (grade, "Dist");
	}
	else 
	{
    	strcpy (grade, "HDist");
	}
}

  	  	  	  	  
void arrayToOutfile (fstream& afile, char fileName [], UOWSIM p [], int size)
{
	afile.open (fileName, ios::out);
	
	for (int i = 0; i < size; i++)
	{ 	   
	
		int marks;
		
		afile << "Student " << i + 1 << endl;
		
		if (p [i].gen == Male)
		{
			
		    p [i].result = (p [i].st.m.mark1 + p [i].st.m.mark2 + p [i].st.m.mark3) / 3; 
			
			afile << "Name: " << p [i].st.m.name << endl;
			afile << "Sex: Male" << endl;
			afile << "Assignment 1 " << p [i].st.m.mark1 << endl;
			afile << "Assignment 2 " << p [i].st.m.mark2 << endl;
			afile << "Assignment 3 " << p [i].st.m.mark3 << endl;
			afile << "Result " << p [i].result << endl;
			afile << "Grade " << findGrade (marks, char grade [MAX]) << endl; //How do I display the findGrade function to outfile.txt
		}
		else
		{
			
			p [i].result = (p [i].st.f.mark1 + p [i].st.f.mark2 + p [i].st.f.mark3 + p [i].st.f.mark4) / 4;
		
			afile << "Name: " << p [i].st.f.name << endl;
			afile << "Sex: Female" << endl;
			afile << "Assignment 1 " << p [i].st.f.mark1 << endl;
			afile << "Assignment 2 " << p [i].st.f.mark2 << endl;
			afile << "Assignment 3 " << p [i].st.f.mark3 << endl;
			afile << "Assignment 4 " << p [i].st.f.mark4 << endl;
			afile << "Result " << p [i].result << endl;
			
		}
		
		afile << "------------------------------------------------" << endl;
	}
	
	afile.close ();
}

Edited 6 Years Ago by makan007: n/a

You get a compiler error because the return type of your function is void (therefore no object to pass to cout)

Since you are passing grade by pointer into the findGrade method, you should call the function first by itself, after that grade will have changed and you can output your array of characters.

findGrade (marks, char grade [MAX]);
afile << "Grade " << grade << endl;

You get a compiler error because the return type of your function is void (therefore no object to pass to cout)

Since you are passing grade by pointer into the findGrade method, you should call the function first by itself, after that grade will have changed and you can output your array of characters.

findGrade (marks, char grade [MAX]);
afile << "Grade " << grade << endl;

1.
findGrade (marks, char grade [MAX]);\
Is this for preprocessor?
2.
afile << "Grade " << grade << endl;
Does not seems to work... I am a little lost.

Replace line 141 with those two lines. No, it has nothing to do with the preprocessor. Your function findGrade is void, it returns nothing. If your function returned an int, you could say afile << findGrade(etc.); there would be a value coming back to send into the output stream.

However, all is not lost because you are sending a char array grade[] into your function which will (effectively) come back out "knowing" the answer(in reality the address is being passed in via a pointer and so any further instances of it are changed since they point to the same item).
This char array is something that afile can handle, so it outputs it to your file. Think of afile almost as a fancy cout that sends the data to your file instead of the screen.

What is not working about it? Does it compile? Is it giving you weird results in the datafile?

Edited 6 Years Ago by jonsca: n/a

jonsca, I have solved it. Tks.

I am wondering if my sample file with underscore in between the names, can it be remove and display in outfile with space instead.

Eg, Lewis_Hamilton to Lewis Hamilton

Just step through your char array:

for (int i = 0;name[i] !='\0';i++)
	if (name[i] == '_')
		name[i] = ' ';

Edited 6 Years Ago by jonsca: n/a

This article has been dead for over six months. Start a new discussion instead.