Hello CodeCall, I MISSED YOU ALL. My uncle got married a couple of days ago so I couldn't visit CC these days. Talked too much, back on thread.
I was learning something about functions in C++ and I wanted to try something myself. I wanted to create a function that would return a char array but I got tons of errors. Could anyone please tell me how can I do that in an other way. Use CString?
Looking forward for reply
Wish you all the best,
Dren
PS: My Daddy was about to kill me when he heard that I wanted to fire an AK on my Uncles wedding lol![]()
How about a std::string?
I would also say that a C++ string (std::string) is the best choice, especially now when you're working with C++ anyway.
Check this out.
This should give you an idea of how to pass an array to a function, and then return it. You are going to have to use pointers in order to do the job.Code:#include <iostream> #include <conio.h> using namespace std; char * Return_Phrase(char * array); int main() { char * return_value; char phrase[300]; cout << "Enter a phrase: "; cin.getline(phrase, 299); return_value = Return_Phrase(phrase); cout << endl << return_value; getch(); return 0; } char * Return_Phrase(char * array) { return array; }
And yes I used <conio.h>, because although not standardized it is still a popular library.
I will also take a direct quote from this site in one of the stickied threads for anyone who has a problem with me using it.
From the C/C++ Resources thread...So yeah, apparently it is okay to use conio.h on this site, well if you would have read stickied threads you would have known that.Is it okay to use conio.h? ...
... and questions alike regarding platform-dependent libraries are not really questions for us, but more for yourself.
Anyways hopefully this code will help you out.
Thank you all, especialy you MeTh0Dz|Reb0rn, your post helped me so much. I still dont know how to use std::string, that std:: looks so weird indeed.
Thanks again,
CodeCall #1,
Dren
Well you don't have to use it as std::string, that is only if you don't declare 'namespace std' globally..
Like:
orCode:#include <iostream> int main() { std::cout << "Hello World" << std::endl; return 0; }
orCode:#include <iostream> using std::cout; using std::endl; int main() { cout << "Hello World << endl; return 0; }
Code:#include <iostream> using namespace std; // globally declare the std or standard library int main() { cout << "Hello World" << endl; return 0; }
http://logickills.org
Science - Math - Hacking - Tech
Many dudes prefer NOT doing the "using namespace std" thing globally (that polluting namespaces etc), so it could be a nice habit to put std:: in front of those functions (cout etc) that need it, and not putting "using namespace..." on the top. To use strings, #include <cstring>, and you have a data type named "string" to use:
The string variable is an object, having its own methodz (yeah...) and stuff, like myString.length etc...Code:#include <cstring> std::string DoSomethingWithThatString(std::string str); // Function declaration std::string myDearString = ""; // Global, initialized string. Notice "std::" if you're not having "using namespace.." /* MAIN */ int main() { using namespace std; // Now this is OK, but will only affect stuff within function string mySecondString; // Local, uninitialized string. Notice, no "std::", because of "using namespace.." mySecondString = DoSomethingWithThatString(myDearString); // Yup you probably get it :D return (EXIT_SUCCESS); // Has nothing to do with strings } /* FUNCTION RETURNING STRING */ std::string DoSomethingWithThatString(std::string str) { // Do something with the string object (str) here if ya want... return str; // Returns input }
Hey! Check out my new Toyota keyboaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
I am very weak in this issue
Don't worry, there are even free e-books on the subject!
"Learn to program with C++" (John Smiley), for example... Or "Thinking in C++" 1 and 2...
Hey! Check out my new Toyota keyboaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks