By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,750 Members | 1,165 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,750 IT Pros & Developers. It's quick & easy.

cout string problem

P: n/a

Hi
could some one please look at this and tell me why it is printing out
what it is.
all what I am trying to do is print out 105 of "-".

std::string s = string("-", 105 );
std::cout << s << std::endl;

out:
-%d-%d-%d %d:%d:%d8%G���%@X&%G�� %@x>%G���%@R%Gï¿½ï¿½ï¿ ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½%@zPL %G�%@
thanks
Feb 19 '07 #1
Share this Question
Share on Google+
3 Replies


P: n/a

I always thought that the std::string constructor looked like this:

string(size_type n, const T& t)

this would mean, you should do this instead:

// note, size goes first and
// const T is char not a string (ie. not "-")
std::string s( 105, '-' );

Hope that works for you.

Gary Wessle wrote:
Hi
could some one please look at this and tell me why it is printing out
what it is.
all what I am trying to do is print out 105 of "-".

std::string s = string("-", 105 );
std::cout << s << std::endl;

out:
-%d-%d-%d %d:%d:%d8%G���%@X&%G�� %@x>%G���%@R%Gï¿½ï¿½ï¿ ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½%@zPL %G�%@
thanks
Feb 19 '07 #2

P: n/a

"Gary Wessle" <ph****@yahoo.comwrote in message
news:m3************@localhost.localdomain...
>
Hi
could some one please look at this and tell me why it is printing out
what it is.
all what I am trying to do is print out 105 of "-".

std::string s = string("-", 105 );
std::cout << s << std::endl;

out:
-%d-%d-%d
%d:%d:%d8%G���%@X&%G��%@x>%G�� %@R%G����������%@zPL%G %@
thanks
#include <iostream>
#include <string>

int main()
{
std::cout << std::string(105, '-') << '\n';
return 0;
}

-Mike
Feb 19 '07 #3

P: n/a
On Feb 19, 2:20 pm, Gary Wessle <phd...@yahoo.comwrote:
>
std::string s = string("-", 105 );
std::cout << s << std::endl;
This creates a string of length 105, using the character
pointed to by the pointer in the first parameter, and
the 104 subsequent characters in memory.
out:
-%d-%d-%d %d:%d:%d8%G���%@X&%G��%@x>%G�� %@R%G����������%@zPL%G %@
Looks about right :)
Feb 19 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.