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

Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Oct 2009
Posts: 11
Reputation: calypso&noname is an unknown quantity at this point 
Solved Threads: 1
calypso&noname calypso&noname is offline Offline
Newbie Poster

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

 
0
  #1
Nov 2nd, 2009
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'

  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?
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 622
Reputation: jonsca will become famous soon enough jonsca will become famous soon enough 
Solved Threads: 82
Sponsor
jonsca jonsca is online now Online
Practically a Master Poster
 
0
  #2
Nov 2nd, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 11
Reputation: calypso&noname is an unknown quantity at this point 
Solved Threads: 1
calypso&noname calypso&noname is offline Offline
Newbie Poster
 
0
  #3
Nov 2nd, 2009
Originally Posted by jonsca View Post
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.
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 622
Reputation: jonsca will become famous soon enough jonsca will become famous soon enough 
Solved Threads: 82
Sponsor
jonsca jonsca is online now Online
Practically a Master Poster
 
1
  #4
Nov 2nd, 2009
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. }
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 198
Reputation: necrolin will become famous soon enough necrolin will become famous soon enough 
Solved Threads: 20
necrolin's Avatar
necrolin necrolin is offline Offline
Junior Poster
 
1
  #5
Nov 2nd, 2009
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:

  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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 11
Reputation: calypso&noname is an unknown quantity at this point 
Solved Threads: 1
calypso&noname calypso&noname is offline Offline
Newbie Poster
 
0
  #6
Nov 6th, 2009
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.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:




Views: 352 | Replies: 5
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC