Why is it so hard to display a string?

Hi,
First,let me mention that I am coming back to C++ from Java, which has a printf() method that is very easy to use compared to what I've seen from C++'s cout and printf().

I am getting very frustrated with trying to display a simple C++ string, not a C string, not a char array.

I can do it easily using cout and #include<string> as follows:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
#include <string>
using namespace std;

void main()
{
	string hello = "HELLO!";

	cout << hello << endl;

	cout << endl;
	cout << "Press any key to exit.";
	cin.get();
}


However, I find cout cumbersome and would love to use printf() instead.

I have no trouble using printf() to display an int:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <string>
using namespace std;

void main()
{
	/*
	string hello = "HELLO!";

	cout << hello << endl;
	*/

	int age = 21;

	printf("you are %d years old\n", age);

	cout << endl;
	cout << "Press any key to exit.";
	cin.get();
}


However, printf() will not display a string
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <string>
using namespace std;

void main()
{

	string hello = "HELLO!";
	/*
	cout << hello << endl;
	

	int age = 21;

	printf("you are %d years old\n", age);
	*/

	printf("%s", hello);

	cout << endl;
	cout << "Press any key to exit.";
	cin.get();
}


This display garbage.

I UNDERSTAND THAT %s IS THE SPECIFIER FOR "String of characters" AND THAT C++ APPARENTLY HAS NO SPECIFIER FOR string. I have found some examples that turn a string into a char array so that %s will work.

MY QUESTION IS: why isn't there a nice simple way to display a C++ string, using printf() and can someone show me the easiest way to do it?
Err...I would honestly just learn to get used to using the standard C++ streams (std::cout, std::cin, etc.).

But if you insist:
1
2
std::string str = "Hello";
std::printf("%s", str.c_str());
The answer to your question is that printf() is C code, std::string is C++.
Once you get used to it, it just looks so short (And it's safer overall)
1
2
3
4
5
6
7
8
#include <iostream>
int main()
{
    int age = 21;
    std::cout << "You are " << age << " years old" << std::endl;
    std::cin.get();
    return 0;
}


Also, don't store std::string unless you really need to edit them or copying them many times.
Last edited on
Thank you guys for answering so quickly. I understand what you're saying.

long double main gave me what I was looking for and I will use it unless you can tell me why using cout and cin is better or safer.
Simply put, std::cout/std::cin are C++ and printf/scanf are C.

There are also several other advantages the C++ iostreams have over the C printf/scanf.
I won't list them all, since they're covered pretty nicely here:
http://stackoverflow.com/questions/2872543/printf-vs-cout-in-c
Ok, I'm convinced lol ty :)
However, I find cout cumbersome and would love to use printf() instead.
No, don't ever think in such way. Follow what C++ itself define would be the best.
Topic archived. No new replies allowed.