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!
// 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..
#include <fstream>
#include <string>
#include <iostream>
void get_caption (std::fstream& file,
std::string& captionText,
unsigned captionWidth,
unsigned& captionLines)
{
std::string eachLine;
while (!file.eof())
{
std::getline(std::cin,eachLine);
captionText += eachLine;
}
unsigned charsPerLine = (12/100) * captionWidth; // 12 chars fit under 100 pixels of image
for (int i=charsPerLine; i<captionText.size(); i+=charsPerLine)
{
if (i >= captionText.size())
return;
while (captionText.at(i) != ' ' && captionText.at(i) != '\n')
i--;
captionText.at(i) = '\n';
captionLines++;
}
return;
}