Closed. This question is off-topic。它当前不接受答案。
                            
                        
                    
                
                            
                                
                
                        
                            
                        
                    
                        
                            想改善这个问题吗? Update the question,所以它是on-topic,用于堆栈溢出。
                        
                        3年前关闭。
                                                                                            
                
        
我一直在尝试做Chocolate Manager游戏(更具体地说是控制台测试),并且在我输入命令后就循环了!忽略命令值cin和幸福...我不知道幸福会发生什么,因为它甚至没有显示出来!

**Main.cpp**

// Include Space
//======
#include <iostream>
#include "timer.cpp"
#include <string>
#include "clrscr.cpp"
//======

using namespace std;

int main() {
    bool exit = false;
    int numchocolate = 15;
    double money = 25;
    string commandname;
    string commandvalue;
    string placeholder;
    double happiness;
    int cmdvl;

    while (exit == false) {
        commandname = "";  //Empties the variables so that they can be reused.
        commandvalue = ""; //
        cout << "Chocolate: " << numchocolate << "\nMoney:" << money << "\nCommand:"; //stats and initial prompt
        cin >> commandname; //Input for command name
        cout << "\nCommand value:"; //Second prompt
        cin >> commandvalue; //Input for command's value
        cmdvl = StringToNumber(&commandvalue); //Converts the string into a int. But this always fails for some reason....


        //Wait! I found the problem... wait, I don't know why it's caused so let's go on find more problems :3
        if (cmdvl == -1) {
            cout << "Something gone wrong in conversion! Exiting...";
            return 1;
        }

        if (commandname == "buy n chocolates") {
             numchocolate += buyChocolate(money, cmdvl);
        }
        else if (commandname == "eat n chocolates") {
            numchocolate -= cmdvl;
            happiness += cmdvl * 2.5;
        }
        else if (commandname == "go work n hours") {
            money += cmdvl * 2;
            happiness -= cmdvl / 3.5;
        }
        else if (commandname == "exit") {
            exit = true;
        }
        else {
            cout << "\nInvalid command! Happiness penalty!\n";
        }
        if (happiness > 101.0) {
            happiness = 101.0;
        }
        if (happiness > 1.0) {
            happiness -= 1.0;
        }
        cout << "\nNow you're " << happiness << "% happy.\n";
        cout << "Press Enter to continue to next simulation cycle.";
        placeholder = "";
        getline(cin, placeholder);
        ClearScreen();
    }

    return 0;
}


Timer.cpp(我知道,与计时器无关)

// Include Space
//======
#include <string>
#include <sstream>
//======

using namespace std;

int buyChocolate(double money, int amount) {
    if (money > amount * 3.5) {
        return amount;
    }
    else if (money == 0) {
        return 0;
    }
    else {
        double newAmont = money / 3.5;
        return (int) (newAmont);
    }
    return -1;
}

int StringToNumber ( const string * sometxt ) //Why do you always fail? :(
{
    stringstream ss(*sometxt); //A string stream declarer and initializer. Nothing much.
    int result; //Results are good :)
    return ss >> result ? result : -1; //Returns the int of the string. If it isn't int-compatible returns the error int -1.
}


clrscr.cpp

#include <windows.h>

void ClearScreen(){
    HANDLE                     hStdOut;
    CONSOLE_SCREEN_BUFFER_INFO csbi;
    DWORD                      count;
    DWORD                      cellCount;
    COORD                      homeCoords = { 0, 0 };

    hStdOut = GetStdHandle( STD_OUTPUT_HANDLE );
    if (hStdOut == INVALID_HANDLE_VALUE) return;

    /* Get the number of cells in the current buffer */
    if (!GetConsoleScreenBufferInfo( hStdOut, &csbi )) return;
    cellCount = csbi.dwSize.X *csbi.dwSize.Y;

    /* Fill the entire buffer with spaces */
    if (!FillConsoleOutputCharacter(
    hStdOut,
    (TCHAR) ' ',
    cellCount,
    homeCoords,
    &count
    )) return;

    /* Fill the entire buffer with the current colors and attributes */
    if (!FillConsoleOutputAttribute(
    hStdOut,
    csbi.wAttributes,
    cellCount,
    homeCoords,
    &count
    )) return;

    /* Move the cursor home */
    SetConsoleCursorPosition( hStdOut, homeCoords );
}


它的主要目的是增加幸福感,同时避免破产或巧克力破产。祝你好运(固定和玩耍)! :3

哦,感谢谁编写了清晰的屏幕代码,因为这不是我的,我忘记了谁编写的...我想在发布简单游戏的控制台版本之前避免版权问题,并且我不知道:(

此外,代码也已更新,但字符串到int的转换不起作用。我不认为调试它会有所帮助,因为它就像明显的四行功能一样,我已经检查了上千次!

最佳答案

我建议您使用调试器遍历代码,并设置断点以查看其确切停止位置以及所导致的错误。

顺便说一下,您在这里忘记了分号(;):

if (cmdvl == -1) {
    cout << "Something gone wrong in conversion! Exiting..." <-
    return 1;
}

关于c++ - Chocolate Game:输入循环而无需插入循环或打印游戏统计信息,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/34601277/

10-11 22:53
查看更多