C++ 如果在函数中不起作用

标签 c++ function if-statement void

我是 C++ 的新手,我正在尝试制作我自己的“cmd”。仍然收到错误:“|11|错误:带有值的返回语句,在返回‘void’的函数中 [-fpermissive]|”... 请帮助:(

代码在这里:

#include <iostream>
#include <string>

using namespace std;

void cmd(int command)
{
    if (command == login)
    {
        cout << "Prikaz login neni k dispozici!";
        return 0;
    }
    else
    {
        cout << "Prikaz ";
        cout << command;
        cout << " neni znamy!";
        return 0;
    }
}

int main()
{
    cout << "Zadejte prikaz: ";
    cin << command;
    cmd(command);
    return 0;
}

最佳答案

不知道您到底打算做什么,但下面的程序应该可以工作。您只是没有正确声明整数变量。

#include <iostream>
#include <string>

using namespace std;

int cmd(string command)
{
    if (command == "login")
    {
        cout << "Prikaz login neni k dispozici!";
        return 0;
    }
    else
    {
        cout << "Prikaz ";
        cout << command;
        cout << " neni znamy!";
        return 0;
    }
}

int main()
{
    string command; /* Define String */
    cout << "Zadejte prikaz: ";
    cin >> command; /* Input */
    cmd(command);
    return 0;
}

关于C++ 如果在函数中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34977732/

相关文章:

c++ - 如何避免重复发送消息

c++ - 将标量乘以 boost.units.quantity(自动类型转换问题?)

c++ - 如何包含 C++ 库以便 node-gyp 可以链接?

javascript - 创建涉及预定结构的新标签

c++ - 我需要帮助在 C++ 中尽可能压缩此 IF 语句

c++ - C++中的运算符重载

c - 如何拥有一个返回整数指针的函数数组

python - n 函数的乘法

if-statement - TCL 代码的 "else"命令中 "if"子句后的额外字

if-statement - 为什么一个变量与多个值的不相等检查总是返回 true?