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;
}
}
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.
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:
Kindly rate the answer and for any help just drop a comment
Tying to list all the courses the teacher teaches in c++ map but only the first...
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 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...