int main的返回值

时间:2013-11-13 20:21:40

标签: c++ return return-value

#include<iostream>
using namespace std;

bool running = 1;

void newGameFunc();

void titleFunc();

int userInput = 0;
int playerInfo[2]; //player info, change variable to number of options for the player
int main() {

void titleFunc(); {
    cout << "\t\t\t\t game dialogue \n\n\n";

    cout <<  "\game dialogue";

    cin >> userInput;

    if (userInput == 1) {
        newGameFunc();
    }
    else {
        running = 0;
    }
        return 0;
}

titleFunc();

return 0;

void newGameFunc();{
    cout << "game dialogue \n";
    cout << "game dialogue.\n";
    cout << "game dialogue";
    cin >> userInput;
    playerInfo[0] = userInput;

    cout << "game dialogue\n";
    cin >> userInput;
    playerInfo[1] = userInput;
    return;  <--- the problem
}

while (running) {
}

return 0;
}

我昨天开始用c ++编程,运行在线教程。这是我到目前为止,但返回值有问题。它说'main':函数必须返回一个值。我指出了代码中的问题。我之前提出了一个值,它会出现更多错误。可以sombody请帮帮我?

2 个答案:

答案 0 :(得分:1)

您在代码中犯了很多语法错误。我建议不要做在线教程,然后阅读像Koenig这样的Accelerated C ++这样的书。否则,如果您无法访问本书,请查看此主题中的书籍:The Definitive C++ Book Guide and List

答案 1 :(得分:0)

我建议您按照这个tutorial on cplusplus.com这样的好教程进行操作。 此代码将编译并运行,但很可能它不会按预期执行。例如,

while (running) {
}

此代码永远不会停止,因为运行的变量永远不会设置为0 / false。它应该在循环中设置为0。以下代码现在不是很实用,但它编译:)

#include<iostream>

using namespace std;

bool running = 1;

void newGameFunc();

void titleFunc();

int userInput = 0;
int playerInfo[2]; // player info, change variable to number of options for the player

int main() {
    titleFunc(); // You call/execute the function titlefunc
    return 0; // and after that return from the main function
              // this ends your program/game
}

void titleFunc() {
    cout << "\t\t\t\t game dialogue \n\n\n";

    cout <<  "game dialogue";

    cin >> userInput;

    if (userInput == 1) {
        newGameFunc();
    } else {
        running = 0;
    }
    return; // A void function does not return anything
}

void newGameFunc() {
    cout << "game dialogue \n";
    cout << "game dialogue.\n";
    cout << "game dialogue";
    cin >> userInput;
    playerInfo[0] = userInput;

    cout << "game dialogue\n";
    cin >> userInput;
    playerInfo[1] = userInput;
    while (running) {
        cin >> userInput;
        if(userInput==0) {
            running = 0;
        }
        // Other code that should be executed while the game loop is active
    }
    return;
}

祝你的游戏好运!以及你作为程序员的生活:)