-2
#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':函数必须返回一个值。我指出了代码中的问题。我之前输入了一个值,它会出现更多错误。有人可以帮帮我吗?

4

2 回答 2

1

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

于 2013-11-13T20:52:19.103 回答
0

我建议在 cplusplus.com 上遵循一个不错的教程,例如本教程。此代码将编译并运行,但很可能它不会按照您的预期执行。例如,

while (running) {
}

这段代码永远不会停止,因为变量 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;
}

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

于 2013-11-13T21:19:34.413 回答