Hi guys, I need to read this text file to get the name, date, number of subjects, subject name and the result from the text file. I'm Kinda stuck at the subject name and result. This is an example of the output i need:
name: jack
date: 18-4-1988 // anyone knows how to change to this 18-april-1988?
subject result
eng1 55
sci2 54
math3 87
This is my main:
int main()
{
char name[20];
char date[20];
int number;
char subject[20];
int result;
ifstream infile;
infile.open("fin.txt");
infile >> name;
infile >> date;
infile >> number;
cout<< name <<endl;
cout<< date <<endl;
infile.close();
}
Data in the Text file:
Jack 18-4-1988 3 Eng1 Sci2 Math3 55 54 87
Tom 23-10-1996 2 Eng1 Sci2 6 80
Ben 11-8- 1968 2 Eng1 Sci2 34 88
*Note that there are different number of subjects for each student. The numbers in front of (Eng1) denotes the number of subjects taken. I need to read all the data and output them. But if someone shows me how to get jack I can figure out the rest myself. Thanks.
You should have a function that will convert that number to corresponding month. Example, if 4 will return April, 5 will return May and so on. Have a look at the code below for your reference.
void CMyClass::convertToMonth(int iMonth)
{
switch (iMonth)
{
case 1:
mo = "JAN";
break;
case 2:
mo = "FEB";
break;
case 3:
mo = "MAR";
break;
case 4:
mo = "APR";
break;
.
.
.
case 12:
mo = "DEC";
break;
}
}
You can make that function return the value of mo.