簡體   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

我收到此錯誤 [錯誤] 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 :)");
    }
}

問題是上面代碼中的變量i是一個int數組,由於類型衰減衰減指向int的指針 另一方面,變量rint 所以當你寫:

while (i != r)

這意味着您正在嘗試將指向int int比較,因此出現上述錯誤。

解決此問題,您可以使用以下程序:


#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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM