Let's say I have a function defined as
str_call (int x)
When I do str_call(0), I want the function to return the string
"Ninety"...
How can I write up the function? I have no experience dealing with strings.
I am using C++ and I need help with a program question dealing with string/function call)?
See below for an example of a function that returns a string. The functionality you're looking for calls, I think, for a lookup table. The example also illustrates that.
By the nature of your question, I assume you're something of a beginner in C++. Some of the things you see below may be new to you, but it's never too soon to learn. C++ offers many useful features, and you should learn to take advantage of them.
#include %26lt;iostream%26gt;
#include %26lt;string%26gt;
#include %26lt;map%26gt;
using namespace std;
typedef map%26lt;int,string%26gt; LookupTable;
typedef pair%26lt;int,string%26gt; LookupTableElement;
string translate(int,const LookupTable%26amp;);
int main(int argc, char *argv[]) {
LookupTable lookup;
// Initialize
lookup.insert(LookupTableElement(0,strin...
lookup.insert(LookupTableElement(1,strin...
// ...
lookup.insert(LookupTableElement(90,stri...
// Translate
cout %26lt;%26lt; translate(90,lookup) %26lt;%26lt; endl;
cout %26lt;%26lt; translate(99,lookup) %26lt;%26lt; endl;
return 0;
}
string translate(int x,const LookupTable%26amp; map) {
LookupTable::const_iterator i;
if ((i = map.find(x)) != map.end()) {
return i-%26gt;second;
} else {
return string("not found");
}
}
// Program output:
// ninety
// not found
Reply:I doubt you're dumb, you just haven't learned enough C++ yet. For now, you can skip over the more complicated stuff. The fundamentals of what you asked for - how to return a string from a function - are fairly simple, and clearly shown in my 'translate' function. Report It
Reply:When dealing with strings as output parameters of functions, it's best to provide the function a buffer for filling it up. For example:
bool str_call(int x, char *result, int resultSize)
{
if (x==0)
{
if (resultSize%26gt;=strlen("Ninety")) // check to see if the provided buffer is big enough
{
strcpy(result, "Ninety");
return true; // return a successfull return code
}
}
return false; // result isn't valid
}
void main()
{
char result[100] = { '\0' };
if (str_call(0, result, sizeof(result))==true) // Calling the str_call function
{
printf("%s\n", result);
}
};
// Have fun,
// S. B.
Reply:You have two options. If you only ever want to return the one string, "ninety", then you can do this:
char *str_call(int x)
{
return "ninety";
}
my_other_function()
{
printf ("%s\n", str_call(0)); // Prints "ninety"
}
This is OK, but it's a bit limited. A better option would be to have something like this:
char *str_call(int x, char *storage)
{
strcpy(storage, "ninety");
return storage;
}
my_other_function()
{
char str[20];
printf ("str_call returns %s\n", str_call(0, str)); // Prints "str_call returns ninety"
printf ("Also, str = %s\n", str); // Prints "Also, str = ninety"
}
For completeness it's better to set limits on the size of these arrays, so you'd have this:
char *str_call(int x, char *storage, int maxlen)
{
strncpy(storage, "ninety", maxlen);
return storage;
}
my_other_function()
{
const int strSize = 20;
char str[strSize];
printf ("str_call returns %s\n", str_call(0, str, strSize)); // Prints "str_call returns ninety"
printf ("Also, str = %s\n", str); // Prints "Also, str = ninety"
}
Finally, if you're using C++ then you may as well also use the benefits of STL like this:
void str_call(int x, std::string %26amp;storage)
{
storage = "ninety";
}
my_other_function()
{
std::string str;
str_call(0, str);
printf ("str = %s\n", str); // Prints "str = ninety"
}
Reply:Have a read about pointers.
Strings are arrays of characters. when passing an array, you should pass a pointer the memory address of the first element in the array.
char mystring[50]
char* StringFunction(int x){
char mystring2[50];
strcpy(mystring2, "Ninety");
return mystring2;
}
int main(){
mystring = StringFunction(0);
cout %26lt;%26lt; mystring;
return 0;
}
__
I havnt been learning C++ long, but try compiling that. I can't garuntee it is the most efficient way even if it does work, but it might give you a start
Reply:go to www.freeprogrammersheaven.com and search you can learn and will get the answers
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment