Question

Tying to list all the courses the teacher teaches in c++ map but only the first...

Tying to list all the courses the teacher teaches in c++ map but only the first one is being shown, not the rest of them. please help.

------------------------------------------------------------------------------------------------------------------------------

#include<iostream>

#include<map>

using namespace std;

int main(){


                map <string,string> instructors;

                //mapping course numbers and instructor names

                instructors.insert(pair<string,string>("Haynes","CS101"));
              
                instructors.insert(pair<string,string>("Haynes","CS102"));
              
                instructors.insert(pair<string,string>("Haynes","CS103"));


                instructors.insert(pair<string,string>("Alvarado","CS201"));
              
                instructors.insert(pair<string,string>("Alvarado","CS202"));
              
                instructors.insert(pair<string,string>("Alvarado","CS203"));


              
                char choice='y';

                //looping until user wishes to quit

                while(choice=='y' || choice=='Y'){

                                cout<<"Enter a instructor name: ";

                                string instructor;

                                cin>>instructor;

                                //searching in maps for the required course number, this will return

                                //an iterator

                                map<string, string>::iterator it1=instructors.find(instructor);

                                if(it1!=instructors.end()){

                                                cout<<"Courses: "<<it1->second<<endl;

                                }

             

                              

                                //prompting again

                                cout<<"\nDo you want to search again? (y/n): ";

                                cin>>choice;

                }

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

The code you have written will serve the purpose as keys are unique in a map. You are trying to insert multiple values having the same keys. The insert function fails to insert the key-value pair if the key to be inserted is already present in the map. Thus, you are only getting the first value only.

instructors.insert(pair<string, string>(Haynes, CS101)); // Insertion successful instructors.insert(pair<string, string>(

To support multiple key-value pairs with the same key, you could either use STL's multimap or create an STL map with pair<string, vector<string>>.

An implementation using STL's multimap:

#include <iostream>
#include <map>

using namespace std;

int main() {
    multimap<string, string> instructors;

    // Mapping course numbers and instructor names
    instructors.insert(pair<string, string>("Haynes", "CS101"));
    instructors.insert(pair<string, string>("Haynes", "CS102"));
    instructors.insert(pair<string, string>("Haynes", "CS103"));
    instructors.insert(pair<string, string>("Alvarado", "CS201"));
    instructors.insert(pair<string, string>("Alvarado", "CS202"));
    instructors.insert(pair<string, string>("Alvarado", "CS203"));

    char choice = 'y';
    // Looping until user wishes to quit
    while (choice == 'y' || choice == 'Y') {
        cout << "Enter a instructor name: ";
        string instructor;
        cin >> instructor;

        // Searching for courses of a particular instructor
        cout << "Courses: ";
        for (auto itr = instructors.begin(); itr != instructors.end(); itr++) {
            if (itr->first == instructor)
                cout << itr->second << " ";
        }

        // Prompting again
        cout << "\nDo you want to search again? (y/n): ";
        cin >> choice;
    }
    return 0;
}

Sample output:

shubh@ACER-NITRO MINGW64 ~/OneDrive/Documents/Chegging $ ./main Enter a instructor name: Haynes Courses: CS101 CS102 CS103 Do

Kindly rate the answer and for any help just drop a comment

Add a comment
Know the answer?
Add Answer to:
Tying to list all the courses the teacher teaches in c++ map but only the first...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Python Language Only!!!! Python Language Only!!!! Python Language Only!!!! Python 3 is used. Write a program...

    Python Language Only!!!! Python Language Only!!!! Python Language Only!!!! Python 3 is used. Write a program that creates a dictionary named rooms containing course numbers and the room numbers of the rooms where the courses meet. The dictionary should have the following key-value pairs: Course Number (Key) Room Number (Value) CS101 3004 CS102 4501 CS103 6755 NT110 1244 CM241 1411 The program should also create a dictionary named instructors containing course numbers and the names of the instructors that teach...

  • Python Language Only!!!! Python Language Only!!!! Python Language Only!!!! Python 3 is used. Write a program...

    Python Language Only!!!! Python Language Only!!!! Python Language Only!!!! Python 3 is used. Write a program that creates a dictionary named rooms containing course numbers and the room numbers of the rooms where the courses meet. The dictionary should have the following key-value pairs: Course Number (Key) Room Number (Value) CS101 3004 CS102 4501 CS103 6755 NT110 1244 CM241 1411 The program should also create a dictionary named instructors containing course numbers and the names of the instructors that teach...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT