Create a file manually with the file name and contents shown below. The file contains records of student
name, age, sex, and favorite subject unit code.
Record.txt
Wong 18 M ATGE3022
John 19 M ATGE2222
Shirley 20 F ATGE2053
Tan 20 F ATGE1234
Lee 21 M ATGE2053
Chin 18 F ATGE2053
Chan 21 M ATGE2053
Write a program that reads the contents of the file and display the following result on the screen:
i) Total numbers of female whose favorite subject unit code is ATGE2053
ii) Total numbers of male whose favorite subject unit code is ATGE2053
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
const string FEMALE = "F";
const string MALE = "M";
const string FAVORITE_SUBJECT = "ATGE2053";
ifstream input("Record.txt");
string name;
int age = 0;
string sex;
string subject;
int countFemale = 0;
int countMale = 0;
while(input >> name) {
input >> age >> sex >> subject;
if (sex == FEMALE && subject == FAVORITE_SUBJECT)
countFemale++;
if (sex == MALE && subject == FAVORITE_SUBJECT)
countMale++;
}
cout << "Total female: " << countFemale << endl;
cout << "Total male: " << countMale << endl;
return 0;
}
Comments
Leave a comment