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?
Printable View
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?
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.
}
}
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 :)
Omg! Is it legal? :D 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
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.
}
}
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.Quote:
Holy sheet is it just me, or is my user info column messed up?
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 :)
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.Quote:
Originally Posted by Tman
Oh okay, thanks for the clarification.Quote:
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 :rolleyes:
yea, cuz my reasoning behind was because I dont write:Quote:
Originally Posted by hk_mp5kpdw
return int (0);
:p
P.S. FYI cuz i'm just a smart ass like that, Tman you quoted the wrong person. :rolleyes: