473,385 Members | 1,256 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

cout string problem


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
3 2703

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

"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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Ingo Nolden | last post by:
Hi there, I am writing c++ for some months now. I think I know the language now, but not yet all the tricky things of stl. For a kernel which is not using mfc I am writing a serialization. For...
6
by: Omid | last post by:
Hi. I have problems when I try to redirect everything that is sent to cout to a file. I have one piece of code that works and one that does not work. The only difference is which headers I use....
3
by: Eric Lilja | last post by:
Hi, I need a function that expects a const std::string& containing the visual representation of a number in binary format. It should then output this number in groups of four bits separated by a...
6
by: Protoman | last post by:
I'm trying to print a string, and cout isn't printing it; here's my code: #include <iostream> #include <cstdlib> #include <string> #include <cctype> using namespace std; namespace
4
by: olson_ord | last post by:
Hi, I am not so new to C++, but I have not used it much. I was trying to append a string at the end of a previous string. If I just do this in a test program that is 3 lines long i.e. define the...
8
by: Martin Jørgensen | last post by:
Hi, I have a program that reads in a couple of numbers from files and then stores them with some latex-code but I'm not satisfied with the exponential output, as it takes up too many character...
19
by: Dancefire | last post by:
Hi, everyone It might be a simple question, but I really don't know the answer. char c = '1'; cout << c; The above code will only output a '1' rather than 0x31; If I use int cast, it can...
3
by: subramanian100in | last post by:
Consider the code: #include <iostream> using namespace std; int main( ) { cout << "test string "; cout.operator<<(10).operator<<(endl);
5
by: wongjoekmeu | last post by:
Dear All, I have written a small program to read in from console a user string. I wanted to be able to read in a string containing of all sorts of characters untill the user press enter. I have to...
1
by: zackp | last post by:
Hello, I am learning C++ lately. This morning, out of curiosity I constructed a simple code below, but to my surprise, when char* is used, the program fails to print almost anything. The only...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.