Adding to maps by a function

Hi! I am trying to build a function that will add a new course to a map. My map is supposed to have strings as both key and value. It is going to connect a subjectcode (key) to a subject (value). The task gives me this as a start.

To add a new course to the map I tried writing the code as downunder, but I don't know how to call it correctly in main and check if map now really contains my new (key, value) that I want to add to the map. Are the code even correctly written?

I am pretty new to references and maps, so please bare with me. Thanks a lot for the help.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
//Given code from the task
  class CourseCatalog
{
   map<string, string> subjects;
    
public:
    
    void addCourse(string subjectcode, string subjectname, map<string, string>& subjects);
    void getCourse();
    void removeCourse();
    void addExactCourses();
    
};

void CourseCatalog::addCourse(string subjectcode, string subjectname, map<string, string>& subjects)
{
    subjects.insert({subjectcode, subjectname});
}

void CourseCatalog::addExactCourses()
{
    for (int i = 0; i < 3; ++i)
    {
    cout << "Write a subjectcode you want to add: ";
    string subjectcode;
    cin >> subjectcode;
    cout << "Write the corresponding subjectname ";
    string subjectname;
    cin >> subjectname;

    addCourse(subjectcode, subjectname, subjects);
    }
    
    for (const auto& p : subjects)
    {
        cout << p.first << ":" << p.second << endl;
    }
    
    
}
Last edited on
Hello theredp,

First,
In your CourseCatalog class, the public function, void addCourse(), is a member function, which means it has access to your private subjects map variable. Therefore, you do not need to pass subjects as a parameter by reference to addCourse(). My assumption is that you want to call addCourse() to add a course to the private subjects map variable. Please tell me if I am incorrect in that assumption.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class CourseCatalog
{
   map<string, string> subjects;
    
public:
    
    void addCourse(string subjectcode, string subjectname); // This has access to map<string, string> subjects
    void getCourse();
    void removeCourse();
    void addExactCourses();
    
};

void CourseCatalog::addCourse(string subjectcode, string subjectname)
{
    subjects.insert({subjectcode, subjectname});
}


Second, in order to use the function in the main fuction, you would have to instantiate the CourseCatalog class (Create an object).
CourseCatalog courseCatalog; // Invokes the default constructor of the CourseCatalog

To call addCourse() function, you do this.
courseCatalog.addCourse("subjectCode", "subjectName");

I would read up on classes tutorial for better understanding.
http://www.cplusplus.com/doc/tutorial/classes/

Let the community know if you have any more questions.
Last edited on
Okay, thanks! That worked fine.

But, when I call addExactCourses and have a subjectname that are separated with a space for example:

MET2100 Mathematics and physics

then "and" belongs to the second key and "physics" belongs to the third key and the output becomes messy. Are there some way I can fix this in the for-loop in void addExactCourses()?
theredp,

Before I answer your question about messy output caused by your std::cin, I will say this first.

I personally do not agree with your design decision.

OOP is all about modeling real world objects. I do not believe that it is CourseCatalog's responsibility to output message to the screen to get user responses and then add the course to its collection.

Let's talk about the Course Catalog in the real world.
1. It contains 1 or more courses (Hence, you have subjects map variable.)
2. You can add more courses to the catalog and remove old courses from the catalog (Hence, you have addCourse() and removeCourse() functions).
3. You can get course information from the catalog (Hence, you have getCourse() function)

So, all of these operations are consistent with what the course catalog actually does in the real world.
1
2
3
4
5
6
7
8
class CourseCatalog
{
   map<string, string> subjects;
public:
    void addCourse();
    void getCourse();
    void removeCourse();
};


What does your CourseCatalog have to do with actually getting input from user and add a course to itself? That's not consistent with what the course catalog is all about in the real world.

If anything, your main function or an utility function would have to get the user input and call addCourse() on CourseCatalog to add each course to the catalog.

If we go with the MVC model, View is the entity responsible for displaying information to user and getting input, controller would invoke an operation of Model to change the model's state, and model is the object class that maintains data state.


But anyways, I apologize for a long text that doesn't answer your question. Let's get back to your problem with std::cin.

When std::cin is used for a string variable, it will only extract a word (Up to any whitespace - including spaces, tab, newline) and store it in the string variable. In order to capture everything in a line including characters and spaces, you may want to use std::getline() function.
http://www.cplusplus.com/reference/string/string/getline/

Last edited on
Topic archived. No new replies allowed.