how to return char[] back to main

Hi.I'm trying to teach myself c++. Lately I've been practicing functions, and trying to return values back to main. I finally figured out how to do that with int type variables, but having trouble returning char[].

There are no compiler errors. When my program runs and you type in your name, it is blank where your name should be. The rest of the program works well. Below is my code....

#include <iostream>

using namespace std;

char yourName();
int yourNumber1();
int yourNumber2();

int main()
{
int a,b;
char e;

cout <<"Enter your full name:\n";
e = yourName();
cout << "Hi, " << e << ".\n\n";

cout << "Enter first number:\n";
a = yourNumber1();
cout << a << endl << endl;

cout << "Now enter second number:\n";
b = yourNumber2();
cout << b << endl << endl;

cout << a << " plus " << b << " is ";
cout << a + b;
cout << endl << endl;

cout << "Press ENTER to quit.";

cin.ignore();
cin.get();

return 0;
}

char yourName()
{
char g [256];
cin.getline(g,256);
return g[256];
}

int yourNumber1()
{
int c;
cin >> c;
return c;
}

int yourNumber2()
{
int d;
cin >> d;
return d;
}

I have also tried changing lines 12, 15, & 16 to e[256], but the result is the same. What am I doing wrong or forgetting? Any help would be appreciated as I'm still tryping to figure out functions, and programming. Thank you!

char e;

cout <<"Enter your full name:\n";
e = yourName();

..

char yourName()
{
char g [256];
cin.getline(g,256);
return g[256];
}


I don't understand your intention. You return g[256] which is actually char[] but you declare it as char yourName(). Shouldn't it be char[] yourName() ?

And then in your calling code, it should be char[] e = getName();
Thank you for the response. Since I'm new at programming I can use all of the help I can. =)

My intention is to get used to functions, defining functions, calling functions, & returning variables back to the main function. Returning char is more difficult than returning int; that's something I will have to work on. I will study the information that you have given me. Thanks again.
If you want, you may want to consider using C++ string datatype instead of char[]. It make life so much easier for you.

using namespace std;

string e = yourName();

string yourName()
{
char g[256];
cin.getline(g,256);
return string(const_cast<char*>(g), 256);
}
Hmmm....that is beyond my noob knowledge at this time. The reason I avoided the simple string is because once you hit space, that string stops. In my program, I wanted both the first and last name inputted. So I just tried it the only way I knew how. =)
If you take a look, I still uses char[] as in cin.getline(g, 256) is not changed at all. What I changed is the return statement, I uses the C++ string to return out to outside.
I appreciate all your help, but this last solution did not work. I changed that line of code for the return, but got an "cannot convert 'std::string' to 'char' in return" error. I even included the header #include <string> just in case.

No problem. At least I still learned a couple of things today. One day I'll wake up and a bunch of light-bulbs will turn on. (can't wait for that day!)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <string>

using namespace std;

string yourName() {
  char g[256];
  cin.getline(g,256);
  return string(const_cast<char*>(g));
}

int main()
{
cout <<"Enter your full name:\n";
string e = yourName();
cout << "Hi, " << e << ".\n\n";
}


Above is compiled using Linux Red Hat 3.4.6-10 g++ and it works as expected.

PS I remove 256 argument in the string constructor to ensure only valid characters get constructed inside the string object.
Topic archived. No new replies allowed.