• FEATURED

View more

View more

View more

### Image of the Day Submit

IOTD | Top Screenshots

### The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.

Sign up now

# Help with for loops and strings when loading files.

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

8 replies to this topic

### #1Monkey_Missile  Members

Posted 20 February 2012 - 08:05 PM

With C++ and SDL I'm having a bit of a brain fart remembering and finding the syntax for what I'm trying to do. I have a for loop in a function that is loading files into an array. (char* file, int number) are the two parameters of the function where file is the first part of the file name and number is the number of files. I have the files named as such:

example0.png
example1.png
example2.png

The code for the for loop is as follows:

for (int i = 0; i < number; i++)
{
Array[i] = OnLoad((file) + i + ".png") ;
}

OnLoad is a function I have for loading files which works independently of this. My question is when I pass the parameters "example" and "3" to the function and thus this for loop how do I have the above translate it into "example0.png" and so on.

### #2Washu  Senior Moderators

Posted 20 February 2012 - 08:08 PM

You're using C++, so you should be using std::string, but anyways...
std::ostringstream ss;
ss<<file<<i<<".png";
std::string filename = ss.str();

ostringstream is located in the <sstream> header.

In time the project grows, the ignorance of its devs it shows, with many a convoluted function, it plunges into deep compunction, the price of failure is high, Washu's mirth is nigh.
ScapeCode - Blog | SlimDX

### #3zacaj  Members

Posted 20 February 2012 - 08:26 PM

You're using C++, so you should be using std::string, but anyways...

std::ostringstream ss;
ss<<file<<i<<".png";
std::string filename = ss.str();

ostringstream is located in the <sstream> header.

Is there any reason to do this vs:
OnLoad(string(file) + '0'+i+ ".png")

### #4Washu  Senior Moderators

Posted 20 February 2012 - 08:30 PM

THe result of '0'+i is an integer. an integer + std::string is not a valid operator combination (there is no appropriate overload).

In time the project grows, the ignorance of its devs it shows, with many a convoluted function, it plunges into deep compunction, the price of failure is high, Washu's mirth is nigh.
ScapeCode - Blog | SlimDX

### #5Monkey_Missile  Members

Posted 20 February 2012 - 08:33 PM

Washu's suggestion worked, thanks Now to get my rendering function to work. I'll be back if I get stuck again.

### #6zacaj  Members

Posted 20 February 2012 - 08:40 PM

You could always cast it. In my opinion that'd still look a lot better than instantiating a whole new class just to concatenate a string

### #7Washu  Senior Moderators

Posted 20 February 2012 - 08:41 PM

And the instant i is no longer a number between 0 and 9 you would immediately fail. Btw, the character immediately after '9' is ':', which as you can imagine doesn't work out so well in the middle of a filename.

In time the project grows, the ignorance of its devs it shows, with many a convoluted function, it plunges into deep compunction, the price of failure is high, Washu's mirth is nigh.
ScapeCode - Blog | SlimDX

### #8rip-off  Moderators

Posted 21 February 2012 - 03:44 AM

You could always lexical_cast<> it, which would hide the details of the conversion (as we don't really care about them in this case).

### #9Washu  Senior Moderators

Posted 21 February 2012 - 04:20 AM

You could always lexical_cast<> it, which would hide the details of the conversion (as we don't really care about them in this case).

True, if he's using boost. If he's using a modern compiler AND C++0x he might have access to std::to_string functionality as well.

However, this is For Beginners, so the best answer is usually the one that requires the fewest number of "If you have X and version of X is greater than Y" or "if you have X installed and configured correctly", which in this case happens to be stringstream.

In time the project grows, the ignorance of its devs it shows, with many a convoluted function, it plunges into deep compunction, the price of failure is high, Washu's mirth is nigh.
ScapeCode - Blog | SlimDX

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.