-1

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;
}
trese
  • 15
  • 5

1 Answers1

0

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;
}
A4F9
  • 76
  • 2
  • 1
    Close, but might get you into this problem [Why does std::getline() skip input after a formatted extraction?](https://stackoverflow.com/questions/21567291/why-does-stdgetline-skip-input-after-a-formatted-extraction) – BoP Feb 02 '22 at 09:10
  • No. I just solved this from my previous problem solving experience on codeforces and Uva online judge. – A4F9 Feb 02 '22 at 09:15
  • So-called "online judge" sites are not good learning resources (they aren't learning or teaching resources *at all* actually), and their examples are typically only examples on bad code and what *not* to do. I can easily break your program by pressing the `Enter` key between the gender and the name (and if I don't then the name could have a leading space that's wouldn't be wanted). – Some programmer dude Feb 02 '22 at 09:25
  • @A4F9 I must use map or set only – trese Feb 02 '22 at 09:44
  • @rese check my code again. It's update. – A4F9 Feb 02 '22 at 10:06
  • @rese Note that the code in this answer have a flaw: When entering the input, try pressing the `Enter` key between the gender and the name, and the program will break. – Some programmer dude Feb 02 '22 at 10:13
  • @Someprogrammerdude what should i do then? also how can I display if the user only want to display the female or male. when i tried it won't let me – trese Feb 02 '22 at 12:08
  • @rese In a case like this I would recommend you read input as separate lines. Write a prompt about getting input for gender, then use `std::getline` to read the input. Parse it any suitable way (skip leading space and check the first non-space character, for example). Then write another prompt for the name, and again use `std::getline` to get the input. Since you use `std::getline` for both inputs, there won't be problems if the input goes out of sync with what's expected. – Some programmer dude Feb 02 '22 at 12:46