954,499 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

printf function

Hello,
I have this code:

#include <iostream>
#include <string>

void main()
{
	 std::string a = "Sample";
	 printf("%s", a);
	 std::cin.get();
}


and it prints some I think random letters. Where i made mistake? And how should i change the code to write "Sample"?

Thanks for answers and sorry for my english

denethor
Newbie Poster
4 posts since Feb 2012
Reputation Points: 10
Solved Threads: 0
 

Hello, I have this code:

#include <iostream>
#include <string>

void main()
{
	 std::string a = "Sample";
	 printf("%s", a);
	 std::cin.get();
}

and it prints some I think random letters. Where i made mistake? And how should i change the code to write "Sample"?

Thanks for answers and sorry for my english


Any specific reason to use printf()? Because you can rather use cout.

phil_alloy
Newbie Poster
3 posts since Oct 2010
Reputation Points: 10
Solved Threads: 0
 

why not like this?

#include<iostream.h>
#include<conio.h>
void main()
{
char *a="sample";
cout>>a;
getch();
}
sukhi badhe
Newbie Poster
23 posts since Jan 2012
Reputation Points: 6
Solved Threads: 1
 

I'm pretty sure "%s" is expecting a C style string, not an STL string object, which is what a is declared to be. Use cout instead of printf() or use a.c_str() instead of a.

Mixing C and C++ I/O methods like printf() and cin in the same program isn't recommended.

And, last, but not least, main() should have return type int, not void, even if your compiler allows you to use void in this setting.

Lerner
Nearly a Posting Maven
2,382 posts since Jul 2005
Reputation Points: 739
Solved Threads: 396
 

why not like this?

#include<iostream.h>
#include<conio.h>
void main()
{
char *a="sample";
cout>>a;
getch();
}


Yes, just make the following change to your code:

cout<<a;


The operator you used is for cin!

phil_alloy
Newbie Poster
3 posts since Oct 2010
Reputation Points: 10
Solved Threads: 0
 

In my program, I am using Allegro and function allegro_draw_textf(),

al_draw_textf(font15, al_map_rgb(200, 200, 200), mouse_position_x + 15, mouse_position_y, ALLEGRO_ALIGN_LEFT, "%s", name);



and I have exactly the same problem there. And I can't use there "cout" I think.

denethor
Newbie Poster
4 posts since Feb 2012
Reputation Points: 10
Solved Threads: 0
 

Again, "%s" indicates a C style string. If a is an STL string object then you can use the c_str() method to use a as a C style string.

Lerner
Nearly a Posting Maven
2,382 posts since Jul 2005
Reputation Points: 739
Solved Threads: 396
 

typed it fast so made dat error

cout<<a;
sukhi badhe
Newbie Poster
23 posts since Jan 2012
Reputation Points: 6
Solved Threads: 1
 

Thanks very much, it works!

denethor
Newbie Poster
4 posts since Feb 2012
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: