Welcome to Dream.In.Code
Become a C++ Expert!

Join 149,957 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,348 people online right now. Registration is fast and FREE... Join Now!




problems with append

 
Reply to this topicStart new topic

problems with append

xpertpan
4 Dec, 2007 - 03:15 PM
Post #1

New D.I.C Head
*

Joined: 22 Nov, 2007
Posts: 9


My Contributions
Hi Guys, i have a question for you.

i wrote this
CODE
for(int k(s2.length()-1); k>=0; --k)
      s2An.append(s2.at(k));
      

(std::string s2, s2An;)

and it doesn't work compiler, says to me this:

146 D:\my files\TEI\D cemester\texnikes prog\texn_teu_4\v1.cpp invalid conversion from `char' to `const char*'
146 D:\my files\TEI\D cemester\texnikes prog\texn_teu_4\v1.cpp initializing argument 1 of `std::basic_string<_CharT, _Traits, _Alloc>& std::basic_string<_CharT, _Traits, _Alloc>::append(const _CharT*) [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>]'


if wrote it like this
CODE
for(int k(s2.length()-1); k>=0; --k)
       s2An+=s2.at(k);


is working without problems, what is going on here??

im working in Dev-C++ v4.9.9.2

thanx for your time
sorry about my bad english
User is offlineProfile CardPM
+Quote Post

Nayana
RE: Problems With Append
4 Dec, 2007 - 08:28 PM
Post #2

DIC Hawk - 나야나 नयन:
Group Icon

Joined: 14 Nov, 2007
Posts: 824



Thanked: 5 times
Dream Kudos: 175
My Contributions
OK, the reason for this is because the way you used append expected a string, but you were giving it a char.

What you should do is
CODE

    for(int k(s2.length()-1); k>=0; --k)
      s2An.append(1, s2.at(k));


The number 1, above is how many repetitions of the char you wanted.
So if you went s2An.append(5, 'c'); then you would get "ccccc"

The best way to find out this kind of thing is to first look it up in a reference, such as cppreference.com

Anyway, you can find an append reference here, and an at reference here

User is offlineProfile CardPM
+Quote Post

xpertpan
RE: Problems With Append
5 Dec, 2007 - 02:38 AM
Post #3

New D.I.C Head
*

Joined: 22 Nov, 2007
Posts: 9


My Contributions
QUOTE(Nayana @ 4 Dec, 2007 - 09:28 PM) *

OK, the reason for this is because the way you used append expected a string, but you were giving it a char.



thank you Nayana very mach for your reply
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 1/8/09 06:05PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month