【问题标题】:Issue compiling program in CodeBlocks在 CodeBlocks 中发布编译程序
【发布时间】:2013-09-04 06:07:51
【问题描述】:

我已经成功编译/运行了其他程序,但是当我尝试编译/运行下面的代码时,它告诉我:“这个项目还没有构建,你想现在构建它吗?”。

当我从函数 getWinnings() 中取出 int b 时,它将编译/运行,但如果我添加更多参数,code::blocks 会给我上面的消息。这是什么原因造成的?

    #include <iostream>
    #include <ctime>
    #include <cstdlib>
    #include <string>

    int gameCoins;

    using namespace std;

    void instructions();
    void playSlots();
    void spin();

    int main()
    {
    srand(time(NULL));
    instructions();
    playSlots();
    }

    void instructions()
    {
    cout << "\t\t" << "VIRTUAL SLOT MACHINE" << "\n\n";
    cout << "INSTRUCTIONS: " << endl << "You start with 500 coins. The game is over either when you run out of coins or you quit the game." << "\n";
    cout << "It will cost you 25 coins per spin. Good luck!" << "\n\n" ;
    }
    void playSlots()
    {
    string user_input;
    string replay_or_quit;

    while (replay_or_quit != "n" && replay_or_quit != "no")
    {
    gameCoins = 500;
    cout << "Coins: " << gameCoins << "\n";
    while (user_input != "spin" && user_input != "Spin")
    {
        cout << "Spin the wheels to start playing. Type 'spin' to begin" << "\n";
        cin >> user_input; cout << "\n\n";
    }
    spin();
    while (gameCoins > 0 && user_input != "no" && user_input != "n")
    {
        cout << "Spin again?" << "\n" << "Decision: ";
        cin >> user_input;
        if (user_input == "y" || user_input == "yes")
        spin();
    }
    if (gameCoins == 0)
    {
        cout << "You went broke. Play again?" << "\n" << "Decision: ";
        cin >> replay_or_quit;
    }
    else
    {
        cout << "Congratulations, you won " << gameCoins << " coins. Play again?" << "\n" << "Decision: ";
        cin >> replay_or_quit;
    }
}
}

    int getWinnings(int a, int b)
    {
    return (a+b);
    }
    void spin()
    {
int wheelOne_Top, wheelOne_Mid, wheelOne_Bot;
int wheelTwo_Top, wheelTwo_Mid, wheelTwo_Bot;
int wheelThree_Top, wheelThree_Mid, wheelThree_Bot;
int coinsWon;

gameCoins = gameCoins-25;
wheelOne_Top = (rand() % 3) + 1, wheelTwo_Top = (rand() % 3) + 1, wheelThree_Top = (rand() % 3) + 1;
wheelOne_Mid = (rand() % 3) + 1, wheelTwo_Mid = (rand() % 3) + 1, wheelThree_Mid = (rand() % 3) + 1;
wheelOne_Bot = (rand() % 3) + 1, wheelTwo_Bot = (rand() % 3) + 1, wheelThree_Bot = (rand() % 3) + 1;

while (wheelOne_Top == wheelOne_Mid || wheelOne_Top == wheelOne_Bot || wheelOne_Bot == wheelOne_Mid)
{
    wheelOne_Mid = (rand() % 3) + 1;
    wheelOne_Bot = (rand() % 3) + 1;
}
while (wheelTwo_Top == wheelTwo_Mid || wheelTwo_Top == wheelTwo_Bot || wheelTwo_Bot == wheelTwo_Mid)
{
    wheelTwo_Mid = (rand() % 3) + 1;
    wheelTwo_Bot = (rand() % 3) + 1;
}
while (wheelThree_Top == wheelThree_Mid || wheelThree_Top == wheelThree_Bot || wheelThree_Bot == wheelThree_Mid)
{
    wheelThree_Mid = (rand() % 3) + 1;
    wheelThree_Bot = (rand() % 3) + 1;
}

cout << "Coins: " << gameCoins << endl << endl;
cout << "\t\t\t" << wheelOne_Top << " " << wheelTwo_Top << " " << wheelThree_Top << "\n";
cout << "\t\t\t" << wheelOne_Mid << " " << wheelTwo_Mid << " " << wheelThree_Mid << "\n";
cout << "\t\t\t" << wheelOne_Bot << " " << wheelTwo_Bot << " " << wheelThree_Bot << "\n\n";

coinsWon = getWinnings(wheelOne_Top, wheelOne_Mid);
gameCoins = coinsWon + gameCoins;

if (gameCoins > 0)
    cout << "You won " << gameCoins << " coins!" << "\n" << "Total Coins: " << gameCoins << "\n\n";
else
    cout << "You lost 25 coins!" << "\n" << "Total coins: " << gameCoins << "\n\n";
}

【问题讨论】:

  • 当它说“这个项目还没有构建,你想现在构建它吗?”您必须点击“是”。
  • 我点击是,什么也没有。没有错误,没有程序,只是在构建日志中。就像我说的,如果我从函数 getWinnings() 中删除 int b ,它将编译,但是一旦我添加更多参数,我什么也得不到。 -------------- Build: Debug in Slot Machine (编译器: GNU GCC Compiler)--------------- mingw32-g++.exe -o " bin\Debug\Slot Machine.exe" obj\Debug\main.o 进程以状态 0(0 分 1 秒)终止 0 个错误,0 个警告(0 分 1 秒)
  • 然后你必须运行程序,按 F9 来做。
  • 我以前编译/运行过程序。这不是问题

标签: c++ codeblocks


【解决方案1】:

确保程序的最后一个实例没有仍在运行。检查您的任务管理器,如果看到 Slot Machine.exe,则强制关闭它。

【讨论】:

    【解决方案2】:

    可能是编译器的行为很有趣,也可能与环境有关。

    编译器:

    如果只是编译器出现了一些奇怪的故障导致它崩溃(这可能会阻止 Code::Blocks 打开可执行文件并让它给你该消息),那么我建议更改...

    int getWinnings (int a,int b)
    {
        return a + b;
    };
    

    ...到#define getWinnings(a,b) (a + b)

    环境:

    [我假设您使用的是 Windows。]

    我有时会在两种情况下收到该通知。

    首先是我不小心在我正在修复的头文件上点击了 Build & Run,而不是在我使用测试头文件的实际程序上。在这种情况下,如果您给源文件提供了错误的扩展名,只需将其关闭,将扩展名更改为“.c”或“.cpp”而不是“.h”,然后重新打开它。

    第二个是Code::Blocks在Windows打开它之前寻找可执行文件以在编译器写入文件后关闭文件后进行读取访问。在这种情况下,在构建它之后,如果它给你消息,只需点击“No”,然后点击运行按钮(不是 Build & Run 按钮)。

    如果这两个都不是问题,则您的文件访问可能有问题,例如您不是管理员级别的帐户,或者 Code::Blocks 没有被授予管理员级别的权限。尝试设置 Code::Blocks 以管理员级别权限打开(在 Windows 7 上,这意味着您每次打开时都必须单击弹出窗口以允许它),或者如果您不能这样做,那么也许更改您正在编译的目录。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-10-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多