Jump to content

[SOLVED] Putting togheder an string and an int?

- - - - -

  • Please log in to reply
10 replies to this topic

#1
Samuka97

Samuka97

    Newbie

  • Members
  • PipPip
  • 15 posts
So let's cut the introduction stuff here and just move on to my problem. My game loads the levels from external files, but I want to do something kinda like this:
string load_path;

int level_number = 1;

load_path = "gamedata/levels/level_" + level_number + ".map";

//etc loading the level

But obviously, it doesn't work. I tried:
char load_string[80];

char load_string_2[80];

load_string_2 = itoa(level_number);

load_string = "data\\level\\level" + load_string_2 + "_properties.dat";
and
char load_string[80];

strcat_s(load_string, "data\\level\\level");

strcat_s(load_string, level_number);

strcat_s(load_string, "_properties.dat");
but none work. So... I need some help here.

Thanks,
-Samuka97 :)

Edited by Samuka97, 31 October 2010 - 04:49 PM.


#2
Flying Dutchman

Flying Dutchman

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 889 posts
  • Location:::1

std::string path;

int level;

std::stringstream ss;


ss << level;


path = "gamedata/levels/level_" + ss.str() + ".map";

More info here
A conclusion is where you got tired of thinking.
#define class struct    // All is public.

#3
Samuka97

Samuka97

    Newbie

  • Members
  • PipPip
  • 15 posts
1>SFML_Test.cpp

1>.\SFML_Test.cpp(41) : error C2079: 'load_string_temp' uses undefined class 'std::basic_stringstream<_Elem,_Traits,_Alloc>'

1>        with

1>        [

1>            _Elem=char,

1>            _Traits=std::char_traits<char>,

1>            _Alloc=std::allocator<char>

1>        ]

1>.\SFML_Test.cpp(42) : warning C4552: '<<' : operator has no effect; expected operator with side-effect

1>.\SFML_Test.cpp(43) : error C2228: left of '.str' must have class/struct/union

1>        type is 'int'

1>.\SFML_Test.cpp(45) : error C2664: 'void std::basic_ifstream<_Elem,_Traits>::open(const wchar_t *,std::ios_base::openmode,int)' : cannot convert parameter 1 from 'std::string' to 'const wchar_t *'

1>        with

1>        [

1>            _Elem=char,

1>            _Traits=std::char_traits<char>

1>        ]

1>        No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
Ouch.

#4
Flying Dutchman

Flying Dutchman

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 889 posts
  • Location:::1
Did you include sstream? And did you put any value in level variable?
#include <sstream>

A conclusion is where you got tired of thinking.
#define class struct    // All is public.

#5
Samuka97

Samuka97

    Newbie

  • Members
  • PipPip
  • 15 posts
Whoops! I included string.h instead... well, putting both in I get this:
1>.\SFML_Test.cpp(73) : error C2664: 'void std::basic_ifstream<_Elem,_Traits>::open(const wchar_t *,std::ios_base::openmode,int)' : cannot convert parameter 1 from 'std::string' to 'const wchar_t *'

1>        with

1>        [

1>            _Elem=char,

1>            _Traits=std::char_traits<char>

1>        ]

1>        No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called


#6
Flying Dutchman

Flying Dutchman

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 889 posts
  • Location:::1
Are you using any non-ASCII letters?
A conclusion is where you got tired of thinking.
#define class struct    // All is public.

#7
Samuka97

Samuka97

    Newbie

  • Members
  • PipPip
  • 15 posts
I don't think so... here's the code from the start of int main to the line where I put togheder the variables:
int player_hspeed = 0;

	int player_vspeed = 0;

	int bullet_count = 0;

	int level_number = 1;

	const double pi = 3.1415;


	const int player_hspeed_max = 125;

	const int player_vspeed_max = 125;


	// Load the sprite image from a file

    sf::Image PlayerImage;

	sf::Image BulletImage;

	if (!PlayerImage.LoadFromFile("data/gfx/player1.png"))

        return EXIT_FAILURE;


	if (!BulletImage.LoadFromFile("data/gfx/bullet1.png"))

        return EXIT_FAILURE;


	ifstream LevelProperties;

	string load_string;

	stringstream load_string_temp;

	load_string_temp << level_number;

	load_string = "gamedata/levels/level_" + load_string_temp.str() + ".map";


#8
Flying Dutchman

Flying Dutchman

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 889 posts
  • Location:::1
I've no idea what's wrong, guess you'll to wait for experts or figure it out yourself. :)
A conclusion is where you got tired of thinking.
#define class struct    // All is public.

#9
Samuka97

Samuka97

    Newbie

  • Members
  • PipPip
  • 15 posts
Wow, what? Commenting out this line:
LevelProperties.open(load_string);
compiles just fine (actually that's lying, I have a little other problem in my code but that's for later)... how am I supposed to load a file then? Gee, moving to C++ and just trying something out like an actual game is not a good idea.

Edit: Fixed.
string load_string;

	stringstream load_string_temp;

	load_string_temp << level_number;

	load_string = "gamedata/levels/level_" + load_string_temp.str() + ".map";


	load_string.append(".txt");

	char *load_string_2 = (char*)load_string.c_str();


	ifstream LevelProperties;

	LevelProperties.open(load_string_2, ios::in);

	if (!LevelProperties)

		return EXIT_FAILURE;

Edited by Samuka97, 31 October 2010 - 04:48 PM.


#10
Flying Dutchman

Flying Dutchman

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 889 posts
  • Location:::1
LevelProperties.open(load_string[COLOR="red"].c_str()[/COLOR]);
This should fix the problem. fstream constructor and open method take char* instead of string.
A conclusion is where you got tired of thinking.
#define class struct    // All is public.

#11
Samuka97

Samuka97

    Newbie

  • Members
  • PipPip
  • 15 posts
Huh. I tried with str(), but I didn't know there was a c_ before it. Oh well, thanks anyways!




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users