简体   繁体   English

C ++数学游戏十六进制转换为小数

[英]c++ math game hex to decimal issue

I have completed the first two modes successfully of this math game which I am working on. 我已经成功完成了此数学游戏的前两种模式。 I am struggling with the Hex to decimal portion though . 在用十六进制到小数部分作斗争 The random isn't as random as I would like it to be. 随机性不如我希望的那样随机。 I want the Hex code to cout more random letters and in different spots: instead of just one letter in the same place. 我希望十六进制代码可以在不同位置引用更多随机字母:而不是在同一位置仅包含一个字母。 Also I would like to keep the amount of character output to a minimum of 7. 另外,我想将字符输出的数量保持在最少7个。

Could someone explain to me the flaw in my code, give me a working example, or some guidance in the right direction? 有人可以向我解释我的代码中的缺陷,给我一个有效的例子,或在正确的方向上提供一些指导吗?

Please help me stackoverflow, 请帮助我stackoverflow,

your my only hope! 您是我唯一的希望!

Here is a picture of the game so far, 这是到目前为止的游戏图片,

在此处输入图片说明

The following is the portion of the code of which I'm having an issue with. 以下是我遇到问题的部分代码。 It's not fancy with classes or anything, just a simple function prototype as I'm still fairly new to c++ and programming. 它不喜欢类或任何东西,只是一个简单的函数原型,因为我对C ++和编程还很陌生。

void gamethree()
{

    float secs;
    secs = 33;
    clock_t delay = secs * CLOCKS_PER_SEC;              
    clock_t start = clock();
    int correct = 0;                                    

while (clock() - start < delay )
{
    srand(time(NULL));

    int hexdigits[15] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15};
    char hexletters[7] = {'a', 'b', 'c', 'd', 'e', 'f'};
    //
    //enum letters{

    //  hexa = 10, 
    //  hexb = 11, 
    //  hexc = 12, 
    //  hexd = 13, 
    //  hexe = 14, 
    //  hexf = 15
    //};
    //hexa == hexletters[0];
    //hexb == hexletters[1];
    //hexc == hexletters[2];
    //hexd == hexletters[3];
    //hexe == hexletters[4];
    //hexf == hexletters[5];

    int randindex = rand() % 14;
    int randindexx = rand() % 6;
    cout << hexdigits[randindex];
    cout << hexletters[randindexx];


    int hexf[4] = {
        hexdigits[randindexx], 
        hexdigits[randindex], 
        hexletters[randindexx], 
        hexdigits[randindex],
    };


    random_shuffle(begin(hexf), end(hexf));

    for(int hi = 0; hi < 4; ++hi)
        cout << hexf[hi];
        char hexy = 0;
    cin >> hexy;

    //int hexy = 0;
    //cin >> hexy;



    ////int c = (a * b);
    //int d = 0;
    //char choice = 0;

    //cout <<"What does " << a << " * " << b << " equal?" << endl << endl << endl;
    //cout << "\n";

    //do
    //{
    //  cout << "Please enter a number: ";
    //  cin >> d;       
    //  if(d == c)
    //       ++correct;
    //}while(d != c);

    //cout << "\n\nCorrect! " << (a * b) << " is the answer!" << endl << endl;  
}
    cout << "Your score is: " << correct << "\n\n" <<endl;
cout << "Would you like to play again (Y) or (N)?\n\n\n";
}

void hextodec()
{
        float secs;
        secs = 1;
        clock_t delay = secs * CLOCKS_PER_SEC;              
        clock_t start = clock();
        while (clock() - start < delay )
            ;


        char choice = 'z';
        gamethree();
        while(choice != 'n')
        {

        cin >> choice;

        if (choice == 'y')
        {
            cout << "\n\n";
            game();
        }
        else
            choice = 'n';

    }


}

What I would do if this was my problem would be to have one one array full of all available characters then have a function to generate a string of max length from that, something like this. 如果这是我的问题,我将要做的就是让一个数组充满所有可用字符,然后具有一个从中生成最大长度的字符串的函数,类似这样。

std::string rand_string(const char* possible, int options, int max_length){
    std::string result; //we're using strings just so we don't have to pass a char*
    int length = rand()%max_length+1; //You may want to try to biase this towards bigger values or just set it at some value

    for (int i=0; i<length; ++i) {
        result[i]=possible[rand()%options]; //simple really
    }

    return result;
}

Basically just put all you characters in one array and loop through that. 基本上只是将所有字符放在一个数组中并循环遍历。

Couple side notes. 几个旁注。 Why does your hexdigits array go up to 15. 15 is not a valid character in hex for the simple reason that it can't be distinguished from 1,5. 为什么您的hexdigits数组最多可以达到15。15不能作为十六进制中的有效字符,原因很简单,无法将其与1,5区分开。 Also what is this for. 这也是为了什么。 I'd hate to be helping you cheat on a assignment, though as this has not been tested and is probably obviously plagiarized were you to just use it whole sale I'm not too concerned. 我不希望能帮助您作弊,尽管这尚未经过测试,而且很显然是窃,如果您只用它整个销售,我就不会太在意。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM