DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   C++ (http://www.daniweb.com/forums/forum8.html)
-   -   C++ File I/O (http://www.daniweb.com/forums/thread36063.html)

kdw3 Nov 29th, 2005 9:59 am
C++ File I/O
 
I am trying to create a simple program to read in words from a file, count the words, and output a histogram of word lengths normalised to 1. I am having trouble reading in the words from the file. If anyone could help, it would be greatly appreciated.

Regards
Kevin

SpS Nov 29th, 2005 10:28 am
Re: C++ File I/O
 
It sounds like homework....post what you have done till now and ask specific question regarding that code

kdw3 Nov 30th, 2005 2:56 pm
Re: C++ File I/O
 
Ok, yes, it is homework, and I've been tearing my hair out trying to get it to work, but eventually got there. The final thing I need to do is adjust the output that is sent to the console. It is supposed to be a histogram displaying the frequency of word lengths. I can get it to display, but its very bunched up. I think I'm supposed to use the setw() function, but am not sure where it is supposed to be used. The code is below, and any help would be appreciated.

int Displayhist (int scaledfrequency[])
{
    cout << setw(4);
    for (int row = 20 ; row >= 0; row--)
    {
        if (row == 20)
        {
          cout << "1.0 ";
        }
        else if (row == 0)
        {
            cout << "0.0 ";
        }
        else if (row == (20 / 2))
        {
            cout << "0.5 ";
        }
        else
        {
            cout << " .  ";
        }

        for (int k = 0; k < 20; k++)
        {

            if (scaledfrequency[k] >= row)
            {
              cout << "*";
            }
            else
            {
                cout << " ";
            }
        }
   
    cout << endl;
    }
    for (int j = 0; j <= 20; j++)
    {
        cout << j;
    }
    cout << endl;
}
<< moderator edit: added [code][/code] tags >>

Regards
Kevin

Dave Sinkula Nov 30th, 2005 4:03 pm
Re: C++ File I/O
 
You need to make room for the numbers on the bottom, I'd guess. And that would mean you'd need to space out your columns:
        for (int k = 0; k < 20; k++)
        {

            if (scaledfrequency[k] >= row)
            {
              cout << " * ";
            }
            else
            {
                cout << "  ";
            }
        }

    cout << endl;
    }
    cout << "    ";
    for (int j = 0; j <= 20; j++)
    {
        cout << setw(2) << j << ' ';
    }


All times are GMT -4. The time now is 10:38 am.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC