Modify a value in a set after being pushed?

So here's my code. It's mostly finished(I think) besides the end. So my problem is that I have to rearrange the term(Spring, 2019) in order of the season and year. I modify the terms so that Spring 2019 looks like 20191, Summer 2019 to be 20192 etc. After I push_back my value into the set I have no clue how to modify it again so that it'll show the original string. Everything is working like I want besides that. And sorry for including the inputFile part of it. I'm hoping no one needs to actually run the program to help.... And I honestly don't know how to re-write it to get the same results.

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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#include <iostream>
#include <string>
#include <fstream>
#include <cstring>
#include <cmath>
#include <map>
#include <set>

using namespace std;

int hashCode(const string&);

int main()
{
 
    map<string, set<string>> allClasses;

    //for parsing the inputfile
    char* token;
    char buf[1000];
    const char* const tab = "\t";

    //open the inout file
    ifstream fin;
    fin.open("course-schedule.txt");
    if(!fin.good()) cout << "I/O error";

    //read the input file
    while (fin.good())
    {
        //read the limne
        string line;
        getline(fin, line);
        strcpy(buf, line.c_str());

        if(buf[0] == 0) continue;  //skip blank lines

        //parse the line
        string term(token = strtok(buf, tab));
        const string section((token = strtok(0, tab))? token : " ");
        const string course((token = strtok(0, tab))? token : " ");
        const string instructor((token = strtok(0, tab))? token : " ");
        const string whenWhere((token = strtok(0, tab))? token : " ");
        if(course.find('-') == string::npos)continue; //invalid line: no dash in course name
        const string subjectCode(course.begin(), course.begin() + course.find('-'));
        const string classCode = term + section;

        const string termOrder(term.begin(), term.begin() + term.find(' '));
        if(termOrder == "Spring")
        {
            term.push_back('1');
            term.erase(0,7);
        }
        else if(termOrder == "Summer")
        {
            term.push_back('2');
            term.erase(0, 7);
        }
        else if(termOrder == "Fall")
        {
            term.push_back('3');
            term.erase(0, 5);
        }
        allClasses[course].insert(term);

    }
    ///////////////////////
    ////////DISPLAY////////

    string userInput;

    while(true)
    {
        cout << "Enter a course name (like, COMSC-210) to search for the first and last semester the course was offered [X/x to exit]: "<< endl;
        cin >> userInput;
        if(userInput == "x" || userInput == "X")
            break;
        set<string>::iterator itr;
        set<string>::reverse_iterator ritr;
        for(itr = allClasses[userInput].begin(); itr != allClasses[userInput].end(); itr++)
            for(ritr = allClasses[userInput].rbegin(); ritr != allClasses[userInput].rend(); ritr++)
            {
                if(itr == allClasses[userInput].begin() && ritr == allClasses[userInput].rbegin())
                {
                    cout << userInput << " was first offered in " << *itr << endl;
                    cout << userInput << " was last offered in " << *ritr << endl;
                }
            }
    }
    cout << "Program Finished!";

    return 0;
}
Last edited on
You can't modify a value in a std::set. You can remove old and insert new.

However, you don't want to change the sets, do you? You want to show values in nice format.

You want:
1
2
cout << userInput << " was first offered in " << human(*itr) << endl;
cout << userInput << " was last offered in " << human(*ritr) << endl;

where function human() takes "20307" and returns something like: "After 2030".
It's almost works perfectly. Here's the function I wrote up. Only thing I changed in main is the conversion of the string numbers before I push_back the values. It works for some of them, but not all. I'm thinking it has something to do with my if/else if statements? But the math makes sense to me.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
string outPut(int value)
{
    int x = 0;
    string outPut = " ";
    if((value%10) == 1)
    {
        x = value/10;
        outPut = "Spring " + to_string(x);
    }
    else if((value&10) == 2)
    {
        x = value/10;
        outPut = "Summer " + to_string(x);
    }
    else if((value%10) == 3)
    {
        x = value/10;
        outPut = "Fall " + to_string(x);
    }

    return outPut;
}
I tested it as an int function and returned just x, and some of its values ended up being 0
WOWWWWWW, There was & where a % is supposed to be. Just spent an hour to find that. Thanks for the help, though! It works
Topic archived. No new replies allowed.