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 :D
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 :p
Make a function that returns Char Arrays
Started by Dren, Aug 08 2008 03:28 PM
11 replies to this topic
#1
Posted 08 August 2008 - 03:28 PM
|
|
|
#2
Posted 08 August 2008 - 07:17 PM
How about a std::string?
#3
Posted 08 August 2008 - 10:58 PM
I would also say that a C++ string (std::string) is the best choice, especially now when you're working with C++ anyway.
#4
Posted 09 August 2008 - 08:41 AM
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.
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.
Anyways hopefully this code will help you out.
#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;
}
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.
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...
Quote
Is it okay to use conio.h? ...
... and questions alike regarding platform-dependent libraries are not really questions for us, but more for yourself.
... and questions alike regarding platform-dependent libraries are not really questions for us, but more for yourself.
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.
Anyways hopefully this code will help you out.
#5
Posted 27 August 2008 - 12:27 PM
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
Thanks again,
CodeCall #1,
Dren
#6
Posted 27 August 2008 - 12:37 PM
Dren said:
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
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:
#include <iostream>
int main()
{
std::cout << "Hello World" << std::endl;
return 0;
}
or
#include <iostream>
using std::cout;
using std::endl;
int main()
{
cout << "Hello World << endl;
return 0;
}
or
#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
Science - Math - Hacking - Tech
#7
Posted 29 August 2008 - 07:56 AM
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...
#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
}
The string variable is an object, having its own methodz (yeah...) and stuff, like myString.length etc...
Hey! Check out my new Toyota keyboaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
#8
Posted 29 August 2008 - 11:08 AM
#9
Posted 29 August 2008 - 11:10 AM
directoryxd said:
I am very weak in this issue
Okay? Go read a tutorial online. Or study the code I posted above.
#10
Posted 29 August 2008 - 11:31 AM
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...
"Learn to program with C++" (John Smiley), for example... Or "Thinking in C++" 1 and 2...
Hey! Check out my new Toyota keyboaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
#11
Posted 30 August 2008 - 02:35 PM
#12
Posted 01 September 2008 - 12:17 PM
Hey, that's really not bad! Quite a lot of links there, eh....? :)
Although I prefer the learn-stuff-from-a-book-thing, this might get useful...
Although I prefer the learn-stuff-from-a-book-thing, this might get useful...
Hey! Check out my new Toyota keyboaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa


Sign In
Create Account


Back to top









