| | |
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:
Solved Threads: 1
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'
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?
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)
#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?
•
•
Join Date: Oct 2009
Posts: 11
Reputation:
Solved Threads: 1
0
#3 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.
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.
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. 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)
#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; }
Last edited by necrolin; Nov 2nd, 2009 at 10:40 pm.
![]() |
Similar Threads
- Returning Strings from functions (C)
- Ending a While loop using stop (Java)
- Difference between C and C++ (C++)
- C strings in Functions? (C)
- passing strings as functions (C++)
- Help with strings/scanf in C (for newbie) (C)
- is C++ for me? (C++)
Other Threads in the C++ Forum
Views: 352 | Replies: 5
| Thread Tools | Search this Thread |
Tag cloud for C++
6 api application array arrays assignment beginner binary bitmap c++ c/c++ calculator char class classes code coding compile compiler console conversion convert count data database delete developer display dll email encryption error file forms fstream function functions game generator getline givemetehcodez graph homeworkhelper iamthwee ifstream image input int java lazy lib loop looping loops map math matrix memory multidimensional multiple newbie news node number output parameter pointer problem program programming project proxy python random read recursion recursive reference return sort sorting string strings struct template templates text tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets





