944,111 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 930
  • C++ RSS
Jun 25th, 2007
0

Trouble with this function..

Expand Post »
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!

C++ Syntax (Toggle Plain Text)
  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. }
Similar Threads
Reputation Points: 15
Solved Threads: 5
Junior Poster in Training
phalaris_trip is offline Offline
91 posts
since Apr 2007
Jun 26th, 2007
0

Re: Trouble with this function..

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:
C++ Syntax (Toggle Plain Text)
  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:
C++ Syntax (Toggle Plain Text)
  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
Reputation Points: 254
Solved Threads: 74
Practically a Posting Shark
thekashyap is offline Offline
804 posts
since Feb 2007
Jun 26th, 2007
0

Re: Trouble with this function..

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
Reputation Points: 15
Solved Threads: 5
Junior Poster in Training
phalaris_trip is offline Offline
91 posts
since Apr 2007

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: program does not seems to run to end.
Next Thread in C++ Forum Timeline: One Dimensional Array





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC