Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Textprint_ex and strings

Credits go to HoHo for helping out!
This thread is locked; no one can reply to it. rss feed Print
Textprint_ex and strings
Raf Vermeulen
Member #7,166
April 2006

Hey, I'm trying to print a string using textprint_ex, but whenever I get to the line of code that does that, the program crashes. The string itself shows fine in a console window when doing cout.

The line of code I use, is this:
textprintf_ex(buffer, font, 100, y, makecol(255, 255, 255), -1, "%s", test);

Anybody knows why it crashes, and how to fix it?

Kauhiz
Member #4,798
July 2004

Let's see some more code.

---
It's Ridge Racer! RIIIIIDGE RAAAAACER!

Raf Vermeulen
Member #7,166
April 2006

1void load_highscores() {
2 int y = 50;
3 string line;
4 string test = "TEST";
5 ifstream file ("High.txt");
6 if (file.is_open()) {
7 while (!file.eof()){
8 getline (file,line);
9 cout << test << "\n";
10 textprintf_ex(buffer, font, 100, y, makecol(255, 255, 255), -1, "%s", test);
11 y += 20;
12 }
13 file.close();
14 }
15}

When I comment out the textprintf_ex command, it all works smoothly, but with that command, it crashes.

Kauhiz
Member #4,798
July 2004

That's because textprintf_ex wants a C-string, not a std::string. I'm surprised that even compiles.

---
It's Ridge Racer! RIIIIIDGE RAAAAACER!

X-G
Member #856
December 2000
avatar

It compiles because variadic functions don't typecheck "..." arguments.

--
Since 2008-Jun-18, democracy in Sweden is dead. | 悪霊退散!悪霊退散!怨霊、物の怪、困った時は ドーマン!セーマン!ドーマン!セーマン! 直ぐに呼びましょう陰陽師レッツゴー!

Kauhiz
Member #4,798
July 2004

Good to know.

---
It's Ridge Racer! RIIIIIDGE RAAAAACER!

HoHo
Member #4,534
April 2004
avatar

instead of test use test.c_str()

__________
In theory, there is no difference between theory and practice. But, in practice, there is - Jan L.A. van de Snepscheut
MMORPG's...Many Men Online Role Playing Girls - Radagar
"Is Java REALLY slower? Does STL really bloat your exes? Find out with your friendly host, HoHo, and his benchmarking machine!" - Jakub Wasilewski

Raf Vermeulen
Member #7,166
April 2006

That works like a charm, the .c_str():D Thanks for the help!

Kikaru
Member #7,616
August 2006
avatar

.c_str() returns a char array (C-style string) just like the string you used it on. Just felt the need to point that out. :)

Kitty Cat
Member #2,815
October 2002
avatar

Quote:

It compiles because variadic functions don't typecheck "..." arguments.

It does if you add the printf attribute to the function. So either MSVC doesn't have a way to check it and he's using MSVC, or he has warnings off. :P

--
"Do not meddle in the affairs of cats, for they are subtle and will pee on your computer." -- Bruce Graham

Raf Vermeulen
Member #7,166
April 2006

Warnings're turned on. Compiler's standard one in Dev-C++ (with the Allegro parameter).

Neil Black
Member #7,867
October 2006
avatar

Figuring out how to print strings got me so frustrated. I still have no idea where anyone could find out they had to add .c_str() to their string variable. I'm just glad someone on the forums knew.

GullRaDriel
Member #3,861
September 2003
avatar

Possumdude0 said:

I still have no idea where anyone could find out they had to add .c_str() to their string variable.

Google is your friend. Or your favorite porn search engine .

"Code is like shit - it only smells if it is not yours"
Allegro Wiki, full of examples and articles !!

Neil Black
Member #7,867
October 2006
avatar

GullRaDriel: wow, you've answered almost every one of my posts today!

gnolam
Member #2,030
March 2002
avatar

Quote:

I still have no idea where anyone could find out they had to add .c_str() to their string variable.

From wherever you were supposed to have learned C++.

--
Move to the Democratic People's Republic of Vivendi Universal (formerly known as Sweden) - officially democracy- and privacy-free since 2008-06-18!

Neil Black
Member #7,867
October 2006
avatar

I learned C++ from gamedev.net, and a little right here.

HoHo
Member #4,534
April 2004
avatar

Get a good free C++ book. I learnt about .c_str() from the one that should be on the first link, "thinking in C++" by Bruce Eckel.

__________
In theory, there is no difference between theory and practice. But, in practice, there is - Jan L.A. van de Snepscheut
MMORPG's...Many Men Online Role Playing Girls - Radagar
"Is Java REALLY slower? Does STL really bloat your exes? Find out with your friendly host, HoHo, and his benchmarking machine!" - Jakub Wasilewski

GullRaDriel
Member #3,861
September 2003
avatar

Possumdude0: It's because I got nothing else to do today.

I second HoHo and Gnolam thought.

"Code is like shit - it only smells if it is not yours"
Allegro Wiki, full of examples and articles !!

Go to: