我試圖用C++做撲克,但是當我試圖擺脫卡片時,它顯示值-858993460後面跟着一個奇怪的符號。雖然這不會每次都發生。我想知道爲什麼這會顯示爲這個值。下面是代碼:在變量錯誤地存儲大,負數顯示一個很大的負數(C++)
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 << "";
}
這通常表示您有一些未初始化的變量。 –
我找不到任何未初始化的變量......他們都設置。 –
'cards'在哪裏初始化?我找不到它。 –