0

I have the C++ code. It reading file and look for anagrams in this file ("diction").

while (1)
{
    infile.getline(buff, sizeof(buff));
    if(infile.eof()) break;
    sbuff = buff;
    sort(sbuff.begin(), sbuff.end());
    an.insert(pair<string, string>(sbuff, buff));
}

im = an.begin();
ane = an.end();

vector<multimap<string, string>::iterator> chg;

chg.push_back(im);

while (++im != ane)
{
    chg.push_back(im);
    if((*im).first != (*chg[0]).first)
    {
        if(chg.size() > 2)
        {
            for(unsigned int i=0; i < chg.size() - 1; i++);
        cout << endl;
        }

        chg.clear();
        chg.push_back(im);
    }   

}

if(chg.size() > 1)
    for(unsigned int i=0; i < chg.size(); i++)
        cout << (*chg[i]).second << endl;
    infile.close();
}

After compiling it displays a brunch of empty rows and all. On MacOS on Fedora the same result

What's amiss?

1
  • Code comments are "amiss" Commented Jan 21, 2014 at 10:01

2 Answers 2

2

This for loop doesn't do anything in your code:

 {
    for(unsigned int i=0; i < chg.size() - 1; i++);
    cout << endl;
 }

you don't want the ; at the end of it, probably. If you take it away it'll just give you lots of empty lines though as you don't cout any text to the screen.

Sign up to request clarification or add additional context in comments.

1 Comment

thank you very much!!! Thank you very much! I delete ; at the end. And added this line after that: cout << (*chg[i]).second << endl; It's working!
1

i thing you miss the {

try this

if(chg.size() > 1)
{
    for(unsigned int i=0; i < chg.size(); i++)
    { 
        cout << (*chg[i]).second << endl;
     }
    infile.close();
}

instead of

if(chg.size() > 1)
    for(unsigned int i=0; i < chg.size(); i++)
        cout << (*chg[i]).second << endl;
    infile.close();
}

1 Comment

Thank's! In this case the braces no necessary. But it's working though as i don't {}. Although they improve code readability.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.