I am a student in a c++ class. I need help with an assignment.
"Read from a text file called salaries.txt that contains the following format:
M321232.34
F43234.34
M23432.23
M191929.34
The letter 'M' and 'F' represents the gender and the number represents their salary. Count the number of Males each time you read one in, and add each of the male salaries to a cumulative totalMaleSalary. Do the same for the female. At the end of the loop compute the average female salary and average male salary - display your results and also determine which of the 2 averages is greater."
How would I count the numbers of males and add each of the salaries?
This is what I have:
int main(){
int male=0, female=0;
double salary,totalMaleSalary=0,totalFemaleSalary=0;
char gender;
ifstream fin;
fin.open("salary.txt");
do{
fin>>gender>>salary;
if(gender=='M'){
male=male+1;
totalMaleSalary=salary+totalMaleSalary;
}
if(gender=='F'){
female=female+1;
totalFemaleSalary=salary+totalFemaleSalary;
}
cout<<"Number of Males is "<<male<<endl;
cout<<"Number of Females is "<<female<<endl;
cout<<"Total Male Salary is "<<totalMaleSalary<<endl;
cout<<"Total Female Salary is "<<totalFemaleSalary<<endl;
cout<<"Average Male salary is "<<totalMaleSalary/male<<endl;
cout<<"Average Female salary is "<<totalMaleSalary/female<<endl;
}while(!fin.eof());
fin.close();
return 0;
}