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.