• ### Announcements

#### Archived

This topic is now archived and is closed to further replies.

# Char Incrementing Problem

## Recommended Posts

Chryzmo    122
Heyas, I am putting together a text based adventure game. I am having this problem with loading my levels. I keep the file name in a variable, it was a char until I realized once the player gets to the final level of the 9th file (map9.txt) and the file name variable gets incremented to 10, the char doesn't work anymore.
I have the game put together the file name by doing this:

string mapName;
char levelNumber;

mapName = "map";
mapName += levelNumber;
mapName += ".txt";

I also tried making the levelNumber variable an int, but when I run the program the mapName ends up looking something like this: map .txt (edit: there should be 5 spaces here, even though my post only shows one) Is there someway to append an int to a string? Or maybe someone could suggest another way to go about solving this problem? Thanks, Chris [edited by - Chryzmo on January 6, 2004 7:59:27 PM]

##### Share on other sites
falkone    444
A char only has room for a single character. "10" consists of two characters.

The simple solution to that would be to to expand support for two level number characters, or to just use a string instead.

##### Share on other sites
AnthonyLewis    142
There''s a class called stringstream that does just what you want. Here''s a quick example for you.

#include <iostream>#include <sstream>                                                                                                    using namespace std;                                                                                                    int main ( int argc, char *argv[] ) {        int level_num = 10;        stringstream map_name;                                                                                                            map_name << "map" << level_num << ".txt";                                                                                                            cout << map_name.str() << endl;}

That should print out "map10.txt"

Hope that helps,

Tony

##### Share on other sites
Chryzmo    122
Thanks a lot, that was perfect.

Now, I have another question, this time regarding classes. I want to seperate my map functions from the rest of my code, and put them in their own class. However, there are a few functions in my CGame class that I need to call a public function in my new CMap class. Is there someway to do this in the function declarations?

// Here is an example:    if (cBoard[mYPOS][mXPOS] == ''U'')    {        iCurrentLevel++;        if (iCurrentLevel == 10)        {            CMap.LoadFile();            iCurrentLevel = 0;            CMap.LoadMap(iCurrentLevel);        } else {            CMap.LoadMap(iCurrentLevel);        }    }// LoadFile() and LoadMap(int level) are public functions in my new CMap class.

I get the following error:

TAGFunctions.cpp: In member function void CGame::GetInput()'':

TAGFunctions.cpp:90: parse error before .'' token
TAGFunctions.cpp:92: parse error before .'' token
TAGFunctions.cpp:94: parse error before .'' token

Thanks,
Chris

##### Share on other sites
Are you seperating your code into .h and .cpp files? If so, then at the beginning of the file CGame.cpp, make sure to #include "CMap.h"

If you aren''t seperating your code into .h and .cpp, then umm, start doing that.

##### Share on other sites
porthios    130
Or do you have a variable named CMap? Or is that your class name? To me it sounds like the class name and if so, you cannot do that.

CMap myMap;// Here is an example:    if (cBoard[mYPOS][mXPOS] == ''U'')    {        iCurrentLevel++;        if (iCurrentLevel == 10)        {            myMap.LoadFile();            iCurrentLevel = 0;            myMap.LoadMap(iCurrentLevel);        } else {            myMap.LoadMap(iCurrentLevel);        }    }// LoadFile() and LoadMap(int level) are public functions in my new CMap class.

##### Share on other sites
Chryzmo    122
Hmm... that is what I thought. However, I called: CMap map
from my main loop, but it still gives me this error when I change the function calls.

Error:

TAGFunctions.cpp: In member function void CGame::GetInput()'':

TAGFunctions.cpp:90: map'' undeclared (first use this function)
TAGFunctions.cpp:90: (Each undeclared identifier is reported only once for each
function it appears in.)

int main(){CGame game;CMap map;   // other stuff   return 0;}// New function calls// This is within the definition of one of my functionsCGame::GetInput(){//...    if (cBoard[mYPOS][mXPOS] == ''U'')    {        iCurrentLevel++;        if (iCurrentLevel == 10)        {            map.LoadFile();            iCurrentLevel = 0;            map.LoadMap(iCurrentLevel);        } else {            map.LoadMap(iCurrentLevel);        }    }//...}

I believe the problem is because this is in the definition of CGame::GetInput(), so I''m not even sure I can call a function from a differnt class. However, I would think there is some way to do it, right?

Thanks,
Chris