C++ 生成超出我的范围C++;(太大了)

C++ 生成超出我的范围C++;(太大了),c++,C++,为了好玩,我决定尝试制作一个简单的程序,以一种简单的方式模拟21点。基本上已经完成了,只是随机生成的数字太大了。我不在乎srand/rand的偏见(目前),我只想让它正常工作 #include <iostream> #include <cstdlib> #include <cmath> #include <ctime> using namespace std; int genRandInt (int low, int high) { i

为了好玩,我决定尝试制作一个简单的程序,以一种简单的方式模拟21点。基本上已经完成了,只是随机生成的数字太大了。我不在乎srand/rand的偏见(目前),我只想让它正常工作

#include <iostream>
#include <cstdlib>
#include <cmath>
#include <ctime>

using namespace std;

int genRandInt (int low, int high) {
    int randnum = low + (rand() % (high - low + 1));
    return randnum;
}

int main()
{
    srand(time(NULL));
    int computerScore;
    int score;
    int card;

    while (int playAgain = 1)
    {
        cout << "Enter 0 to hold or 1 to hit: ";
        int play;
        cin >> play;

        if (play == 0)
        {
             computerScore = genRandInt(1, 31);

             if (score < computerScore)
             {
                 cout << "Your score is " << score << " and the computer's score is " << computerScore << "! You lose.\n";
             }

             if (score > 21)
             {
                 cout << "Your score is " << score << " which is greater than 21. Bust!\n";
             }

             if (score > computerScore && score <= 21)
             {
                 cout << "Your score is " << score << " and the computer's score is " << computerScore << "! You win!\n";
             }

             cout << "Would you like to play again? 1 for yes, 0 for no. : ";
             cin >> playAgain;
        }


        if (play == 1)
        {
            card = genRandInt(1, 11);
            score = score + card;
            cout << "Your score is: " << score << "\n";
        }
    }



    return 0;
}
#包括
#包括
#包括
#包括
使用名称空间std;
int-genRandInt(int-low,int-high){
int randnum=low+(rand()%(高-低+1));
返回randnum;
}
int main()
{
srand(时间(空));
国际计算机评分;
智力得分;
int卡;
while(int playreach=1)
{
不能玩;
如果(播放==0)
{
计算机评分=genRandInt(1,31);
如果(分数<计算机分数)
{

您是否可以使用
int-score;
在中未初始化

if (score < computerScore) 
取决于
if(play==0)
if(play==1)
条件

它碰巧有一些垃圾作为其内存内容,编译器不会为您初始化为零。事实上,使用未初始化的变量是未定义的行为。在第一次使用之前初始化它,最好是在定义本身中

int score = 0;

另外,在(
-Wall-Wextra
for g++/clang++)上使用警告进行编译,因为编译器很容易对这些错误发出警告。

试着运行这个程序,看看是否有相同的问题。我只是添加了一些打印语句来尝试和调试它,它停止向我显示非常大的数字

     EDIT:
//ADD
    int score = 0;
//

                if (play == 1)
                {
                    cout << "printing in the PLAY = 1 "<< score << endl;
                    card = genRandInt(1, 11);
                    score = score + card;
                    cout << "Your score is: " << score << "\n";
                }
编辑:
//加
智力得分=0;
//
如果(播放==1)
{

cout
while(int playreach=1)
这不应该是
playreach==1
?一个简单的程序显示它不是随机数或你的
getRandInt
函数:啊,这肯定是“int score”问题。我在if(play==1)中打印出来了语句,我认为Xcode为我初始化了它,所以问题就消失了。好的,现在已经解决了,谢谢!你能解释一下你最后一句话的意思吗?我不知道如何为编译设置警告。@CodyHayes如果某个答案帮助解决了你的问题,你应该接受它。如果你使用
g++
,你可以像
g>一样编译你的程序++-Wall-Wextra program.cpp-o program
,即可以添加警告标志,以便编译器发出各种有用的警告,例如关于使用未初始化变量的警告。
     EDIT:
//ADD
    int score = 0;
//

                if (play == 1)
                {
                    cout << "printing in the PLAY = 1 "<< score << endl;
                    card = genRandInt(1, 11);
                    score = score + card;
                    cout << "Your score is: " << score << "\n";
                }