Strings, Files and Integers

closed account (oE6pX9L8)
Hey guys,

I've been stuck on several problems. I'm still an inexperienced programmer of C++, and I've recently learnt arrays, pointers and dynamic memory.

There's two things that I'm having trouble with:
1. Turning an integer into a string.
2. Reading the amount of lines in a text file, and making an array accordingly (i.e. to read a list of weapon names into a game from an external file).

I'm sure there'll be a simple solution that I've probably missed, so I'm sorry about the simplicity of it.

Thanks for your time.
archmage84
1) http://www.cplusplus.com/articles/numb_to_text/

2) Use an std::vector or a similar container instead. Then you don't have to worry about the size.
closed account (oE6pX9L8)
1) Thanks, I think I'll be able to use that. Much appreciated.

2) Less successful, I'm not experienced at much past while loops and such, so would you mind elaborating on that?
Instead of doing something like this:

1
2
3
4
5
6
int my_array[SIZE];
//assume there is only SIZE ints in the file
for(unsigned int i = 0; i < SIZE; ++i) {
    //read an int
    my_array[i] = the_int;
}


You can just do something like this:
1
2
3
4
5
6
std::vector<int> my_vector;
while(/*there are still ints in the file*/) {
    //read an int
    my_vector.push_back(the_int); //adds it to the vector
    //no need to care about the size or anything
}
closed account (oE6pX9L8)
Sweet, that looks like what I need :D
Thanks for your help. Is there an article on this?

archmage84
Topic archived. No new replies allowed.