Answer to Question #208889 in C++ for she

Question #208889

QUESTION 1


Modify the given program so that the program is able to calculate and display the number of male and female employees. Use a function name displayGender() to display counter number of gender.


  #include<iostream>

using namespace std;

int main() {

  char name[30];

  char gender;

for(int counter = 0; counter < 5; counter++) {

cout<<"Name of the employee: ";

cin>>ws;

cin.getline(name, 30);

cout<<"Gender of the employee (M / F): "; cin>>gender;

}

return 0;

}


1
Expert's answer
2021-06-20T18:15:29-0400
#include<iostream>

using namespace std;

void displayGender(char *g){
    int males = 0;
    for(int c = 0; c < 5; c++) {
        if(g[c]=='M'){
            males++;
        }
    }
    cout<<"Male: "<<males<<endl;
    cout<<"Female:"<< 5 - males<<endl;
}


int main() {
  char name[30];
  char gender;
//   Array to store values of gender entered by user
  char all_gender[5];
for(int counter = 0; counter < 5; counter++) {
    cout<<"Name of the employee: ";
  
    cin>>ws;
    
    cin.getline(name, 30);
    
    cout<<"Gender of the employee (M / F): "; cin>>gender;
    
    // Assign input gender value to array
    all_gender[counter] = gender;
   
}
displayGender(all_gender);

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