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!

wkl
  • 77,184
  • 16
  • 165
  • 176
LeMartin
  • 13
  • 5
  • 8
    Use `+` to concatenate `std::string`, not `,` – Jarod42 Aug 03 '15 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](http://en.cppreference.com/w/cpp/language/operator_other), not to be mistaken with commas in function calls/definitions or in initializer lists though). – Caninonos Aug 03 '15 at 15:44
  • Could these s****ing robo upvoters please stop to upvote each and every bad 1 rep user question here! – πάντα ῥεῖ Aug 03 '15 at 15:54
  • `#include "../../std_lib_facilities.h"` looks very wrong. – Christian Hackl Aug 03 '15 at 16:05

3 Answers3

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;
yizzlez
  • 8,757
  • 4
  • 29
  • 44
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;
Community
  • 1
  • 1
NathanOliver
  • 171,901
  • 28
  • 288
  • 402
2

In

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

you print the address of the function.

you want

cout << "Is " << LeMenu(course1, course2) << " ok?\n"; 
Jarod42
  • 203,559
  • 14
  • 181
  • 302