[英]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
.另一方面,变量r
是int
。 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.