How do I return a string from a function? The only way I know how to use strings are character arrays, but how would you declare a function to return a character array?
How do I return a string from a function? The only way I know how to use strings are character arrays, but how would you declare a function to return a character array?
._«:¤©•®™ª°*¯*°ª™®•©¤:»_. C++ ._«:¤©•®™ª°*¯*°ª™®•©¤:»_.
yuo return a pointer to a character array or includein your code and you may use "string" as a type. e.g. string myStringCode:#include<string>
1. do I need to include any header files for string types?
2. is this considered okay in context to the information you just gave me:
Code:string convert_enemy(int enemy) { switch(enemy) { case 1: return "Rabid Wolf"; break; case 2: return "Bear"; break; etc. } }
._«:¤©•®™ª°*¯*°ª™®•©¤:»_. C++ ._«:¤©•®™ª°*¯*°ª™®•©¤:»_.
just to show you what ryan is talking about
Code:#include <iostream> #include <string> using std::string; using std::cout; using std::cin; string creturn() { string letters="hello"; return letters; } int main() { string print=creturn(); cout<<print; cin.get() return 0; } //btw: isn't the string class nice :)
Woop?
Omg! Is it legal? I mean.. is it standart?
Yeah, it is
(at least.. there isn't anything nonstandard that I see with it)
Really? Then please explain me why there is no that method in most books? And why most tutorials in internet using character arrays instead of this so-handy method?
umm i don't know the string class is really easy to use and its completly standard. Most tutorials are a bit out of date still very usefull but a little old
Woop?
Because you're reading about C, not C++ possibly?
Oh, and Overload, I think that you'll need to return like this:
//EDIT: Holy sheet is it just me, or is my user info column messed up?Code:#include <string> using namespace std; // for our purposes, you can just make this 'using std::string;', otherwise you'll have to use std::string all the time string convert_enemy(int enemy) { switch(enemy) { case 1: return string ("Rabid Wolf"); break; case 2: return string ("Bear"); break; etc. } }
Last edited by Tman; 07-29-2004 at 10:28 AM.
*** TITANIC has quit (Excess Flood)
I may have been wrong. But a lot of books specify you can make functions with any return type. I'll let someone else answer the rest
Ok, its clear.
Its not you or your column. Its forum bug :\ I have the same in previous post.Holy sheet is it just me, or is my user info column messed up?
Last edited by rockdj; 07-29-2004 at 10:34 AM.
Its not your user collum you just posted a really long comment so the thing is adjusting for it
Code://I am posting a really long comment so that my user collum will be really small it is quite entertaining :)
Woop?
You shouldn't need to return an explicit string. An implicit conversion will be made for you. I tested it both ways under the MSVC++ 6.0 environment and they both work.Originally Posted by Tman
"Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
-Christopher Hitchens
Oh okay, thanks for the clarification.Originally Posted by hk_mp5kpdw
I realise how it's "adjusting" to my post, but it's really ugly and would be nicer if it just stayed the same
*** TITANIC has quit (Excess Flood)
yea, cuz my reasoning behind was because I dont write:Originally Posted by hk_mp5kpdw
return int (0);
P.S. FYI cuz i'm just a smart ass like that, Tman you quoted the wrong person.
._«:¤©•®™ª°*¯*°ª™®•©¤:»_. C++ ._«:¤©•®™ª°*¯*°ª™®•©¤:»_.