Thread: Assigning the content of one map to another map and doing the same with vector

  1. #1
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827

    Assigning the content of one map to another map and doing the same with vector

    Ok, this is getting a little annoying...
    After spending 2 hours debugging, I have successfully inserted all enumerators (both string names and their values) into a map<string, int> in my ConvertEnumToStrings program. The output of my program proves this. However, now what's happening is, when I get the map, the resulting map that I use in the int main() function has all mapped values set to 0 (even though the original member map of the CenumOperations class stored several different values). The size() is correct, but all values are set to 0. The same is true with the vector of enumerator values. The key value side of the map (i.e. "string") has the right values, but the mapped values are all 0.

    Why would that be?

    I'm thinking it has something to do with the assignment operator '=' of the std::map class failing to work right. Basically, how this works is:

    1. A file containing an enum is passed to the program.
    2. The complete enum (along with various elements, such as a vector of its enumerators string names, and a vector of its enumerators int values, as well as the map already mentioned) is stored in memory (internally inside of members of the CenumOperations class).
    3. Now in int main(), I create an object of the class, call the CenumOperations::getEnumerationsMap() to retrieve the enumerations map. getEnumerationsMap returns a const map<string, int> reference to a copy of the original map (inside this function, the copy is assigned the content of the original, then the original map is cleared, and a reference to the copy is returned), i.e:

    Code:
    const map <string, int>& CenumOperations::getEnumeratorsMap() {
    
        enumeratorsMapCopy_m_si = enumeratorsMap_m_si; //get a copy of the enumerators map
        enumeratorsMap_m_si.clear(); //then clear the original
        return enumeratorsMapCopy_m_si; //and return a reference to the copy
    
    }
    Note that the enumerators map copy is also a member of the CenumOperations, same as the original map. Similiarly, the getEnumeratorValues() member function looks like this:

    Code:
    const vector <int>& CenumOperations::getEnumeratorValues() {
    
        enumeratorValuesCopy_v_i = enumeratorValues_v_i; //get a copy of the values of the enumerators
        enumeratorValues_v_i.clear(); //then clear the original
        return enumeratorValuesCopy_v_i; //and return a reference to the copy
    
    }
    Now, in int main(), what happens is, I create another map (and vector). I assign to the map the return value of getEnumeratorsMap(), and I assign to the vector the return value of getEnumeratorValues():

    Code:
    int main() {
      //do stuff...
      map <string, int> enumeratorsMap_m_si; //for storing a map of the enumerators
      vector <int> valuesOfEnumerators_v_i; //for storing the values of enumerators
      //do more stuff...
      enumeratorsMap_m_si = convertEnumToStringsObject.getEnumeratorsMap(); 
      //we now (actually, should...) have a map of both enumerator names and numerical values...BUT WE DON'T!! :D haha
      valuesOfEnumerators_v_i = convertEnumToStringsObject.getEnumeratorValues(); 
      //we now have a int vector of the values of the enumerators...or not :(
    }
    Note that the "convertEnumToStringsObject" is an object of a class that inherits from both the "CenumOperations" and "CfileOperations" classes, and has no members of its own.

    So can anyone tell me why my program outputs this:
    gorilla@ubuntu:~/Documents/Programming/Completed_Programs/ConvertEnumToStrings/bin/Debug$ ./ConvertEnumToStrings "/home/gorilla/Documents/test.cpp" "results"
    Thank you for running ConvertEnumToStrings.
    This program will work with either C or C++ enums.
    Before we begin converting enums to strings though,
    please take note of the following points:

    1. I did not provide support for invalid enums.
    2. I also did not provide support for enums which
    use variable names in any enumerator assignments.
    You must use only const value numbers when you explicitly
    assign an enumerator a value. The reason for this is, its
    not possible to figure out the internal value of a variable.
    3. The only exception to the last part of 2. is when
    you specified the type of enum, and its a different
    numerical type, such as a char. In this case, the program
    will output the numerical representation of whatever char
    has been assigned to a given enumerator. You are then free
    to convert it back to a char if you want.
    4. There are no other restrictions. The program should handle
    all other kinds of enums.

    Please press Enter to begin.


    Thank you. We will now get the converting process underway...

    currentEnumName_s is: DAY
    We found an enumerator.
    currentEnumeratorName_s is: sunday
    enumeratorsCounter_ui is: 1
    enumeratorsMap_m_si.find("sunday")->second is: 0
    We found an enumerator.
    We found an enumerator with an assigned value.
    currentEnumeratorValue_i is: 0
    currentEnumeratorName_s is: monday
    currentEnumeratorHasAssignedValue_b is true.
    enumeratorsMap_m_si.find("monday")->second is: 0
    We found an enumerator.
    currentEnumeratorName_s is: tuesday
    The current enumerator is not the first and does not have an assigned value.
    currentEnumeratorValue_i before incrementing: 0
    currentEnumeratorValue_i after incrementing: 1
    enumeratorsCounter_ui is: 3
    enumeratorsMap_m_si.find("tuesday")->second is: 1
    We found an enumerator.
    currentEnumeratorName_s is: wednesday
    The current enumerator is not the first and does not have an assigned value.
    currentEnumeratorValue_i before incrementing: 1
    currentEnumeratorValue_i after incrementing: 2
    enumeratorsCounter_ui is: 4
    enumeratorsMap_m_si.find("wednesday")->second is: 2
    We found an enumerator.
    currentEnumeratorName_s is: thursday
    The current enumerator is not the first and does not have an assigned value.
    currentEnumeratorValue_i before incrementing: 2
    currentEnumeratorValue_i after incrementing: 3
    enumeratorsCounter_ui is: 5
    enumeratorsMap_m_si.find("thursday")->second is: 3
    We found an enumerator.
    currentEnumeratorName_s is: friday
    The current enumerator is not the first and does not have an assigned value.
    currentEnumeratorValue_i before incrementing: 3
    currentEnumeratorValue_i after incrementing: 4
    enumeratorsCounter_ui is: 6
    enumeratorsMap_m_si.find("friday")->second is: 4
    We found an enumerator.
    currentEnumeratorName_s is: saturday
    The current enumerator is not the first and does not have an assigned value.
    currentEnumeratorValue_i before incrementing: 4
    currentEnumeratorValue_i after incrementing: 5
    enumeratorsCounter_ui is: 7
    enumeratorsMap_m_si.find("saturday")->second is: 5
    namesOfEnumerators_v_s.size() is: 7
    valuesOfEnumerators_v_i.size() is: 7
    enumeratorsMap_m_si.size() is: 7
    numOfEnumerators_ui is: 7
    We found a single-line enum.
    currentEnumName_s is: AREYOUAROCK
    We found an enumerator with an assigned value.
    currentEnumeratorValue_i is: 1
    currentEnumeratorName_s is: yes
    currentEnumeratorHasAssignedValue_b is true.
    enumeratorsMap_m_si.find("yes")->second is: 1
    We're at the last enumerator of the single-line enum.
    We found an enumerator that has an assigned value.
    currentEnumeratorValue_i is: 0
    currentEnumeratorHasAssignedValue_b is true.
    enumeratorsMap_m_si.find("no")->second is: 0
    namesOfEnumerators_v_s.size() is: 2
    valuesOfEnumerators_v_i.size() is: 2
    enumeratorsMap_m_si.size() is: 2
    numOfEnumerators_ui is: 0
    currentEnumName_s is: Work
    We found an enumerator.
    We found an enumerator with an assigned value.
    currentEnumeratorValue_i is: 1
    currentEnumeratorName_s is: hard
    currentEnumeratorHasAssignedValue_b is true.
    enumeratorsMap_m_si.find("hard")->second is: 1
    We're at the last enumerator of the enum.
    The last enumerator of the enum has an assigned value.
    currentEnumeratorValue_s is: 2
    currentEnumeratorValue_i is: 2
    currentEnumeratorName_s is: lazy
    currentEnumeratorHasAssignedValue_b is true.
    enumeratorsMap_m_si.find("lazy")->second is: 2
    namesOfEnumerators_v_s.size() is: 2
    valuesOfEnumerators_v_i.size() is: 2
    enumeratorsMap_m_si.size() is: 2
    numOfEnumerators_ui is: 2
    The current line with 'enum' has a comment.
    currentEnumName_s is: weirdenum
    We found an enumerator.
    currentEnumeratorName_s is: weirdenum1
    enumeratorsCounter_ui is: 1
    enumeratorsMap_m_si.find("weirdenum1")->second is: 0
    We found an enumerator.
    We found an enumerator with an assigned value.
    currentEnumeratorValue_i is: 2
    currentEnumeratorName_s is: weirdenum2
    currentEnumeratorHasAssignedValue_b is true.
    enumeratorsMap_m_si.find("weirdenum2")->second is: 2
    We're at the last enumerator of the enum.
    The last enumerator of the enum has an assigned value.
    currentEnumeratorValue_s is: 0
    currentEnumeratorValue_i is: 0
    currentEnumeratorName_s is: weirdenum3
    currentEnumeratorHasAssignedValue_b is true.
    enumeratorsMap_m_si.find("weirdenum3")->second is: 0
    namesOfEnumerators_v_s.size() is: 3
    valuesOfEnumerators_v_i.size() is: 3
    enumeratorsMap_m_si.size() is: 3
    numOfEnumerators_ui is: 3
    4 enums were found.
    They were successfully written to file.
    Generated file can be found at: /home/gorilla/Documents/results.h

    If you chose to write to a header file, then you
    can include the header in your source file, and
    call up a function to retrieve the names of all
    the enumerations or a specific enumeration you choose.
    Otherwise if you wrote to a source file, you will need
    to put the implemention in the source file you wrote to.
    The generated file provides the following functions inside
    a class with the name of 'CnameofenumToStrings':

    const vector<string>& getEnumeratorNames();
    const vector<int>& getEnumeratorValues();
    const string& getNameOfEnum();
    const int& getNumOfEnumerators();
    const map<string, int>& getEnumeratorsMap();
    const string& getEnumeratorValue(nameofenum enumerator);

    where 'nameofenum' is replaced with the actual name of the
    enum you want to retrieve something from.

    The function names are pretty self-explanatory:

    getEnumeratorNames() obviously returns a reference
    to a vector of the string names of the enumerators in
    your enum.
    getEnumeratorValues() returns a reference to a vector
    of the int values of the enumerators in your enum.
    getNameOfEnum() is provided in case you need to retrieve
    the string name of the enum for some reason. It returns
    a reference to a string.
    getNumOfEnumerators() returns a reference to an int containing
    the total number of enumerators found in the enum, in case
    you need that for some reason.
    getEnumeratorsMap() returns a reference to a map<string, int>
    containing the string names and int values of the enumerators in an enum.
    The int values can be either explicit or implicit, and the type of the enum does
    not even have to be int. It can be some other intergal type, but
    ConvertEnumToStrings will always return it in int form.
    getEnumeratorValue(nameofenum enumerator) returns a
    reference to a int containing the value of the enumerator
    you passed in the parameter.

    Congratulations, and feel free to use this program again anytime.


    Press Enter to end this program.
    Haha, I figured everyone would love the really long output...
    Note that most of the output is debug output statements (and they demonstrate that I was able to get the actual enumerator values and insert them into the map, since calling find() on them after inserting returns the right values). I'll remove those before I'm done with the program.
    Last edited by Programmer_P; 07-11-2010 at 09:08 PM.
    I'm an alien from another world. Planet Earth is only my vacation home, and I'm not liking it.

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I see you're still slaving away with this.
    Unfortunately, I cannot exactly tell you what is wrong. The only thing I can not is the extremely unnecessary copies you're making before returning the map and vector.

    Also, debug statements are pointless unless you're shopping your software to someone else. Use your debugger.
    Better yet, post the smallest possible compilable example that demonstrates the problem.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,401
    Quote Originally Posted by Programmer_P View Post
    I'm thinking it has something to do with the assignment operator '=' of the std::map class failing to work right.
    Doubt it. The assignment operator of a standard container has been used far more times and by far more programmers than any code you've written. Look for the bug elsewhere.

    The code that you've shown, while it doesn't appear to have a bug in it, does look suspicious. If I call getEnumeratorsMap() more than once, I'm going to get an empty map the second time. That's not exactly intuitive for a 'get' operation.
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  4. #4
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Quote Originally Posted by Elysia View Post
    I see you're still slaving away with this.
    lol...yeah, the map part of it turned out to be more difficult than expected.
    (I told you I hated maps...)
    But at least its almost working now.
    Unfortunately, I cannot exactly tell you what is wrong. The only thing I can not is the extremely unnecessary copies you're making before returning the map and vector.
    Well, it may seem like unneccessary copies, but I do it that way since the function that operates on enums is supposed to return after only one enum is found. So there will be a call on operatedOnEnum() for each enum found in the file. Now for multiple enums in the file, if I didn't use copies, what would happen is there would already be content in both the vector and the map by at least the second call of the function, so I would only be adding elements to a non-empty map and vector.
    But, come to think of it, I guess I could always check to see if they're empty at the start of the function, and clear them if they're not...
    Also, debug statements are pointless unless you're shopping your software to someone else. Use your debugger.
    The debugger only helps with runtime errors (not logical errors), I think. I needed to find out what was actually happening. That is why I wrote the debug statements.
    Better yet, post the smallest possible compilable example that demonstrates the problem.
    Code:
    for (unsigned int i = 0; i < namesOfEnumerators_v_s.size(); i++) {
          output_s += "    enumeratorsMap.insert(make_pair(\"";
          output_s += namesOfEnumerators_v_s.at(i);
          output_s += "\", ";
          currentEnumeratorValue_i = enumeratorsMap_m_si.find(namesOfEnumerators_v_s.at(i))->second;
          currentEnumeratorValue_ss<< currentEnumeratorValue_i;
          currentEnumeratorValue_ss >> currentEnumeratorValue_s;
          output_s += currentEnumeratorValue_s;
          output_s += "));\n";
    }
    I'm thinking its adding 0s because the find() function is not finding what's being searched for, apparently because the map inside the int main is not the same as the original map. Anyway, I got rid of the copies, and am about to add a check to see if the find() funtion returned map::end() or not.
    I'm an alien from another world. Planet Earth is only my vacation home, and I'm not liking it.

  5. #5
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Quote Originally Posted by pianorain View Post
    Doubt it. The assignment operator of a standard container has been used far more times and by far more programmers than any code you've written. Look for the bug elsewhere.

    The code that you've shown, while it doesn't appear to have a bug in it, does look suspicious. If I call getEnumeratorsMap() more than once, I'm going to get an empty map the second time. That's not exactly intuitive for a 'get' operation.
    No, you wouldn't. Because both the function and both the maps being used are members of a class. If you call the function more than once from the same object, the content of the map will be the same so long as another call of operatedOnEnum() has not been made. And I call the function once for every call of operatedOnEnum() (if the function returned true that is). So the content of the map being retrieved should reflect the content of the original internal map of the class which contains the enum stuff. But it doesn't...
    Anyway, like already mentioned though, I got rid of the copies and replaced them with a couple of checks inside operatedOnEnum() to see if the vector and map are not empty. If so, then I clear them both before the enum operations. Then I just return a reference to the original map from getEnumeratorsMap().
    I'm an alien from another world. Planet Earth is only my vacation home, and I'm not liking it.

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    This is very pretty, but I still can't compile it. That's why I said smallest possible compilable example.
    Cut down on parts of your source until the error disappears. Then post that small version. It doesn't have to do anything, it just has to reproduce that problem.
    And of course the debugger helps you find logical errors. It does have step-by-step functionality and all that.
    Boy, I managed to make a huge mistake (logic error; error in thinking) in my AI implementation. The wonderful debugger helped me find out what was wrong and it turned out it was an error in my thinking. That's pretty nice, isn't it?

    What pianorain is trying to say is this:
    Code:
    CenumOperations myobj;
    myobj.getEnumeratorValues(); // OK, we get a non-empty vector.
    myobj.getEnumeratorValues(); // Problem! Vector returned is empty!
    And you know there's a multi-quote button, yes?
    Last edited by Elysia; 07-12-2010 at 07:32 AM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Quote Originally Posted by Elysia View Post
    What pianorain is trying to say is this:
    Code:
    CenumOperations myobj;
    myobj.getEnumeratorValues(); // OK, we get a non-empty vector.
    myobj.getEnumeratorValues(); // Problem! Vector returned is empty!
    And what I'm trying to say is this:
    Code:
    CenumOperations myobj;
    myobj.getEnumeratorValues(); //Ok, we get a non-empty vector.
    myobject.getEnumeratorValues(); //no problem! the vector returned is NOT empty!
    And you know there's a multi-quote button, yes?
    Yeah, I know, but I sometimes forget about that button.
    Last edited by Programmer_P; 07-12-2010 at 07:40 AM.
    I'm an alien from another world. Planet Earth is only my vacation home, and I'm not liking it.

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Those are two different objects. Call the function twice on the same object and you get a problem. Which sounds like a design flaw.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Quote Originally Posted by Elysia View Post
    Those are two different objects. Call the function twice on the same object and you get a problem. Which sounds like a design flaw.
    Oops...that was a typo.
    Anyway, obviously I know that the function wouldn't work if called twice on the same object with the second call before another call of operatedOnEnum(), but I was thinking along the lines of:

    1. Call operatedOnEnum() to do operations on an enum.
    2. Call getEnumeratorsMap() to get a map of the enum.
    3. Call operatedOnEnum() to do operations on another enum (if there is one).
    4. Call getEnumeratorsMap() to get a map of the new enum.

    in which case the function would work perfectly fine like it was before (or more accurately, like it was supposed to before), since before the second function call of getEnumeratosMap(), the original enumeratorsMap would have the enumerators of the new enum inserted into it, then the map copy would be assigned the content of the new enumeratorsMap, hence why it would work. Anyhoo, I stopped using copies, and am now returning the original map, since I call map::clear() on the map before I begin enum operations in operatedOnEnum().
    I'm an alien from another world. Planet Earth is only my vacation home, and I'm not liking it.

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    This sounds like unnecessary work. You generate whatever data you may need, preferably within the constructor and then utilize it. You never destroy what you've created. If I need to access the data multiple times, I should be able to call the function get it.
    And if I need to start over, I would create a new object or tell it that I don't need the old data any longer.
    A get function should never modify the object state.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #11
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,401
    Since the object in question is meant to be used internally rather than exposed for programmers to play with, it's only an issue if the OP cares. The posted code just didn't pass the smell test.

    We still haven't seen any code that might help us help you.
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  12. #12
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Quote Originally Posted by Elysia View Post
    This sounds like unnecessary work. You generate whatever data you may need, preferably within the constructor and then utilize it.
    I've considered doing that before (i.e. calling operatedOnEnum() inside the constructor), but decided against it. The reason is, since there might be more than one enum in the file, it makes sense to call the operatedOnEnum() function until the end of the file has been reached, and after each call, get the relevant stuff from the enum operations class. If I called operatedOnEnum() from inside the constructor, and it failed to find (and/or operate on) an enum, how would I let the outside world know about it? Constructors can't return values. I mean, I guess I could throw an exception or something, but the idea is to read the entire file, and store each enum it finds in data members of the class, and then retrieve it using public functions of the class.
    You never destroy what you've created.
    Don't be so sure about that...what if you didn't like what you created? Then you might destroy it. haha. Obviously I'm not talking about programming in this instance.
    If I need to access the data multiple times, I should be able to call the function get it.
    Well, now you can, since the getEnumeratorsMap() function and other similar functions have been improved.
    And if I need to start over, I would create a new object or tell it that I don't need the old data any longer.
    Well, nothing says you can't create a new object of the enum class and start over.
    A get function should never modify the object state.
    You're right, and its fixed now. What you're talking about here has already been changed. Its ancient history.
    I'm an alien from another world. Planet Earth is only my vacation home, and I'm not liking it.

  13. #13
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Quote Originally Posted by pianorain View Post
    Since the object in question is meant to be used internally rather than exposed for programmers to play with, it's only an issue if the OP cares.
    Exactly. That's why I didn't see a problem with it before. Because the original enumerators map was never meant to be modified outside the class anyway. So returning a copy wasn't a problem. But anyway, I changed it for brevity.
    The posted code just didn't pass the smell test.
    Don't you mean it didn't pass the sight test?
    AFAIK, your nose can't smell code.
    We still haven't seen any code that might help us help you.
    You're right, and that's because I had to go to work, and didn't have time to work on it.

    I'll try to post something in a minute if its still outputting 0s.
    I'm an alien from another world. Planet Earth is only my vacation home, and I'm not liking it.

  14. #14
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Ok, I pinpointed the problem:

    Code:
    for (unsigned int i = 0; i < namesOfEnumerators_v_s.size(); i++) {
          output_s += "    enumeratorsMap.insert(make_pair(\"";
          output_s += namesOfEnumerators_v_s.at(i);
          output_s += "\", ";
          map<string, int>::iterator it = enumeratorsMap_m_si.find(namesOfEnumerators_v_s.at(i));
          if (it == enumeratorsMap_m_si.end()) {
              cout<< "\"" << namesOfEnumerators_v_s.at(i) << "\" was not found in enumeratorsMap_m_si." <<endl;
          }
    
          else {
              currentEnumeratorValue_i = it->second;
              cout<< "In int main(), currentEnumeratorValue_i is: " << currentEnumeratorValue_i <<endl;
          }
          currentEnumeratorValue_ss<< currentEnumeratorValue_i;
          if (!currentEnumeratorValue_ss.good()) {
              cout<< "Not good..." <<endl;
              cout<< "Value was not added to stringstream." <<endl;
          }
          currentEnumeratorValue_ss >> currentEnumeratorValue_s;
          if (!currentEnumeratorValue_ss.good()) {
              cout<< "Not good..." <<endl;
              cout<< "Stringstream did not insert value into string." <<endl;
          }
          output_s += currentEnumeratorValue_s;
          output_s += "));\n";
    }
    output_s += "\n}\n\n";
    Here is what the output shows now:
    currentEnumName_s is: DAY
    We found an enumerator.
    currentEnumeratorName_s is: sunday
    enumeratorsCounter_ui is: 1
    enumeratorsMap_m_si.find("sunday")->second is: 0
    We found an enumerator.
    We found an enumerator with an assigned value.
    currentEnumeratorValue_i is: 0
    currentEnumeratorName_s is: monday
    currentEnumeratorHasAssignedValue_b is true.
    enumeratorsMap_m_si.find("monday")->second is: 0
    We found an enumerator.
    currentEnumeratorName_s is: tuesday
    The current enumerator is not the first and does not have an assigned value.
    currentEnumeratorValue_i before incrementing: 0
    currentEnumeratorValue_i after incrementing: 1
    enumeratorsCounter_ui is: 3
    enumeratorsMap_m_si.find("tuesday")->second is: 1
    We found an enumerator.
    currentEnumeratorName_s is: wednesday
    The current enumerator is not the first and does not have an assigned value.
    currentEnumeratorValue_i before incrementing: 1
    currentEnumeratorValue_i after incrementing: 2
    enumeratorsCounter_ui is: 4
    enumeratorsMap_m_si.find("wednesday")->second is: 2
    We found an enumerator.
    currentEnumeratorName_s is: thursday
    The current enumerator is not the first and does not have an assigned value.
    currentEnumeratorValue_i before incrementing: 2
    currentEnumeratorValue_i after incrementing: 3
    enumeratorsCounter_ui is: 5
    enumeratorsMap_m_si.find("thursday")->second is: 3
    We found an enumerator.
    currentEnumeratorName_s is: friday
    The current enumerator is not the first and does not have an assigned value.
    currentEnumeratorValue_i before incrementing: 3
    currentEnumeratorValue_i after incrementing: 4
    enumeratorsCounter_ui is: 6
    enumeratorsMap_m_si.find("friday")->second is: 4
    We found an enumerator.
    currentEnumeratorName_s is: saturday
    The current enumerator is not the first and does not have an assigned value.
    currentEnumeratorValue_i before incrementing: 4
    currentEnumeratorValue_i after incrementing: 5
    enumeratorsCounter_ui is: 7
    enumeratorsMap_m_si.find("saturday")->second is: 5
    namesOfEnumerators_v_s.size() is: 7
    valuesOfEnumerators_v_i.size() is: 7
    enumeratorsMap_m_si.size() is: 7
    numOfEnumerators_ui is: 7
    In int main(), currentEnumeratorValue_i is: 0
    Not good...
    Value was not added to stringstream.
    Not good...
    Stringstream did not insert value into string.
    In int main(), currentEnumeratorValue_i is: 0
    Not good...
    Value was not added to stringstream.
    Not good...
    Stringstream did not insert value into string.
    In int main(), currentEnumeratorValue_i is: 1
    Not good...
    Value was not added to stringstream.
    Not good...
    Stringstream did not insert value into string.
    In int main(), currentEnumeratorValue_i is: 2
    Not good...
    Value was not added to stringstream.
    Not good...
    Stringstream did not insert value into string.
    In int main(), currentEnumeratorValue_i is: 3
    Not good...
    Value was not added to stringstream.
    Not good...
    Stringstream did not insert value into string.
    In int main(), currentEnumeratorValue_i is: 4
    Not good...
    Value was not added to stringstream.
    Not good...
    Stringstream did not insert value into string.
    In int main(), currentEnumeratorValue_i is: 5
    Not good...
    Value was not added to stringstream.
    Not good...
    Stringstream did not insert value into string.
    We found a single-line enum.
    currentEnumName_s is: AREYOUAROCK
    We found an enumerator with an assigned value.
    currentEnumeratorValue_i is: 1
    currentEnumeratorName_s is: yes
    currentEnumeratorHasAssignedValue_b is true.
    enumeratorsMap_m_si.find("yes")->second is: 1
    We're at the last enumerator of the single-line enum.
    We found an enumerator that has an assigned value.
    currentEnumeratorValue_i is: 0
    currentEnumeratorHasAssignedValue_b is true.
    enumeratorsMap_m_si.find("no")->second is: 0
    namesOfEnumerators_v_s.size() is: 2
    valuesOfEnumerators_v_i.size() is: 2
    enumeratorsMap_m_si.size() is: 2
    numOfEnumerators_ui is: 7
    In int main(), currentEnumeratorValue_i is: 1
    Not good...
    Value was not added to stringstream.
    Not good...
    Stringstream did not insert value into string.
    In int main(), currentEnumeratorValue_i is: 0
    Not good...
    Value was not added to stringstream.
    Not good...
    Stringstream did not insert value into string.
    currentEnumName_s is: Work
    We found an enumerator.
    We found an enumerator with an assigned value.
    currentEnumeratorValue_i is: 1
    currentEnumeratorName_s is: hard
    currentEnumeratorHasAssignedValue_b is true.
    enumeratorsMap_m_si.find("hard")->second is: 1
    We're at the last enumerator of the enum.
    The last enumerator of the enum has an assigned value.
    currentEnumeratorValue_s is: 2
    currentEnumeratorValue_i is: 2
    currentEnumeratorName_s is: lazy
    currentEnumeratorHasAssignedValue_b is true.
    enumeratorsMap_m_si.find("lazy")->second is: 2
    namesOfEnumerators_v_s.size() is: 2
    valuesOfEnumerators_v_i.size() is: 2
    enumeratorsMap_m_si.size() is: 2
    numOfEnumerators_ui is: 2
    In int main(), currentEnumeratorValue_i is: 1
    Not good...
    Value was not added to stringstream.
    Not good...
    Stringstream did not insert value into string.
    In int main(), currentEnumeratorValue_i is: 2
    Not good...
    Value was not added to stringstream.
    Not good...
    Stringstream did not insert value into string.
    The current line with 'enum' has a comment.
    currentEnumName_s is: weirdenum
    We found an enumerator.
    currentEnumeratorName_s is: weirdenum1
    enumeratorsCounter_ui is: 1
    enumeratorsMap_m_si.find("weirdenum1")->second is: 0
    We found an enumerator.
    We found an enumerator with an assigned value.
    currentEnumeratorValue_i is: 2
    currentEnumeratorName_s is: weirdenum2
    currentEnumeratorHasAssignedValue_b is true.
    enumeratorsMap_m_si.find("weirdenum2")->second is: 2
    We're at the last enumerator of the enum.
    The last enumerator of the enum has an assigned value.
    currentEnumeratorValue_s is: 0
    currentEnumeratorValue_i is: 0
    currentEnumeratorName_s is: weirdenum3
    currentEnumeratorHasAssignedValue_b is true.
    enumeratorsMap_m_si.find("weirdenum3")->second is: 0
    namesOfEnumerators_v_s.size() is: 3
    valuesOfEnumerators_v_i.size() is: 3
    enumeratorsMap_m_si.size() is: 3
    numOfEnumerators_ui is: 3
    In int main(), currentEnumeratorValue_i is: 0
    Not good...
    Value was not added to stringstream.
    Not good...
    Stringstream did not insert value into string.
    In int main(), currentEnumeratorValue_i is: 2
    Not good...
    Value was not added to stringstream.
    Not good...
    Stringstream did not insert value into string.
    In int main(), currentEnumeratorValue_i is: 0
    Not good...
    Value was not added to stringstream.
    Not good...
    Stringstream did not insert value into string.
    So, its clear the stringstream operations are failing for some reason. That is why all the enumerator values are 0 when they're outputted to file.
    I'm an alien from another world. Planet Earth is only my vacation home, and I'm not liking it.

  15. #15
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,401
    Quote Originally Posted by Programmer_P View Post
    Don't you mean it didn't pass the sight test?
    AFAIK, your nose can't smell code.
    Code smell - Wikipedia, the free encyclopedia
    Quote Originally Posted by Programmer_P View Post
    Ok, I pinpointed the problem: So, its clear the stringstream operations are failing for some reason. That is why all the enumerator values are 0 when they're outputted to file.
    Yes, that's clear. However, you still haven't given us any code with which we can help you. Why not try something like this:
    Code:
    {//local stringstream instead of...whatever scope it's at now.
       std::stringstream currentEnumeratorValue_ss;
       currentEnumeratorValue_ss << currentEnumeratorValue_i;
       if (!currentEnumeratorValue_ss.good()) {
           cout<< "Not good..." <<endl;
           cout<< "Value was not added to stringstream." <<endl;
       }
       //use .get() to get the string
       currentEnumeratorValue_s = currentEnumeratorValue_ss.get();
       if (!currentEnumeratorValue_ss.good()) {
           cout<< "Not good..." <<endl;
           cout<< "Stringstream did not insert value into string." <<endl;
       }
    }
    output_s += currentEnumeratorValue_s;
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

Popular pages Recent additions subscribe to a feed