Trouble with this function..

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Apr 2007
Posts: 84
Reputation: phalaris_trip is an unknown quantity at this point 
Solved Threads: 5
phalaris_trip's Avatar
phalaris_trip phalaris_trip is offline Offline
Junior Poster in Training

Trouble with this function..

 
0
  #1
Jun 25th, 2007
Hi,
I'm basically trying to create captions that will go under an image in my project.. the problem is that if the caption line is too long, it will get chopped off... so I'm trying to write a function that will re-chop a string with endl's according to the width of the images..

But when I run this, I'm getting runtime errors, saying something about bad memory allocation.. I can't see what the problem is, and I can't really think of an easier way to do it..

Any help is appreciated!

  1. // Basically the function reads text from a file (file) and updates a string (captionText) by reference, using captionWidth as a parameter.. captionLines is also updated for other reasons..
  2.  
  3. #include <fstream>
  4. #include <string>
  5. #include <iostream>
  6.  
  7. void get_caption (std::fstream& file,
  8. std::string& captionText,
  9. unsigned captionWidth,
  10. unsigned& captionLines)
  11. {
  12. std::string eachLine;
  13. while (!file.eof())
  14. {
  15. std::getline(std::cin,eachLine);
  16. captionText += eachLine;
  17. }
  18. unsigned charsPerLine = (12/100) * captionWidth; // 12 chars fit under 100 pixels of image
  19. for (int i=charsPerLine; i<captionText.size(); i+=charsPerLine)
  20. {
  21. if (i >= captionText.size())
  22. return;
  23. while (captionText.at(i) != ' ' && captionText.at(i) != '\n')
  24. i--;
  25. captionText.at(i) = '\n';
  26. captionLines++;
  27. }
  28. return;
  29. }
Reply With Quote Quick reply to this message  
Join Date: Feb 2007
Posts: 539
Reputation: thekashyap will become famous soon enough thekashyap will become famous soon enough 
Solved Threads: 50
thekashyap's Avatar
thekashyap thekashyap is offline Offline
Posting Pro

Re: Trouble with this function..

 
0
  #2
Jun 26th, 2007
Line 15: Reading from cin, read from "file".
Line 18: This would always set charsPerLine to 0. (12/100) would evaluate to 0. Make it (12.0/100.0)
Line 23-24: Not clear what are you trying to do. Remember getline() remove teh '\n' char. So you won't find that in captionText. This would crash also. Assuming that charsPerLine is 5 AND there is no space in first 5 chars of captionText. This loop will keep doing --i and would make i negative, which will result in crash. (to protect against the crash you can add one more condition i >= 0 as the first condition in the while, but I'm not sure what are you doing so can't say if it's correct to do)
Line 25: This would replace ith char, not insert '\n' as (i+1)th char.

Assuming that your file contains one caption per line; and you want to read each caption from the file, chop off the unwanted chars; and store these captions in a single std::string variable separated by '\n' you can do it like this:
  1. while( ! EOF )
  2. {
  3. read eachLine from file
  4. if( eachLine.size() > allowed_caption_size )
  5. output_string += chopOff( eachLine ) + '\n' ;
  6. else
  7. output_string += eachLine + '\n' ;
  8. }
  9. //PS: remove extra '\n' at the end if required
Finally: I would suggest that you use vector instead of a single string containing all captions separated by '\n'. This seems to be a perfect usecase for vector. If you use it your function signature would look like this:
  1. void get_caption (fstream& file,
  2. vector<string>& captionTextsVec,
  3. unsigned captionWidth)
  4. //PS: captionLines not needed as captionTextsVec.size() would give you that
Are you Agile.. ?
Reply With Quote Quick reply to this message  
Join Date: Apr 2007
Posts: 84
Reputation: phalaris_trip is an unknown quantity at this point 
Solved Threads: 5
phalaris_trip's Avatar
phalaris_trip phalaris_trip is offline Offline
Junior Poster in Training

Re: Trouble with this function..

 
0
  #3
Jun 26th, 2007
Thanks for your help man,

..the getline(cin,..) bit was a typo while I was posting, it actually didn't work regardless of that..

The problem was, as you pointed out, that I was using integer division..
In any case I changed the function completely, and it seems to work now. Vectors wouldn't work because I'm passing captionText into a function that takes a string as a parameter..

Lines 23/24 I was basically saying "if eachLine exceeds allowed width, then (starting at that width and working down) find the nearest white space.. then chop the head of the string off and leave on current line.. then repeat procedure on tail of string until it's within allowed limits"..
(The logic here is that I don't chop words in half, but look for a gap between them so I can chop the sentence without chopping words)

Line 25 I was purposely replacing the whitespace char with a newline char.. the idea being that if I have a sentence like "this is a typical lymphocyte", and max chars is 25 or something, then I end up with:
"this is a typical"
"lymphocyte"
where the space is replaced by a newline char..

But as I said, it seems to work fine now.. I can post you a snippet if you want.. it's probably not the tersest algorithm..

thanks again for your time
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC