LeMartin
LeMartin

Reputation: 13

C++: STRING Function returns hexadecimal value instead of string

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!

Upvotes: 0

Views: 148

Answers (3)

NathanOliver
NathanOliver

Reputation: 180500

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;

Upvotes: 2

yizzlez
yizzlez

Reputation: 8805

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;

Upvotes: 4

Jarod42
Jarod42

Reputation: 217235

In

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

you print the address of the function.

you want

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

Upvotes: 2

Related Questions