1

我不断收到一条错误消息,说 ISO C++ 禁止可变大小数组。

我想有一个显示的输出

Level       Score          Stars
----------------------------------
1              3840           ***

等等....

这是我的程序

#include <iostream> // access to cin, cout
#include <cstring>
#include <cstdlib>
#include<fstream>


using namespace std;

int buildArrays(int A [], int B [], int C [])
{
    int i = 0, num;
    ifstream inFile;
    inFile.open("candycrush.txt");

    if (inFile.fail())
    {
        cout << "The candycrush.txt input file did not open" << endl;
        exit(-1);
    }
    while (inFile)
    {
        inFile >> num;
        A[i] = num;

        inFile >> num;
        B[i] = num;

        inFile >> num;
        C[i] = num;

        i++;
    }
    inFile.close();

    return i;
}
void printArrays(string reportTitle, int levelsArray [], int scoresArray [], int starsArray [], int numberOfLevels)
{
    cout << endl;
    cout << reportTitle << endl;
    cout << "Levels\tScores\tStars" << endl;
    cout << "---------------------" << endl;

    for (int i = 0; i < numberOfLevels; i++)
    {
        cout << levelsArray[i] << "\t" << scoresArray[i] << "\t";
        for (int j = 0; j < starsArray[i]; j++)
        {
            cout << "*";
        }
        cout << endl;
    }
}

void sortArrays(int levelsArray [], int scoresArray [], int starsArray [], int numberOfLevels)
{
    for (int i = 0; i < numberOfLevels; i++)
    {
        for (int j = 0; j < numberOfLevels; j++)
        {
            if (levelsArray[i] < levelsArray[j])
            {
                int temp1 = levelsArray[i];
                int temp2 = scoresArray[i];
                int temp3 = starsArray[i];

                levelsArray[i] = levelsArray[j];
                scoresArray[i] = scoresArray[j];
                starsArray[i] = starsArray[j];

                levelsArray[j] = temp1;
                scoresArray[j] = temp2;
                starsArray[j] = temp3;
            }
        }
    }
}


int main()
{
    int MAX = 400;         (This is where I am getting my valid array size error)
        int levelsArray[MAX];
    int scoresArray[MAX];
    int starsArray[MAX];

    int numberOfLevels = buildArrays(levelsArray, scoresArray, starsArray);

    printArrays("Candy Crush UNSORTED Report", levelsArray, scoresArray, starsArray, numberOfLevels);
    sortArrays(levelsArray, scoresArray, starsArray, numberOfLevels);
    printArrays("Candy Crush SORTED Report", levelsArray, scoresArray, starsArray, numberOfLevels);

    system("pause");
}
4

6 回答 6

4

除非您(或您的老师,如果您将其作为家庭作业)打算做得不好,否则您不应该简单地转换MAXconst.

相反,您应该使用std::vector而不是使用数组。

只要你在它:

  1. 创建一个struct来保存单个乐谱的三个部分。
  2. 使用std::sort而不是您自己的排序功能。
  3. 重载operator>>并在对象operator<<上执行 I/O 。score
  4. 更喜欢一步初始化而不是默认构造,然后是真正的初始化(例如,创建,然后单独打开一个流以及创建然后单独填充向量)。
  5. 永远不要使用while (stream) read_data1。始终测试读取数据的结果,并对结果做出反应。

使用这些,我们最终得到如下代码:

struct score { 
    int level;
    int score;
    int stars;

    bool operator<(score const &other) const { 
        return level < other.level;
    }

    friend std::istream &operator>>(std::istream &is, score &s) { 
        return is >> s.level >> s.score >> s.stars;
    }

    friend std::ostream &operator<<(std::ostream &os, score const &s) { 
         return os << s.level << "\t" 
                   << s.score << "\t"
                   << std::string(s.stars, '*');
    }
};

int main() { 
    std::ifstream in("candycrush.txt");
    std::vector<score> scores{std::istream_iterator<score>(in),
                              std::istream_iterator<score>()};

    std::cout << "Unsorted:\n";
    for (auto const &s : scores)
        std::cout << s << "\n";

    std::cout << "\n";

    std::sort(scores.begin(), scores.end());
    std::cout << "Sorted:\n";

    for (auto const &s : scores) 
        std::cout << s << "\n";
}

您可能还应该添加一些东西来处理两个相同级别的分数(例如,通过在这种情况下比较分数),但这可能是另一个答案/诽谤的主题。


1. ...或while (stream.good())while (!stream.eof())

于 2013-11-11T06:09:25.690 回答
2

gcc support variable size arrays, but other compilers do not. Try.

int main()
{
    #define MAX 400        /* use macro instead of stack variable */
    int levelsArray[MAX];
    int scoresArray[MAX];
    int starsArray[MAX];
    /* rest of your code */
    return 0;
}
于 2013-11-11T05:43:25.790 回答
2

The array size should be a compile time constant in C++ for most compilers. Try

#define MAX 400;
...
int levelsArray[MAX];

or

const int MAX=400;
...
int levelArray[MAX];
于 2013-11-11T05:44:04.300 回答
2

You need to make the MAX variable to const variable. Try this:

const int MAX=400;
于 2013-11-11T05:44:36.217 回答
2
#include <iostream> // access to cin, cout
#include <cstring>
#include <cstdlib>
#include<fstream>

#define MAX 400 //<- Try this

using namespace std;

我还建议在处理多个数组时使用类。通过使用类,您无需将多个数组传递给一个函数并使用那长长的数组列表设置函数的参数。比如这个:

void printArrays(string reportTitle, int levelsArray [], int scoresArray [], int starsArray [], int numberOfLevels)
于 2013-11-11T06:05:25.250 回答
0

请遵循矢量用法(当您的目的需要可变大小的数组时):https ://stackoverflow.com/a/49021923/4361073

于 2018-02-28T04:22:51.057 回答