C++ File I/O

Thread Solved

Join Date: Oct 2005
Posts: 11
Reputation: kdw3 is an unknown quantity at this point 
Solved Threads: 0
kdw3 kdw3 is offline Offline
Newbie Poster

C++ File I/O

 
0
  #1
Nov 29th, 2005
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
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 598
Reputation: SpS is on a distinguished road 
Solved Threads: 32
SpS's Avatar
SpS SpS is offline Offline
Posting Pro

Re: C++ File I/O

 
0
  #2
Nov 29th, 2005
It sounds like homework....post what you have done till now and ask specific question regarding that code
Reply With Quote Quick reply to this message  
Join Date: Oct 2005
Posts: 11
Reputation: kdw3 is an unknown quantity at this point 
Solved Threads: 0
kdw3 kdw3 is offline Offline
Newbie Poster

Re: C++ File I/O

 
0
  #3
Nov 30th, 2005
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.

  1. int Displayhist (int scaledfrequency[])
  2. {
  3. cout << setw(4);
  4. for (int row = 20 ; row >= 0; row--)
  5. {
  6. if (row == 20)
  7. {
  8. cout << "1.0 ";
  9. }
  10. else if (row == 0)
  11. {
  12. cout << "0.0 ";
  13. }
  14. else if (row == (20 / 2))
  15. {
  16. cout << "0.5 ";
  17. }
  18. else
  19. {
  20. cout << " . ";
  21. }
  22.  
  23. for (int k = 0; k < 20; k++)
  24. {
  25.  
  26. if (scaledfrequency[k] >= row)
  27. {
  28. cout << "*";
  29. }
  30. else
  31. {
  32. cout << " ";
  33. }
  34. }
  35.  
  36. cout << endl;
  37. }
  38. for (int j = 0; j <= 20; j++)
  39. {
  40. cout << j;
  41. }
  42. cout << endl;
  43. }
<< moderator edit: added [code][/code] tags >>

Regards
Kevin
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,771
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 308
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: C++ File I/O

 
0
  #4
Nov 30th, 2005
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 << ' ';
    }
“The essential notion of a socialist society is force.”
— Milton Friedman
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum


Views: 2416 | Replies: 3
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2010 DaniWeb® LLC