Help with counting problem

This is a discussion on Help with counting problem within the C++ Programming forums, part of the General Programming Boards category; Hello everyone, I am new and hope someone can help me! The problem I am working on is: Code: Write ...

  1. #1
    Registered User
    Join Date
    Jan 2013
    Posts
    6

    Help with counting problem

    Hello everyone, I am new and hope someone can help me! The problem I am working on is:

    Code:
    Write a C++ program that reads several lines of text from a file and prints a table indicating the number of occurrences of each different word in the text. Also, your program should include the words in which they appear in the text. For example, the lines To be, or not to be: that is a question:Whether 'tis nobler in the mind to suffer”contains the words "to" three times, the word "be" two times, the word "or" once, etc.

    This is what I have so far! It seems to run fine if I use cin >> string;, but once i load it from the file, the output i get is :

    to - 1
    be, - 1
    or - 1
    ect.

    Code:
    #include <iostream>#include <string>
    #include <fstream>
    using namespace std;
    void number_of_words(char words[] );
    
    
    int main()
    {
    
    
         ifstream inData;
        ofstream outData;
        inData.open("sentences.dat");
         if (!inData.good()) 
        return 1; // exit if file not found
        outData.open("results.dat");
        char string[100];
        while(!inData.eof())
        {
            inData.getline(string,100);
            number_of_words(string);
            
        }
        return 0;
        inData.close();
    }
    
    
    void number_of_words( char words[] )
    {
        // Variable declaration.
         // Variable declaration.
          char *ptr[25], *token;
          int counter[25], i = 0, test = 0;
    
    
          // initialize each element of counter to 1.
          for( int a = 0; a < 25; a++ ) {
              counter[a]=1;
          }
    
    
          // get a token from string
          token = strtok( words , " ,;" );
          // assigns the token to 0th element of array.
          ptr[0] = token;
          token = strtok( NULL, " ,;" );
    
    
          while( token != NULL )
          {
                 for( int j = 0; j < i+1; j++ )
                 {
                        test = 0;
    
    
                        int compare = strcmp(ptr[j], token);
                        // if this token is already count then increment it.
                        if( compare == 0 )
                        {
                               counter[j]++;
                               test++;
                               break;
                        }
    
    
                 }
    
    
                 // if this token is not yet found, then assign it to a new element
                 if( test == 0 )
                 {
                        i++;
                        
                        ptr[i] = token;
                 }
    
    
                 token = strtok( NULL, " " );
          }
          
          for( int a = 0; a < i+1; a++ )
                 cout << ptr[a] << "   " 
                      << counter[a] << " times." << endl;
    }

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    2,917
    Is there a reason you are using C-strings instead of std::string? In my opinion the string class has member functions that make processing the string much easier than using the C-string functions like strtok().

    Also you shouldn't name a variable "string" in a C++ program, C++ has a class that has that name already.

    Jim

  3. #3
    Registered User
    Join Date
    Jan 2013
    Posts
    6
    Quote Originally Posted by jimblumberg View Post
    Is there a reason you are using C-strings instead of std::string? In my opinion the string class has member functions that make processing the string much easier than using the C-string functions like strtok().

    Also you shouldn't name a variable "string" in a C++ program, C++ has a class that has that name already.

    Jim
    I was using them as i wasn't sure how else to complete the question. How are you suggesting i fix it?

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    2,917
    I suggest using std::string in place of the char* and std::vector in place of the array. Then you can use the extraction operator>> to extract the "words" from the file. Then you can either count the occurrences of each word, or use a map to do the work for you.

    Jim

  5. #5
    Registered User
    Join Date
    Jan 2013
    Posts
    6
    The only problem is we have yet to learn vector instead of arrays! I will look into 'map' tho!

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,007
    Quote Originally Posted by ownerpurez View Post
    The only problem is we have yet to learn vector instead of arrays!
    Then do a little research on the side
    It will do you good!
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with word counting problem
    By optimizer_777 in forum C Programming
    Replies: 6
    Last Post: 01-20-2012, 04:46 PM
  2. problem with character counting program
    By shyam.sunder91 in forum C Programming
    Replies: 3
    Last Post: 05-08-2011, 06:20 AM
  3. Problem with counting
    By jinjinodie in forum C++ Programming
    Replies: 5
    Last Post: 10-23-2010, 11:05 AM
  4. counting problem
    By javani in forum C Programming
    Replies: 16
    Last Post: 04-04-2007, 12:25 AM
  5. Problem with letter and word counting
    By wordup in forum C Programming
    Replies: 3
    Last Post: 10-09-2002, 05:02 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21