Thread: need help with string output

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    81

    need help with string output

    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
    "Our greatest glory consists not in never failing,
    but in rising every time we fall."

    Oliver Goldsmith (1730-1774).
    Anglo-Irish writer, poet and playwright.

  2. #2
    Registered User
    Join Date
    Apr 2002
    Posts
    139
    use [ code ] and [ /code ] without the spaces
    to keep formating in your code sections
    "The most common form of insanity is a combination of disordered passions and disordered intellect with gradations and variations almost infinite."

  3. #3
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,490
    i'll format if for you because at least you made the effort:
    Code:
    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

  4. #4
    Registered User blight2c's Avatar
    Join Date
    Mar 2002
    Posts
    266
    i think i understand what your saying. try this and tell me what happens (but make sure it's in the while loop, not out like you did):
    Code:
    if (len>temp) // checking for longest word 
        { 
          temp=len; 
          cout << word; 
          temp1=word; 
          cout << temp1; // THIS PRINTS THE WORDS OK 
    
        } 
    else
         cout<<temp;

  5. #5
    Registered User matheo917's Avatar
    Join Date
    Sep 2001
    Posts
    279
    you are not doing anything wrong here i guess you just got too nested within your own code....

    First of all you are checking the length of a "word" that is being read in....if it is bigger than the "int" value of Temp, if so then Temp receives this value,
    Next you assign this and every single one of the words that you iterate, to Temp1, until you hit eof()... simple said, every time you read in a word it will be assigned to Temp1, thus when you get to the end of the file Temp1 will be automatically the last word in a file...

    I presume you want to also print to the screen at the end the word that is the longest ... correct???

    if so, then simply on the top, where all the declrations are, declare another pointer...


    Code:
           char* Temp2;
    and where your comparing if statement is just put this....

    Code:
    if (len>temp) // checking for longest word 
        { 
          temp=len; 
          Temp2 = word;
          cout << word; 
          temp1=word; 
          cout << temp1; // THIS PRINTS THE WORDS OK 
    
        }
    and at the end if you just print Temp2 then it should give you the longest word......

    hope I cleared it up for you a bit...?

    Regards,
    matheo917

  6. #6
    Registered User
    Join Date
    Apr 2002
    Posts
    81
    Code:
    void words(int &word_count, float &ave_len, char* &longest)  	 // Counting the words
    {
    char word[30];
    	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);
    		
    			while (len>temp)		// checking for longest word
    			{
    				temp=len;
    				strcpy(longest,word);
    				
    			}
    
    		total_len=len+total_len;	// total for all words
    		word_count++;
    
    	} // END WHILE
    	
    
    		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
    "Our greatest glory consists not in never failing,
    but in rising every time we fall."

    Oliver Goldsmith (1730-1774).
    Anglo-Irish writer, poet and playwright.

  7. #7
    Registered User
    Join Date
    Apr 2002
    Posts
    81
    Dang, I posted it without wanting to

    Thanks for the replies guys! It is much appreciated! I will try both suggestions just to make me practice.

    After many tries and failures, I used a strcpy and 2 while loops, plus a char* and it worked!

    If anyone can give comments on how I coded, please do so. I am here to learn and all comments are appreciated!
    "Our greatest glory consists not in never failing,
    but in rising every time we fall."

    Oliver Goldsmith (1730-1774).
    Anglo-Irish writer, poet and playwright.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. OOP Question DB Access Wrapper Classes
    By digioz in forum C# Programming
    Replies: 2
    Last Post: 09-07-2008, 04:30 PM
  2. String Class
    By BKurosawa in forum C++ Programming
    Replies: 117
    Last Post: 08-09-2007, 01:02 AM
  3. Replies: 4
    Last Post: 04-03-2007, 05:57 AM
  4. problems with overloaded '+' again
    By Brain Cell in forum C++ Programming
    Replies: 9
    Last Post: 04-14-2005, 05:13 PM
  5. can anyone see anything wrong with this code
    By occ0708 in forum C++ Programming
    Replies: 6
    Last Post: 12-07-2004, 12:47 PM