显示一个大的负数(C ++)

时间:2015-04-06 17:53:50

标签: c++

我试图用c ++制作扑克,但当我试图摆脱一张牌时,它显示了值-858993460,然后是一个奇怪的符号。虽然,每次都不会发生这种情况。我想知道为什么这会显示为这个值。这是代码:

int ridcard1 = 0;
int ridcard2 = 0;
int ridcard3 = 0;
int ridcard4 = 0;
int ridcard5 = 0;

    cout << "These are your cards. Which would you like to get rid of? Enter a card number.\n" << "WHEN YOU ARE DONE, ENTER CARD 0\n";
    cout << "\n >> Card Number: ";
    cin >> ridcard1;
    if (ridcard1 != 0){
        cout << "\n >> Card Number: ";
        cin >> ridcard2;
        if (ridcard2 != 0){
            cout << "\n >> Card Number: ";
            cin >> ridcard3;
            if (ridcard3 != 0){
                cout << "\n >> Card Number: ";
                cin >> ridcard4;
                if (ridcard4 != 0){
                    cout << "\n >> Card Number: ";
                    cin >> ridcard5;
                }
                else{
                    cout << "\n";
                }
            }
            else {
                cout << "\n";
            }
        }
        else {
            cout << "\n";
        }
    }
    else {
        cout << "\n";
    }



    if (ridcard1 == 1 || ridcard2 == 1 || ridcard3 == 1 || ridcard4 == 1 || ridcard5 == 1){
        ucard1_rand = rand() % 52;
        ucard1 = ucard1_rand[cards];
        usuitrand1 = rand() % 4;
        usuit1 = usuitrand1[suits];
    }
        else{
            cout << "";
        }
    if (ridcard1 == 2 || ridcard2 == 2 || ridcard3 == 2 || ridcard4 == 2 || ridcard5 == 2){
        ucard2_rand = rand() % 52;
        ucard2 = ucard2_rand[cards];
        usuitrand2 = rand() % 4;
        usuit2 = usuitrand2[suits];
    }
        else{
            cout << "";
        }
    if (ridcard1 == 3 || ridcard2 == 3 || ridcard3 == 3 || ridcard4 == 3 || ridcard5 == 3){
        ucard3_rand = rand() % 52;
        ucard3 = ucard3_rand[cards];
        usuitrand3 = rand() % 4;
        usuit3 = usuitrand3[suits];
        }
        else{
            cout << "";
        }
    if (ridcard1 == 4 || ridcard2 == 4 || ridcard3 == 4 || ridcard4 == 4 || ridcard5 == 4){
        ucard4_rand = rand() % 52;
        ucard4 = ucard4_rand[cards];
        usuitrand4 = rand() % 4;
        usuit4 = usuitrand4[suits];
        }
        else{
            cout << "";
        }
    if (ridcard1 == 5 || ridcard2 == 5 || ridcard3 == 5 || ridcard4 == 5 || ridcard5 == 5){
        ucard5_rand = rand() % 52;
        ucard5 = ucard5_rand[cards];
        usuitrand5 = rand() % 4;
        usuit5 = usuitrand5[suits];
        }
        else{
            cout << "";
        }

1 个答案:

答案 0 :(得分:1)

错误地在变量中存储大的负数的两个常见原因是:

  1. Uninitialized variables(如上所述,其中声明但未初始化的变量以任意值开头)
  2. Integer overflow,尝试为变量存储过大的值会导致其“翻转”而是存储较大的负数
  3. 如果没有真正看到程序中的一些示例输入/输出,很难给出更具体的建议,特别是因为上面链接的代码没有(不应该,不管怎样!)实际打印出任何与卡相关的变量的内容

    您可以粘贴程序的示例执行,包括您提供的输入值以及程序的任何输出吗?