操作数类型不兼容(" double *"和" int")

时间:2016-11-03 02:03:14

标签: c++ int double operands

从我的代码中获取此错误。我在这里看过类似的问题,但是我不知道为什么我的工作不起作用(大多数答案涉及字符和人们使用"而不是',这不是' t对我很有帮助)。不知道我在这里失踪了什么。编辑:为了更具体一点,">"在我的if语句中签名给我带来麻烦。我的代码如下:

    int main()
{
int numGrades = 0;
double *scores = nullptr;
double total = 0;
cout << fixed << setprecision(1);

cout << "How many grades would you like to enter? ";
cin >> numGrades;
if (numGrades < 0)
{
    cout << "Error, please enter positive values only: ";
    cin >> numGrades;
}
scores = new double[numGrades];
for (int x = 0; x <= numGrades; x++)
{
    cout << "Please enter student " << (x + 1) << "'s score: ";
    cin >> scores[numGrades];
    if (scores < 0 && scores > 100)     //ERROR IS HERE
    {
        cout << "Please enter a value between 0 and 100: ";
        cin >> scores[numGrades];
    }
}

2 个答案:

答案 0 :(得分:1)

cin >> scores[numGrades];正在分配内存之外分配数据;之后的任何事情都是未定义的行为。

您的意思是cin >> scores[x];吗?

答案 1 :(得分:0)

if (scores < 0 && scores > 100)     //ERROR IS HERE
{
    cout << "Please enter a value between 0 and 100: ";
    cin >> scores[numGrades];
}

这就是你写的。但是,数字不能小于0且不能大于100,因此这是您应采用的解决方法。

if (scores < 0 || scores > 100)
{
    cout << "Please enter a value between 0 and 100: ";
    cin >> scores[numGrades];
}
相关问题