Jump to content

Word count program noobie help

- - - - -

  • Please log in to reply
2 replies to this topic

#1
code_prowannabe

code_prowannabe

    Newbie

  • Members
  • Pip
  • 4 posts
I'm coding this program where you have to count words in a text, and one function has to count different words (example: "The pie is really really good" the words counted are "the", "pie", "is", "really", and "good" so not counting repeating words)
I've trying it with a loop but it count the first word but it didn't count the rest of the words.
so I tried manipulating the strings by filling in arrays of string of each word of the text and putting a space in each word but that didn't work to :crying:

Here's the program

//John Nguyen

//This program reads and prints out a text line by line

#include <iostream>

#include <fstream>

#include<string>

using namespace std;


const int MAX = 50;


void diffwords(string, ifstream &infile, ofstream &outfile);

int read(string[],int &,ofstream &outfile);

void change(string &);





int main()

{




ifstream infile("input.txt");

//ifstream infile("con");

//ofstream outfile("outfile.txt");

ofstream outfile("con");

string line[MAX];

int i;



 read(line,i,outfile);






 for(int count = 0; count < i; count++)

    {   change(line[count+1]);

        diffwords(line[count],infile,outfile);

    }



}



void diffwords(string str, ifstream &infile,ofstream &outfile)

{   string text;



    getline(infile,text);

    outfile << text << endl;


    int count = 0;

    string::size_type pos;


    pos = text.find(str,0);


    while(pos != string::npos)

        {

            count++;

            pos = text.find(str,pos+str.size());


        }

    outfile << "the word " << str << " has appeared " << count << " times" << endl;


}


int read(string str[], int &i,ofstream &outfile)

{

     i = 0;

    ifstream infile("input.txt");

    //ifstream infile("con");

 while(infile >> str[i])

   {


    outfile << str[i] << endl;

    i++;

   }


    return i;


}


void change(string &str)

{


    str.insert(0," ")

    


}

Here's the output too

Quote

The
pie
is
really
really
good
The pie is really really good
the word The has appeared 1 times

the word pie has appeared 0 times

the word is has appeared 0 times

the word really has appeared 0 times

the word really has appeared 0 times

the word good has appeared 0 times

Process returned 0 (0x0) execution time : 0.328 s
Press any key to continue.

please help

#2
mnirahd

mnirahd

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 330 posts
Hi,

Instead of using following loop

 for(int count = 0; count < i; count++)

    {   change(line[count+1]);

        diffwords(line[count],infile,outfile);

    }




Try Following:


 int countWord;

 string curWord;

 for(int count = 0; count < i; count++)

    {

      countWord = 0;

	  curWord = line[count];

	  for (int k = 0; k < i; k++)

	  {

		  if (curWord.compare(line[k]) == 0)

			  countWord++;

	  }

	  cout<<curWord<<" appeared "<<countWord<<" times."<<endl;

    }



I hope this helps!

Munir

#3
code_prowannabe

code_prowannabe

    Newbie

  • Members
  • Pip
  • 4 posts
omg thx you so much, I was over thinking it thanks so much man




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users