my current code:
I will fix the identing later.
Code:
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
int num_females = 0;
int num_males = 0;
int sum_males;
int sum_females;
char name[25];
char gender;
char code[5];
int score;
int count = 0;
char reply;
ifstream infile;
string myfile;
//char process[200];
ifstream inputFile;
cout << "Enter file name with the extension(Example: scores.txt): " << endl;
cin >> myfile;
inputFile.open (myfile.c_str(), ios::in);
if(!inputFile)
{
cerr << "Can't open input file " << myfile << endl;
cin >> reply;
exit(1);
}
while(inputFile)
{
inputFile >> name;
cout << name;
inputFile >> gender;
if(gender == 'F')
{
num_females++;
}
else
{
num_males++;
}
cout << setw(5) << gender;
inputFile >> code;
cout << setw(3) << code;
inputFile >> score;
cout << setw(4) << score << endl;
//inputFile.getline(process,200);
}
cout << "number of males = " << num_males;
cin >> reply;
return 0;
}
output:
Code:
Enter file name with the extension(Example: scores.txt):
scores.txt
Bailey M CC 68
Harrison F CC 71
Grant M UN 75
Peterson F UN 69
Hsu M UN 79
Bowles M CC 75
Anderson F UN 64
Nguyen F CC 68
Sharp F CC 75
Jones M UN 75
McMillan F UN 80
Gabriel F UN 62
F 62
number of males = 5
that last F 62 is part of Gabriel and should not be there..
here is a copy of scores.txt
Code:
Bailey M CC 68
Harrison F CC 71
Grant M UN 75
Peterson F UN 69
Hsu M UN 79
Bowles M CC 75
Anderson F UN 64
Nguyen F CC 68
Sharp F CC 75
Jones M UN 75
McMillan F UN 80
Gabriel F UN 62