Thread: I/o????????

  1. #1
    Registered User
    Join Date
    Apr 2003
    Posts
    20

    I/o????????

    I have to write a program that reads in data from a text file and
    tells how many of each punctuation mark is in the file and then tells the average length of each word in the file. so my question is what would be the best to do this.

  2. #2
    Registered User
    Join Date
    Nov 2002
    Posts
    1,109
    open file.
    read into string.
    count # of words
    count # of spaces
    count # of chars
    subtract: chars - spaces
    divide
    count # of punctuation marks
    close file.

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    20
    I am having trouble reading it into a string.

    that aint all but that will do for now

  4. #4
    Registered User
    Join Date
    Nov 2002
    Posts
    1,109
    Code:
    ifstream infile("filename.txt");
    if(!infile)
      exit(1);
    
    string mystring;
    getline(infile, mystring);

  5. #5
    Registered User axon's Avatar
    Join Date
    Feb 2003
    Posts
    2,572
    or you could read the whole file into an array...and then step through it picking up punctuations (hint: use ASCII values in a for loop) and at the same time counting all the printable letters (hint: function isalpha() ) as well as number of words. Than divide all the alphabetical characters by the number of words to get the average word length...

    post some of your code and we'll help you further, this isn't that hard

    axon

    some entropy with that sink? entropysink.com

    there are two cardinal sins from which all others spring: Impatience and Laziness. - franz kafka

  6. #6
    Registered User sikamikaniko's Avatar
    Join Date
    Mar 2003
    Posts
    28
    I suggest to open the file and get chars like this:

    Code:
    char letter;
    int count=0;
    
    while(ptr_file.get(letter))
    { 
           if(letter=='.' || letter==',' || letter=='!' || letter=='?')
             {count++;}
    }
    cout << "There are " << count << "punctuations in the file.";
    You can add more signs in the IF operator. Hope this will help You

  7. #7
    Registered User sikamikaniko's Avatar
    Join Date
    Mar 2003
    Posts
    28
    or in case to cont how many of each mark there are, just in the while add IFs. Also create more integers for each mark:
    Code:
    int punkt=0;
    int comma=0;
    ...
    while...
    {
          if(letter==',')
               comma++;
    etc.
    
    then cout the results

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. asynchronized I/O == multiplexing I/O?
    By George2 in forum C Programming
    Replies: 1
    Last Post: 07-24-2006, 10:06 AM
  2. why page based I/O can improve performance?
    By George2 in forum C Programming
    Replies: 1
    Last Post: 06-12-2006, 07:42 AM
  3. Nonblocking I/O
    By fnoyan in forum Linux Programming
    Replies: 4
    Last Post: 11-29-2005, 04:37 PM
  4. Overlapped I/O and Completion Port :: Winsock
    By kuphryn in forum Windows Programming
    Replies: 0
    Last Post: 10-30-2002, 05:14 PM
  5. WSAAsyncSelect I/O Mode :: Winsock
    By kuphryn in forum Windows Programming
    Replies: 1
    Last Post: 05-12-2002, 03:23 PM