0

during my 2nd month learning C++ I got to this: STRING type function to build up and return menu from two user-input dishes (compliled and run in VisualStudio2013)

#include "../../std_lib_facilities.h"

string LeMenu(string meal, string dessert) //a F() concatenates 2 strings
{
    return meal, dessert;  //also tried meal+dessert
}                   

int main()
{
    string course1, course2;
    cout << "What is your chice today Sir?\n";
    cin >> course1 >> course2;                  //request to input meals
    LeMenu(course1,course2);
    cout << "Is " << LeMenu << " ok?\n";        //here we output
    keep_window_open();
}

But it always returns a HEXADECIMAL VALUE, and I do not know why: (compliled and run in VisualStudio2013)

Is 012D15CD ok? 

instead of Is JamEggs ok? (as an example)

From what I have learnt I do not see why, my text book does not even suggests this as a likely issue and I can not find any hint on the internet!. More than a way to solve it it would be nice to understand if this is an expected mssbehavior or not. Thank you all!

4
  • 8
    Use + to concatenate std::string, not , Commented Aug 3, 2015 at 15:40
  • To explain further, , is a legal operator, but it doesn't do what you think (and you should avoid using it if you're learning C++, it won't be useful in most cases). , will evaluate the left expression, discard its result, evaluate the right expression and evaluate to the right expression's result (Edit: see this, not to be mistaken with commas in function calls/definitions or in initializer lists though). Commented Aug 3, 2015 at 15:44
  • Could these s****ing robo upvoters please stop to upvote each and every bad 1 rep user question here! Commented Aug 3, 2015 at 15:54
  • #include "../../std_lib_facilities.h" looks very wrong. Commented Aug 3, 2015 at 16:05

3 Answers 3

4

You are printing out the function address of LeMenu. Try this instead:

cout << "Is " << LeMenu(course1, course2) << " ok?\n";  

Note that you are what you are returning is probably not what you want:

return meal, dessert; //Only returns dessert

You probably want:

return meal + dessert;
Sign up to request clarification or add additional context in comments.

Comments

2
cout << "Is " << LeMenu << " ok?\n"; 

Is printing the address of the function LeMenu() and not the returned string. To print the returned string you would need to call the function like:

cout << "Is " << LeMenu(course1,course2) << " ok?\n"; 

Also

string LeMenu(string meal, string dessert) //a F() concatenates 2 strings
{
    return meal, dessert;  //also tried meal+dessert
}

Is not going to return a concatenated string. It is using the comma operator and is only going to return the string dessert. You need to include the <string> header and then you can use the + operator like

return meal + dessert;

Comments

2

In

cout << "Is " << LeMenu << " ok?\n"; 

you print the address of the function.

you want

cout << "Is " << LeMenu(course1, course2) << " ok?\n"; 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.