Jump to content
  • Advertisement
Sign in to follow this  
cdxrd

Solved - Problems converting std::string to char

This topic is 4509 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Using allegro and c++ Im having a problem here. I have my high score list saved as highscores.dat, where line 1 is the name, line 2 is the score, etc. I have the following function to read in and display the list, however allegro's text out routines seem to only take a char* instead of a std::string. I have been trying to figure out how to convert my std::string to a char* like allegro wants so I can display it, but it keeps erroring out saying it cant convert it. It seems like like c_str() would be my best bet, but it keeps failing. Allegro's textout routines are in the following format:
void textout_ex(BITMAP *bmp, const FONT *f, const char *s, int x, int y, int color, int bg); 
void textprintf_ex(BITMAP *bmp, const FONT *f, int x, int y, int color, int bg, const char *fmt, ...);




Any advice on how I can do this?
struct filestruct
{
	std::string setting;
	int value;
};

std::istream& operator >> (std::istream& str_in, filestruct& d)
{
	str_in >> d.setting;
	str_in >> d.value;
	return (str_in);
}

void game_highscores()
{
	// game high scores

	char filename[20] = "highscores.dat";
	struct filestruct readintext;
	std::vector<filestruct> hslist;

	std::string playername;
	int score;

	int mode = (std::ios::in | std::ios::binary);

	std::fstream fin(filename, mode);
	if(!fin)
	{
		// error out code here
	}
	while (fin >> readintext)
	{
                hslist.push_back(readintext);
	}
	fin.close();

	int exit_hs = 0;
	while (exit_hs == 0)
	{
		blit(menu, buffer, 0,0,0,0,800,600);

		int starty = 40;

		for(int i=0; i<hslist.size(); i++)
		{
			playername = hslist.setting;
			score = hslist.value;
			textprintf(buffer, gk_arial, 446,starty,BLACK, "%i) Longestnamepossible_ ", i+1);
				//textout_ex(screen, font, plname, 466, starty, BLACK, -1);
			textprintf(buffer, gk_arial, 700,starty,BLACK, "%i", score);
			starty += 30;
		}


		draw_sprite(buffer, backbutton, 730, 555);//Draw the picture to the buffer 
		draw_sprite(buffer, mouse_cursor, mouse_x, mouse_y);//Draw the picture to the buffer 
		blit(buffer, screen, 0,0,0,0,800,600);//Draw the buffer to the screen


		if((mouse_x > 730) && (mouse_x < 773) && (mouse_y > 555) && (mouse_y < 575) && (mouse_b & 1))
		{
			exit_hs = 1;
		}
	}
}




I've tried using something like this: char plname[20]; plname = playername.c_str(); But it isnt working for me either... [Edited by - cdxrd on May 23, 2006 1:01:20 PM]

Share this post


Link to post
Share on other sites
Advertisement
Quote:
Original post by cdxrd
UI've tried using something like this:

char plname[20];
plname = playername.c_str();

But it isnt working for me either...

Well, that will never work. You can't just assign one array to another; you have to copy it element by element.

Anyway, I'm 99% certain Allegro functions that take a char* accept c_str(). What's the error you're getting?

.... and why does your topic say "Solved"? That'll cut into your replies ...

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!