Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

[SOLVED] Putting togheder an string and an int?

string

  • Please log in to reply
10 replies to this topic

#1 Samuka97

Samuka97

    CC Newcomer

  • Just Joined
  • PipPip
  • 13 posts

Posted 31 October 2010 - 03:01 PM

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.

  • 0

#2 Flying Dutchman

Flying Dutchman

    CC Leader

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1090 posts
  • Location:::1
  • Programming Language:C++, Python

Posted 31 October 2010 - 03:19 PM

std::string path;
int level;
std::stringstream ss;

ss << level;

path = "gamedata/levels/level_" + ss.str() + ".map";
More info here
  • 0

The roots of education are bitter, but the fruit is sweet.


#3 Samuka97

Samuka97

    CC Newcomer

  • Just Joined
  • PipPip
  • 13 posts

Posted 31 October 2010 - 03:23 PM

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.
  • 0

#4 Flying Dutchman

Flying Dutchman

    CC Leader

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1090 posts
  • Location:::1
  • Programming Language:C++, Python

Posted 31 October 2010 - 03:27 PM

Did you include sstream? And did you put any value in level variable?
#include <sstream>

  • 0

The roots of education are bitter, but the fruit is sweet.


#5 Samuka97

Samuka97

    CC Newcomer

  • Just Joined
  • PipPip
  • 13 posts

Posted 31 October 2010 - 03:28 PM

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

  • 0

#6 Flying Dutchman

Flying Dutchman

    CC Leader

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1090 posts
  • Location:::1
  • Programming Language:C++, Python

Posted 31 October 2010 - 03:31 PM

Are you using any non-ASCII letters?
  • 0

The roots of education are bitter, but the fruit is sweet.


#7 Samuka97

Samuka97

    CC Newcomer

  • Just Joined
  • PipPip
  • 13 posts

Posted 31 October 2010 - 03:32 PM

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";

  • 0

#8 Flying Dutchman

Flying Dutchman

    CC Leader

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1090 posts
  • Location:::1
  • Programming Language:C++, Python

Posted 31 October 2010 - 03:53 PM

I've no idea what's wrong, guess you'll to wait for experts or figure it out yourself. :)
  • 0

The roots of education are bitter, but the fruit is sweet.


#9 Samuka97

Samuka97

    CC Newcomer

  • Just Joined
  • PipPip
  • 13 posts

Posted 31 October 2010 - 03:59 PM

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.

  • 0

#10 Flying Dutchman

Flying Dutchman

    CC Leader

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1090 posts
  • Location:::1
  • Programming Language:C++, Python

Posted 31 October 2010 - 04:54 PM

LevelProperties.open(load_string[COLOR="red"].c_str()[/COLOR]);
This should fix the problem. fstream constructor and open method take char* instead of string.
  • 0

The roots of education are bitter, but the fruit is sweet.


#11 Samuka97

Samuka97

    CC Newcomer

  • Just Joined
  • PipPip
  • 13 posts

Posted 31 October 2010 - 05:07 PM

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





Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download