944,150 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 682
  • C++ RSS
Nov 2nd, 2009
0

Dr. MyName, or how I learned to stop worrying and love using strings in functions

Expand Post »
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'

C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <string>
  3.  
  4. using namespace std;
  5.  
  6. int MyName()
  7. {
  8. string MyName = "TK 421";
  9. return MyName;
  10. }
  11. int main ()
  12. {
  13. MyName();
  14. }

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?
Similar Threads
Reputation Points: 10
Solved Threads: 1
Newbie Poster
calypso&noname is offline Offline
11 posts
since Oct 2009
Nov 2nd, 2009
0
Re: Dr. MyName, or how I learned to stop worrying and love using strings in functions
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.
Sponsor
Featured Poster
Reputation Points: 1165
Solved Threads: 578
Quantitative Phrenologist
jonsca is online now Online
4,271 posts
since Sep 2009
Nov 2nd, 2009
0
Re: Dr. MyName, or how I learned to stop worrying and love using strings in functions
Click to Expand / Collapse  Quote originally posted by jonsca ...
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.
Reputation Points: 10
Solved Threads: 1
Newbie Poster
calypso&noname is offline Offline
11 posts
since Oct 2009
Nov 2nd, 2009
1
Re: Dr. MyName, or how I learned to stop worrying and love using strings in functions
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. }
Sponsor
Featured Poster
Reputation Points: 1165
Solved Threads: 578
Quantitative Phrenologist
jonsca is online now Online
4,271 posts
since Sep 2009
Nov 2nd, 2009
1
Re: Dr. MyName, or how I learned to stop worrying and love using strings in functions
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:

C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <string>
  3.  
  4. using namespace std;
  5.  
  6. string MyName()
  7. {
  8. return "Hubba Bubba";
  9. }
  10.  
  11. int MyID()
  12. {
  13. return 1234;
  14. }
  15.  
  16. void* MyName2()
  17. {
  18. static string name = "Hubba Bubba Jr.";
  19. void* temp = &name;
  20. return temp;
  21. }
  22.  
  23. int main()
  24. {
  25. string name1 = MyName();
  26. void* name2 = MyName2();
  27. int ID = MyID();
  28.  
  29. cout << name1 << endl;
  30. cout << *(string*)name2 << endl;
  31. cout << ID << endl;
  32.  
  33. return 0;
  34. }
Last edited by necrolin; Nov 2nd, 2009 at 10:40 pm.
Reputation Points: 105
Solved Threads: 25
Posting Whiz in Training
necrolin is offline Offline
223 posts
since Jun 2009
Nov 6th, 2009
0
Re: Dr. MyName, or how I learned to stop worrying and love using strings in functions
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.
Reputation Points: 10
Solved Threads: 1
Newbie Poster
calypso&noname is offline Offline
11 posts
since Oct 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC