Answer to Question #235959 in C++ for noone

Question #235959

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


1
Expert's answer
2021-09-11T07:03:38-0400
#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;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS