c++ - 如何使用if语句测试功能

标签 c++ function compiler-errors

嗨,我需要一些代码的帮助。我需要测试一个函数,但是每次尝试编译时都会出现编译器错误。这是我得到的错误:指针类型'void()()'和'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值进行比较,而不是覆盖
  • 关于c++ - 如何使用if语句测试功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33965901/

    相关文章:

    c++ - C格式说明符问题

    c++ - 为什么 numeric_limits<int>::max() > numeric_limits<int>::infinity()?

    c - 如何删除在C循环中作为函数调用的字符图像?

    c# - IntPtr.operator +(IntPtr,int)无法调用运算符或访问器

    c++ - STL 复制、对、 vector 和插入器

    c++ - 使用 CComObjects 的工厂模式

    function - 有没有办法在 R 的函数中使用两个 '...' 语句?

    javascript - setTimeout 将参数从另一个函数传递给函数

    c++ - 如何使在 Visual C++ 2010 中开发的解决方案在 Visual C++ 2012 中运行?

    java - 类路径无效标志 - Java