C++ Using variables in equations; error: expression must have integral or unscoped enum type & others

C++ Using variables in equations; error: expression must have integral or unscoped enum type & others

srand( 0 );
int points; // number of points
float computerNumber; // number generated by the computer
float guess; // user's guess
char quit; // What the user enters when they want to quit
int totalPoints; //the total score of all of the games played
int avgPoints; // the average score of all games played
int gamesPlayed; // how many games have been played
float rangeLow; // the lower end of the range
float rangeHigh; // the higher end of the range
points = 5;
quit = 'n';
gamesPlayed = 0;
totalPoints = 0;
while ( quit != 'q' )
{

    gamesPlayed++;
    cout << "Welcome to Guessing Game! \n";
    points = 5;
    cout << "What would you like your range to be? \n";
    cout << "Low number: \n";
    cin >> rangeLow;
    cout << "High number: \n";
    cin >> rangeHigh;
    if ( rangeLow > rangeHigh )
    {
        cout << "Please use a high number that is greater than the low number. \n";
        cout << "Low number: \n";
        cin >> rangeLow;
        cout << "High number: \n";
        cin >> rangeHigh;
    }
    else
    {
        ;
    }
    computerNumber = rand( ) % (rangeLow - rangeHigh + 1) + 10;
    cout << "Computer Number: " << computerNumber << endl;
    cout << "Points:" << points << endl;
    cout << "what is your guess? \n" << endl;
    cin >> guess;
    cout << "Your guess is: " << guess << endl;

当我输入此代码(以及不影响这些行的其他无错误代码行)时,它不会编译并输出两条错误消息 - "expression must have integral or unscoped enum type" 和“'%' is非法,右操作数的类型为 'float'"

我感觉这与在我的方程中使用变量有关,但这应该不是问题吧?所有的变量类型都是与这个方程有关的浮点数,我很困惑。

因此,错误是 % 运算符不能用于浮点值,它只能用于整数 (int) 数据类型。

您需要将 float 类型转换为 int,一种方法是使用 static_cast<int>(yourFloatNumber) 以便您的计算代码行如下所示:

computerNumber = rand( ) % (static_cast<int>(rangeLow) - static_cast<int>(rangeHigh + 1)) + 10;

The modulus operator (%) has a stricter requirement in that its operands must be of integral type.

@NathanOliver 在评论中提出的另一个解决方案:如果您可以在整个代码中使用 int 而不是 float 作为 rangeLowrangeHigh,那么根本不需要类型转换。

Reference

% 是整数值的取模运算符。您的范围端点是浮点变量。在您的练习中,您应该将端点 computerNumberguess 设为 int 变量。

int rangeLow, rangeHigh, computerNumber, guess;

然后看看电脑的数字生成器,插入一些值试试看:

computerNumber = rand() % (rangeLow - rangeHigh + 1) + 10;
                 rand() % (   5     -    20     + 1) + 10;
                 rand() %          -14               + 10;

因此,您将拥有 rand() % -14 + 10。这看起来不正确。反转范围端点:

computerNumber = rand() % (rangeHigh - rangeLow + 1) + 10;
                 rand() % (   20     -    5     + 1) + 10;
                 rand() %            16              + 10;

这个 rand() % 16 + 10 更好,但显然会创建 [10, 10+16) 范围内的数字,或者换句话说(作为闭区间)[10, 25]。如果像这样用 +rangeLow 替换 +10

computerNumber = rand() % (rangeHigh - rangeLow + 1) + rangeLow;

你现在得到这样的范围:

[rangeLow, rangeLow + rangeHigh - rangeLow + 1)                             =>
[rangeLow, rangeHigh + 1)                          (left-closed interval)   =>
[rangeLow, rangeHigh]                              (closed interval)

这意味着 [5, 20] 我使用的示例值。

现在,看看 <random>。 标准库的这一部分包含 类 和使这一切变得更简单的函数: