Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>As a solution to my comment above, the C++ version of the code you presented:</p> <pre><code>typedef map&lt;std::string, int&gt; wc; int main() { int c; string cc, nombre; wc m; std::cin &gt;&gt; c; while (c--) { std::cin &gt;&gt; cc; std::getline(std::cin, nombre); m[cc] += 1; // This should work } printmap(m); } </code></pre> <p><a href="http://ideone.com/2JP82" rel="nofollow">http://ideone.com/2JP82</a></p> <p>First: <code>std::map</code> sorts it's data based on the key, in your code, <code>char*</code>, which points at <code>char cc[75]</code>. So when you replaced the text in <code>cc</code>, then the keys of the map changed, and that breaks everything. The keys of a map <em>must not change ever</em>. Since we're using C++, you should not use <code>char[]</code> at all; use <code>std::string</code> instead, which (since it is a "value type") will make everything just magically work. I have no idea how it was working before, since you don't show the <code>printmap</code> function.</p> <p>Second: You call <code>printmap</code> each and every time you read a single line, and since a <code>map</code> has no way of printing "the last thing added", that idea makes no sense at all. The <code>printmap</code> call should probably print the entire map, and be outside the loop.</p> <p>Third: Don't use <code>scanf</code>, it's not safe. Use streams: <code>std::cin &gt;&gt; cc</code> for reading in a single word, or <code>std::getline(std::cin, nombre)</code> for reading in what's left on the line. That way the code won't crash if someone enters the line (sources for <a href="http://www.funtrivia.com/askft/Question33835.html" rel="nofollow">longest country name</a> and <a href="http://answers.yahoo.com/question/index?qid=20081022055524AAXVIOc" rel="nofollow">longest last name</a>)</p> <pre><code>Krungthepmahanakornamornratanakosinmahintarayuttha-yamahadilokphopnopparatrajathaniburiromudomrajaniw-esmahasatharnamornphimarnavatarnsathitsakkattiyavi-sanukamprasit Wolfeschlegelsteinhausenbergerdorffvortschaferswesenchafewarenwholgepflegeunzenvonangereifenduchihrraubgiriigfeindtausendjahresvorandieerscheinenbanderechiffgebrauchlichtalsseinursprungvonkrrthinzwischensternartigraumaufdersuchebtbewohnbarplanetenkreisedrehensichundstandigmenshlichkeittkonntevortpflanzeslamdlichfreudeundruhemitnichteinfurchrintlligentgeschopfsvonhinzwischenster </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload