Home > Enterprise >  C program where user will input names and decide which gender (M/F) to show
C program where user will input names and decide which gender (M/F) to show

Time:02-02

Using c I need to make a program using map in c where the gender (M or F) is the key of 6 input names, and then it will ask the user to choose what gender to display (Male or Female). I am stuck with this, I do not know what to do next.

#include <iostream>
#include <set>
using namespace std;

int main(){
    set<pair<string,string>> s;
    set<pair<string,string>>::iterator itr;
    string name;
    string gender;

    cout<<"Enter 6 names with genders : "<<endl;
    for(int a=1; a<=6; a  ){
        cin>>gender;
        getline(cin, name);
        s.insert({name,gender});
    }
    cout<<endl;

    cout<<"Enter the gender(M/F): ";
    cin>>gender;
    for (itr = s.begin(); itr != s.end(); itr  ) {
        if(itr->second==gender)
        cout<<" "<<itr->first<<" "<<itr->second<<endl;
    }
    cout << endl;

    return 0;
}

CodePudding user response:

You can try "set" instead of map.

#include <iostream>
#include <set>

using namespace std;

int main(){
set<pair<char,string>> s;

cout<<"Enter 6 gender and name : "<<endl;
char gender; string name;
for(int a=1; a<=6; a  ){
    cin >> gender; getline(cin, name);
    s.insert({gender,name});
}

set<pair<char,string>>::iterator itr;

for(itr = s.begin(); itr!=s.end(); itr  ){
    cout << itr->first << " " << itr->second << endl;
}cout << endl;

return 0;
}
  •  Tags:  
  • Related