首页 > 解决方案 > 多次调用一个函数,但它只打印一次

问题描述

我正在运行以下代码:

#include <iostream>

using namespace std;

string response(bool isMale, bool isTall)
{
    if (isMale && isTall) {
        cout << "MALE AND TALL" << endl;
    }
    else if (isMale || isTall) {
        cout << "MALE OR NOT TALL" << endl;
    }
    else {
        cout << "ELSE" << endl;
    }
}

int main()
{

    response(true, true);
    response(false, true);
    response(false, false);

    return 0;
}

输出如下:

MALE AND TALL

Process returned -1073740940 (0xC0000374)   execution time : 1.460 s
Press any key to continue.

为什么没有输出?:

MALE AND TALL

MALE OR NOT TALL

ELSE

另一篇论坛帖子暗示未重置全局值。我真的不知道该怎么做。

我将不胜感激任何帮助

标签: c++functionoutput

解决方案


void response(bool isMale, bool isTall){
    if (isMale && isTall) {
        cout << "MALE AND TALL" << endl;
    }
    else if (isMale || isTall ){
        cout << "MALE OR NOT TALL" << endl;
    }
    else {
        cout << "ELSE" << endl;
    }
}

您需要将函数返回类型“string”更改为“void”。


推荐阅读