Hello everyone!
I am having this little problem with my code. I am trying to print the longest word in a paragraph. Within my IF statement, the words print ok (words are printed until the biggest word is found) but when the program ends, the word printed is the last word of the paragraph. I've tried with a character array and with pointers. I think a pointer may be needed here, although I don't know much about them. I've tried other ways as well but I just can't figure it out.
Any comment would be appreciated.
As well can anyone tell me how to format my code properly on this board? When I copy and paste it looks fine but when I preview it, everything is right justified.
void words(int &word_count, float &ave_len) // Counting the words
{
char word[30];
char *temp1;
int len=0, temp=0;
float total_len=0;
ave_len=0;
infile.open ("parag2.txt", ios::in);
while (!infile.eof())
{
infile >> word;
len=strlen(word);
if (len>temp) // checking for longest word
{
temp=len;
cout << word;
temp1=word;
cout << temp1; // THIS PRINTS THE WORDS OK
}
total_len=len+total_len; // total for all words
word_count++;
} // END WHILE
cout << temp;
cout << temp1; // THIS GIVES ME THE LAST WORD IN THE PARAGRAPH
ave_len=float(total_len/word_count); // Average lenght of the words that will be passed
// to other function for printing
infile.close();
} // END WORDS