如何在c++中获取函数的返回值
How can I get the return value of function in c++
我通常尽量不问问题,但这让我困惑了一段时间。所以我的问题是,如何在下面的代码中的"if语句"中检查字符串HomeWTD的值?
Main.cpp:
#include "Header.h"
using namespace Header;
void main()
{
Home();
if (NEED HELP HERE)
{
}
}
Header.h:
#include <iostream>
#include <string>
#include <fstream>
#include <Windows.h>
//Create A Namespace called "Header"
namespace Header
{
using namespace std;
}
//Functions
string Home()
{
string HomeWTD;
string LoginTxt = "Login";
string RegisterTxt = "Register";
string OptionsTxt = "Options";
string CreditsTxt = "Credits";
string QuitTxt = "Quit";
string HomeHeaderMsg = " Home ";
cout << HomeHeaderMsg;
cout << "----------" << endl;
cout << LoginTxt << endl;
cout << RegisterTxt << endl;
cout << OptionsTxt << endl;
cout << CreditsTxt << endl;
cout << QuitTxt << endl << endl;
getline(cin, HomeWTD);
return HomeWTD;
}
void Register()
{
string UsernameIn;
string PasswordIn;
string UsernameOut;
string PasswordOut;
getline(cin, UsernameIn);
getline(cin, PasswordIn);
ofstream UserFile;
UserFile.open(UsernameIn + ".UserSav");
UserFile << PasswordIn;
};
保存结果并稍后进行比较:
string result = Home();
if( result == "foo" ) {
...
}
或者使用内联结果:
if( Home() == "foo" ) {
...
}
#include "Header.h"
using namespace Header;
void main()
{
string answer = Home();
if (answer == "something")
{
...
}
}
使用Home()
就像只使用HomeWTD的值一样。
相关文章:
- 程序不向函数返回值
- 如何将不可移动和不可复制的函数返回值获取到数组中
- 存储函数返回值或立即使用c++
- 如何删除新分配的字符,这也是函数返回值?
- 不从函数返回值会导致段错误
- lambda 函数返回值的有效性
- 如何获取有关 Visual Studio 代码分析中未经检查的函数返回值的警告
- 存储指向函数返回值的指针
- 在 OOP 中,调用函数返回值还是直接调用值更快?
- 函数返回值的自动类型扣除
- 为循环中多次调用的函数返回值预分配内存的正确方法是什么
- 函数返回值的位运算符提升
- 从捕获 constexpr 函数返回值的变量中删除 constexpr 会删除编译时计算
- C++ ReSharper - 生成函数返回值/参数类型的变量
- std::clamp - 检测函数返回值是否绑定到 const T&
- C 递归函数返回值
- 是否可以在C 中使回调函数返回值
- C++标准是否保证函数返回值具有常量地址?
- 将函数返回值传递给引用参数会导致 GCC 中的编译错误
- 函数返回值错误