can't printf string variable

cout works fine with string array. Why not printf?? How to fix that? Thanks a lot.

1
2
3
4
5
6
const string whatday[]={"Sunday","Monday","Tueaday","Wednesday","Thursday","Friday","Saturday"};

for(int i=0;i<7;i++){
		//cout<<whatday[i]<<month<<"/"<<day<<"/"<<year<<endl;
		//printf("%s ",whatday[i]);
		printf("%s %d/%d/%d\n",whatday[i],month,day,year);

You can't use printf() to print a std::string because this C function doesn't know how to print a C++ string. I suggest you stick with the C++ streams when dealing with std::string. Optionally, but not recommended, you can convert the C++ string to a c_str then print that c_str with the printf.


Topic archived. No new replies allowed.