Sign in to follow this  
cdxrd

Solved - Problems converting std::string to char

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[i].setting;
			score = hslist[i].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
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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this