Hi friends;

I've run into a problem trying to complete the below assignment:

Write a function called myName which returns (does not cout) your full name (e.g., "Bob Smith"). Write main to call the function and display the name on the screen. NOTE: Your name should not appear anywhere other than in the function myName.

The problem, as I understand it, is that the string MyName cannot be converted into an integer, and the function must use int to define itself.

I'm between a rock and a hard place, and I keep running into error code C2440: 'return' : cannot convert from 'std::string' to 'int'

#include <iostream>
#include <string>

using namespace std;

int MyName()
{
	string MyName = "TK 421";
	return MyName;
}
int main ()
{
	MyName();
}

Also I used my skills in the internets to try to solve the problem, but the best I found was a bit of code that used a char function as a pointer to the string. Or something like that, but I didn't understand it and I'm not sure I'm allowed to do it for this problem. If it is the only way, could someone explain how it works please?

Why can't myName() return a string instead of an int? Also, the restriction on cout seems to be for your function body only.

Great post title, BTW.

Why can't myName() return a string instead of an int? Also, the restriction on cout seems to be for your function body only.

Great post title, BTW.

Hey Jonsca; thanks, glad you liked it.

I'm not sure why exactly it can't return the string. My guess is that the program tries to convert the string to an integer, since it's in an integer function. However, this doesn't work, since 1) the string is characters, not numbers, and 2) the string has a space in it.

But why does your function return an int if you want it to return a string? Was your assignment given that way? Just saying it doesn't make much sense. If you need to return a string, just change the return type of the function string myName() { etc. }

You have one of a few choices:

1. Convert your function from returning an int to returning a string
2. Convert MyName to an integer.... maybe something like MyID
3. Convert your function to return a void* and then cast it to a string...

Of these choices, option 1 is the best. You cannot return a string when you are specifically telling the program that it must return an int.

My ideas in code:

#include <iostream>
#include <string>

using namespace std;

string MyName()
{
    return "Hubba Bubba";
}

int MyID()
{
    return 1234;
}

void* MyName2()
{
    static string name = "Hubba Bubba Jr.";
    void* temp = &name;
    return temp;
}

int main()
{
    string name1 = MyName();
    void* name2 = MyName2();
    int ID = MyID();

    cout << name1 << endl;
    cout << *(string*)name2 << endl;
    cout << ID << endl;

    return 0;
}

Edited 7 Years Ago by necrolin: n/a

Thanks a lot Necrolin; I could've sworn I had tried that before without good results, but it worked perfectly. Thanks for giving me multiple options too; it's great to see different ways of doing the same thing.

This question has already been answered. Start a new discussion instead.