Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/google-app-engine/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 我解不出数组_C++ - Fatal编程技术网

C++ 我解不出数组

C++ 我解不出数组,c++,C++,我是新来的,我正在学习编程,尤其是c++。我想做一个练习,用数组显示100到200之间的素数,但我不明白为什么会出现错误:数组下标的类型“int[int]”无效。请帮帮我 我在线尝试了devc++和编译器 #include<iostream> #include<stdlib.h> using namespace std; int main(int argc, char *argv[]){ //Define variables int i,j,temp

我是新来的,我正在学习编程,尤其是c++。我想做一个练习,用数组显示100到200之间的素数,但我不明白为什么会出现错误:数组下标的类型“int[int]”无效。请帮帮我

我在线尝试了devc++和编译器

#include<iostream>
#include<stdlib.h>

using namespace std;
int main(int argc, char *argv[]){

    //Define variables 
    int i,j,temp, accountant=0,number = 100;
    bool isPrime = true;

    //algorithm that tells us how many prime numbers there are between 100 and 200

    for(i=100; i<200;i++){
        isPrime=true;

        for(j=2; j<1-i;j++){
            if(i %j==0){
                isPrime=false;
                j=1;
            }
        }
        if(isPrime==true){
            accountant=accountant+1;
        }
    }

    //create the vector primes that have cells
    int vectorPrimes(accountant);

    //filling the vector with the prime numbers between 100 and 200
    for(i=0;i<accountant;i++){
        isPrime=true;
        for(j=2;j<number-1;j++){
            if(number % j == 0){
                isPrime=false;
                j=number;
            }
        }
        if(isPrime== true){
            vectorPrimes[i]=number;
        }
        else{
            i=1-1;
        }
        number=number + 1;
    }

    //Method of ordering the boxes of the arrangement (Major to minor)
    for(i=0;i<accountant-1;i++){
        for(j=i+1;j<accountant;j++){
            if(vectorPrimes[j]>vectorPrimes[i]){
                temp=vectorPrimes[j];
                vectorPrimes[j]=vectorPrimes[i];
                vectorPrimes[i]=temp;
            }
        }
    }

    //print the ordered arrangement
    cout<<"The prime numbers between 100 and 200 are the following: \n\n";
    for(i=0;i<accountant;i++){
        cout<<vectorPrimes[i]<<endl;
    }

    system("PAUSE");
    return 0;
}

你有一些问题

首先,你可能想

#include <vector>
不声明向量,它声明单个整数

std::vector<int> vectorPrimes(accountant);
很可能是你想要的


可能还有其他一些bug,但如果您还没有这样做,请为您选择的环境下载一个合适的IDE,并学习如何使用调试器单步执行程序。这将让您看到它没有按您希望的那样工作,因此您可以修复问题。

程序太长。尝试删除内容以显示问题的确切位置。在删除内容之前对代码进行备份。按输入错误关闭:int vectorPrimesaccountant;应该是,假设编译器和编码标准允许可变长度数组,int vectorPrimes[accounter];。更喜欢使用std::vector进行此操作。@HolyBlackCat good catch-已修复。我被M.V.P.烧了太多次了,所以我对任何看起来像函数原型的东西都有点担心。请问你推荐什么IDE?这取决于你使用的是什么操作系统。对于Windows,VisualStudio是最好的之一,社区版是免费的,供个人和教育使用。当你第一次开始使用它时,它确实有一个陡峭的学习曲线,但是坚持下去,它就会开始有意义。对于macos来说,Xcode或Visual Studio代码都是可行的选择,还有其他选择。对于Linux,我不太确定您的选择是什么。希望Linux专家能够提供一些建议。
std::vector<int> vectorPrimes(accountant);