简体   繁体   English

我是 C++ 的新手,我得到了这个错误帮助。 我很确定这是一个简单的解决方法,但我查找的答案没有意义

[英]I'm new to C++ and I got this error help. I'm pretty sure its an easy fix but the answers i looked up didn't make sense

I got this error [Error] ISO C++ forbids comparison between pointer and integer [-fpermissive] #include <stdio.h>我收到此错误 [错误] ISO C++ 禁止指针和 integer [-fpermissive] #include <stdio.h> 之间的比较

int main()
{
    int i[1];
    
    int r = 4;
    
    {
        printf("enter a number between 1-10\n");
        while (i != r);
        {
        scanf("%d,&i[0]");
        }
        printf("good job\n :)");
    }
}

The problem is that the variable i in your code above, is an array of int which decays to a pointer to an int due to type decay .问题是上面代码中的变量i是一个int数组,由于类型衰减衰减指向int的指针 On the other hand, the variable r is an int .另一方面,变量rint So when you wrote:所以当你写:

while (i != r)

this means you're trying to compare a pointer to an int with an int and hence the said error.这意味着您正在尝试将指向int int比较,因此出现上述错误。

To solve this you can use the following program:解决此问题,您可以使用以下程序:


#include <iostream>

int main()
{
    int arr[4] = {}; //create an array named arr of size 4 with elements of type int all initialized to 0
    
    //iterate through the array and take input from user 
    for(int &element : arr)
    {
        std::cout << "Enter number:" << std::endl;
        std::cin >> element; 
    }
    
    return 0;
}


暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 C ++帮助。 我在解决这个问题时遇到了问题 - C++ help. I'm having problems getting this right 在 C++ 中,我遇到了一个我无法理解的编译器错误 - In C++, I'm getting a compiler error that I can't make sense of 这行得通吗? 我正在尝试制作一个多维数组,我很确定它可以在Java中工作,但是我不了解C ++ - Would this work? I'm trying to make a multi-dimensional array, and I'm pretty sure it would work in Java, but I don't know about C++ C++ 新手,我不确定我做错了什么 - New to C++, I'm not sure what I did wrong C++ function 正在泄漏 memory,我是 ZF6F87C9FDCF8B3C9FDCF8B3C3F07F9C3F1 的新手,不确定如何修复 - C++ function is leaking memory, I'm new to C++ and not sure how to fix it C++ 游戏出现错误,我很确定指针有问题。 但我不能告诉 - Having an error with C++ game, I'm pretty sure it's something wrong with the pointers. but i cant tell (C ++)试图完成一个快速程序,但是我不确定哪里出错了? - (C++) Trying to finish up a quick program but I'm not sure where I went wrong? 我对 C++ 很陌生,而且我一直在尝试制作循环 - I'm very new to C++ and I'm stuck trying to make a loop C ++无限代码吊死了吗? 冷冻? 我不确定 - C++ Infinite Sum Code Hanging? Freezing? I'm not sure C++ 运算符重载和“新”正在工作,但我不确定为什么/如何 - C++ Operator Overloading and 'new' is working, but I'm not sure why/how
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM