153k views
4 votes
Using for loop,write a program that accepts a series of genders from a class of 10 students and count the number of female and male students in the class(c++ programming)

Output:in the file

Using for loop,write a program that accepts a series of genders from a class of 10 students-example-1
User Flyingfox
by
3.9k points

1 Answer

2 votes

#include <iostream>

#include <algorithm>

#include <vector>

#include <cctype>

int main(int argc, char* argv[]) {

//Creating a vector. It's also dynamic array.

std::vector<char> genders;

//Input section using loop.

for(int i=0; i<10; i++) {

//Point. If the user entered the wrong letter and something else, start from here.

wrong_input:

//Variable that store the gender letter.

char gender;

//Ask the user.

std::cout << "Enter gender of student " << i+1 << " <F/M>: "; std::cin >> gender;

//Check if something went wrong. Basic Exception handling.

if(tolower(gender) == 'f' || tolower(gender) == 'm') {

//Send data into vector.

genders.push_back(tolower(gender));

} else {

std::cout << "Something went wrong. Try again for student " << i+1 << std::endl;

goto wrong_input;

}

}

//Inform the user.

std::cout << "\\The number of female students are " << std::count(genders.begin(), genders.end(), 'f') << "\\The number of male students are " << std::count(genders.begin(), genders.end(), 'm') << std::endl;

return 0;

}

Using for loop,write a program that accepts a series of genders from a class of 10 students-example-1
User Joonho
by
3.7k points