C++:STRING函数返回十六进制值而不是字符串

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

本文关键字:字符串 十六进制 STRING 函数 返回 C++      更新时间:2023-10-16

在我学习C++的第二个月里,我做到了:STRING类型的功能,用于从两个用户输入的菜肴中建立和返回菜单(在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();
}

但它总是返回十六进制值,我不知道为什么:(在VisualStudio2013中编译并运行(

Is 012D15CD ok? 

而不是JamEggs可以吗?(举个例子(

据我所知,我不明白为什么,我的课本甚至没有暗示这是一个可能的问题,我在互联网上也找不到任何暗示!。这不仅仅是解决问题的一种方法,如果能理解这是否是预期的mssbehavior,那就太好了。谢谢大家!

您正在打印LeMenu的函数地址。试试这个:

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

请注意,你所返回的可能不是你想要的:

return meal, dessert; //Only returns dessert

你可能想要:

return meal + dessert;
cout << "Is " << LeMenu << " ok?n"; 

正在打印函数LeMenu()的地址,而不是返回的字符串。要打印返回的字符串,您需要调用以下函数:

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

还有

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

不会返回连接字符串。它使用逗号运算符,并且只返回字符串dessert。您需要包含<string>标头,然后可以使用类似的+运算符

return meal + dessert;

在中

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

打印函数的地址。

你想要

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