如何在c ++中获取函数的返回值

时间:2013-07-30 00:15:33

标签: c++ login

我通常尽量不提问题,但这让我感到困惑了一段时间。所以我的问题是如何在以下代码中的“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;
};

2 个答案:

答案 0 :(得分:3)

保存结果并稍后进行比较:

string result = Home();
if( result == "foo" ) {
    ...
}

或者使用结果内联:

if( Home() == "foo" ) {
    ...
}

答案 1 :(得分:0)

#include "Header.h"

using namespace Header;

void main()
{
    string answer = Home();
    if (answer == "something")
    {
       ...
    }
}

使用Home()就像使用HomeWTD的值一样。