如何使用if语句测试函数

How do I test functions with an if statement

本文关键字:测试 函数 语句 if 何使用      更新时间:2023-10-16

嗨,我需要一些代码方面的帮助。我需要测试一个函数,但每次尝试编译时都会遇到编译器错误。我得到的是,错误:不同指针类型"void()()"answers"const-char"之间的比较缺少强制转换。这是我的密码。

#include <iostream>
using namespace std;
void getInput();
bool gameGoing = true;
int main()
{
do{
    cout << "hello world this is a test.n";
    getInput();
    if(getInput == "false")
    {
        return 0;
    }
}while(gameGoing = true);
}
void getInput()
{
string userInput;
cin >> userInput;
}

应该是

#include <iostream>
using namespace std;
string getInput();
bool gameGoing = true;
int main()
{
    do
    {
        cout << "hello world this is a test.n";
        if(getInput() == "false")
            return 0;
    } while(gameGoing == true);
}

string getInput()
{
    string userInput;
    cin >> userInput;
    return userInput;
}

我改变了什么:

  • getInput函数添加了一个返回类型,这样它的结果就不会被忽略
  • 修正了if比较,以实际将getInput函数的结果与"false"值进行比较
  • 修复了while条件以比较gameGoingtrue值,而不是覆盖它