Hi,

I am trying to make a word counter, for that I have to input a string line untill '@@@' character is entered.
My problem is that when I enter "Hello hello" as my input it reads the last hello twice.. or this is what I think it does..
Right now I am just assuming that it takes input untill '\n' is entered.

Here is my code:

class Word
{
      public:
             void setName(string);
             void setCount(int);
             int getCount();
             string getName();
             
      private:
              string name;
              int count;
};
int main(int argc, char *argv[])
{
    Word listWord[200];
    string input;
    int len, tempcount, check = 0, listLen = 0;
    
    cout<<"Please enter your sentences, you can enter '@@@' by a new line to finish the input: ";
    getline(cin, input);
    
    istringstream in(input);
    string singleword;
    do
    {
          check = 0;
          in>>singleword;
          len = singleword.length();
          for(int i=0; i<len; i++)
          {
                singleword[i] = tolower(singleword[i]);                  
          }
          for(int j=0; j<listLen; j++)
          {
                  if(listWord[j].getName() == singleword)
                  {
                          tempcount = listWord[j].getCount();
                          listWord[j].setCount(tempcount+1);
                          check = 1;
                          break;
                  }
          }
          if(check == 0)
          {
                   listWord[listLen].setName(singleword);
                   listWord[listLen].setCount(1);
                   listLen++;
          }
          
          
    }while(in);
    

	int distinctWord = 0;
	int totalWord = 0;
	for(int i=0; i<listLen; i++)
    {
            if(listWord[i].getCount() == 1)
                     distinctWord++;
			totalWord += listWord[i].getCount();
    }

    cout<<"\nTotal Number of words: "<<totalWord<<endl;
	cout<<"Total Number of distinct words: "<<distinctWord<<endl;
    
    cout<<"\nWord\tCount"<<endl;
    for(int i=0; i<listLen; i++)
    {
            cout<<listWord[i].getName()<<"\t"<<listWord[i].getCount()<<endl;
    }
    
    system("PAUSE");
    return EXIT_SUCCESS;
}

void Word::setName(string newName)
{
     name = newName;
}

string Word::getName()
{
       return name;
}

void Word::setCount(int newCount)
{
     count = newCount;       
}

int Word::getCount()
{
    return count;    
}

And also, it wont let me use getline(cin, input, '@@@'), if I try to enforce that condition.

Thanks in advance.

And also, it wont let me use getline(cin, input, '@@@'), if I try to enforce that condition.

Thanks in advance.

That's because the delimiter has to be a character....'@' is a character but '@@@' is not.

Why did you post your C++ question in the C section?

using '@' as a deliminator in getline() is not what you want to do anyway. To make a word counter use >> operator, not getline()

string input;
int word_count = 0;
while( cin >> input)
{
   if( input == "@@@" }\
     break;
   word_count++;
}

Edited 6 Years Ago by Ancient Dragon: n/a

When I use the cin>>input instead of getline(cin, input), then it takes only the first word and store it in input.
But I want it to store the whole line.
For example if I input "Hi, hello, hello"
it counts "Hi" twice

Hey, I have improved my program and now its working if I enter @@@ at the end of line, but my requirement is
"You can stop your entering by inputting "@@@" in a new line as a sentinel."

How should I change my program to satisfy this requirement:

int main(int argc, char *argv[])
{
    Word listWord[200];
    string input;
    int len, tempcount, check = 0, listLen = 0;
    
    cout<<"Please enter your sentences, you can enter '@@@' by a new line to finish the input: ";
    getline(cin, input);
    
    istringstream in(input);
    string singleword;
    do
    {
          check = 0;
          in>>singleword;

		  if(singleword == "@@@")
				break;

          len = singleword.length();
          
		  for(int k=0; k<len; k++)
		  {
			  if(ispunct(singleword[k]))
			  {
				  if(singleword[k] != '-' && singleword[k] != '\'')
				  {
					  singleword.erase(k,1);
					  len = singleword.length();
					  k--;
				  }
			  }
		  }

		  len = singleword.length();

		  for(int i=0; i<len; i++)
                singleword[i] = tolower(singleword[i]);                  

          for(int j=0; j<listLen; j++)
          {
                  if(listWord[j].getName() == singleword)
                  {
                          tempcount = listWord[j].getCount();
                          listWord[j].setCount(tempcount+1);
                          check = 1;
                          break;
                  }
          }

          if(check == 0)
          {
                   listWord[listLen].setName(singleword);
                   listWord[listLen].setCount(1);
                   listLen++;
          }                    
    }while(in);

	sort(listWord, listLen);
	
	int distinctWord = 0;
	int totalWord = 0;
	for(int i=0; i<listLen; i++)
    {
            if(listWord[i].getCount() == 1)
                     distinctWord++;
			totalWord += listWord[i].getCount();
    }

    cout<<"\nTotal Number of words: "<<totalWord<<endl;
	cout<<"Total Number of distinct words: "<<distinctWord<<endl;
    
    cout<<"\nWord\t\tCount"<<endl;
    
	for(int i=0; i<listLen; i++)
            cout<<listWord[i].getName()<<"\t\t"<<listWord[i].getCount()<<endl;
    
    system("PAUSE");
    return EXIT_SUCCESS;
}

Thanks for helping.

This article has been dead for over six months. Start a new discussion instead.