首页 文章

C - 将值从文本文件获取到数组中以进行比较

提问于
浏览
1

我正在使用Microsoft Visual C 2010 Express . 运行我的代码调试会导致以下错误:

1>------ Build started: Project: Word Unscrambler, Configuration: Debug Win32 ------
1>  word unscrambler.cpp
1>c:\users\c m j richards\documents\visual studio 2010\projects\word unscrambler\word unscrambler\word unscrambler.cpp(22): error C2057: expected constant expression
1>c:\users\c m j richards\documents\visual studio 2010\projects\word unscrambler\word unscrambler\word unscrambler.cpp(22): error C2466: cannot allocate an array of constant size 0
1>c:\users\c m j richards\documents\visual studio 2010\projects\word unscrambler\word unscrambler\word unscrambler.cpp(22): error C2133: 'match' : unknown size
1>c:\users\c m j richards\documents\visual studio 2010\projects\word unscrambler\word unscrambler\word unscrambler.cpp(23): error C2057: expected constant expression
1>c:\users\c m j richards\documents\visual studio 2010\projects\word unscrambler\word unscrambler\word unscrambler.cpp(23): error C2466: cannot allocate an array of constant size 0
1>c:\users\c m j richards\documents\visual studio 2010\projects\word unscrambler\word unscrambler\word unscrambler.cpp(23): error C2133: 'used' : unknown size
1>c:\users\c m j richards\documents\visual studio 2010\projects\word unscrambler\word unscrambler\word unscrambler.cpp(59): warning C4154: deletion of an array expression; conversion to pointer supplied
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

我本质上是试图创建一个单词unscrambler,通过使用一个大小等于从“input.txt”文件传入函数的单词的字符串长度的布尔数组 . 然后将其与匹配字符的“wordlist.txt”内容进行比较 .

比较的字符串和成功匹配的字符串应通过控制台窗口显示,并导出到“output.txt” .

我已将“wordlist”和“input”文本文件放在工作目录中(即与.vc proj文件相同),但从失败的调试判断,我不认为ifstream正在访问这些文本文件 .

以下是IDE的屏幕截图:
enter image description here

这是代码:

#include<string>
#include<cstdio>
#include<iostream>
#include<fstream>

using namespace std;

string unscramble(string scram)
{
    int scramlen = scram.length();
    int i = 0;

    string word;
    ifstream file("wordlist.txt");
    if (file.is_open())
    {
        while (file.good())
        {
            getline(file,word);
            if (scramlen == word.length())
            {
                bool match[scramlen];
                string used[scramlen];
                int matchcount = 0;

                for (int x = 0; x < scramlen; x++)
                {
                    string lttrscram = scram.substr(x,1);

                    for (int y = 0; y < scramlen; y++)
                    {
                        string lttrunscram = word.substr(y,1);

                        if (lttrscram == lttrunscram)
                        {
                            if (used[y] == lttrscram) match[matchcount] = false;

                            else
                            {
                                used[y] = lttrscram;
                                match[matchcount] = true;
                                matchcount++;
                                break;
                            }
                        }
                    }
                }

                i = 0;
                for (int j = 0; j < scramlen; j++)
                {
                    if (match[j] == true) i++;
                }
                if (i == scramlen)
                {
                    cout <<"Match found: " << word << endl;
                    return word;
                }
                delete [] match;
            }
        }
        file.close();
    }
}

int main()
{
    string inputkey[10];
    string outputkey[10];
    int wordnum = 0;

    int count = 0;
    string wordtemp;
    ifstream file("input.txt");
    if (file.is_open())
    {
        while (file.good());
        {
            getline (file,wordtemp);
            inputkey[count] = wordtemp;
            count++;
        }
        file.close();
    }

    for (int i = 0; i < 10; i++)
    {
        wordnum++;
        cout <<"#" << wordnum << " Comparing: " << inputkey[i] << endl;
        outputkey[i] = unscramble(inputkey[i]);
    }

    ofstream output;
    output.open("output.txt");

    for (int j = 0; j < 10; j++)
    {
        if (j == 9) output << outputkey[j];
        else output << outputkey[j] << ", ";
    }
    output.close();

    system("pause");
    return 0;
}

2 回答

  • 0

    您的问题是 scramlen 未初始化 . 因此 bool match[scramlen];string used[scramlen]; 将阻止您编译 .

    考虑使用库 #include <vector> 中的 std::vector 而不是 array . 您将能够像数组一样访问元素,但可以动态调整大小 . 用法是一个类似于此的例行程序:

    int scramlen = scram.length();
    std::vector<bool> match(scramlen);
    std::vector<int> used(scramlen);
    
    // ..
    
    else
    {
        used[y] = lttrscram;
        match[matchcount] = true;
        matchcount++;
        break;
    }
    

    Edit:

    从阅读评论,它看起来像我在一件事情上感到困惑 . 您无法使用变量初始化数组,原因如下:Array[n] vs Array[10] - Initializing array with variable vs real number . 你需要一个常数 integer . 然而,使用 std::vector 是解决方案 .

  • 1

    这不合法C:

    int scramlen = scram.length();
    //...
    bool match[scramlen];
    string used[scramlen];
    

    C中的数组必须使用编译时常量来表示条目数,而不是变量 . 此语法可能适用于支持 Variable Length Arrays (VLA)的编译器,但这是编译器扩展,因此是非标准的 .

    这个扩展由 g++ 等编译器支持,但它是 not 并且从未得到Visual C系列编译器的支持(并且不需要支持它,因为同样,以这种方式声明数组不是合法的C ) .

    无论如何,即使编译器确实支持VLA,我也建议不要使用VLA . 相反,如果您想拥有动态数组,请使用 std::vector . 它是标准的C(因此可以与所有编译器一起使用),并为您提供诸如检查数组边界(使用 vector::at() )的好处,这是VLA无法做到的 .

    #include <vector>
    //...
    std::vector<bool> match(scramlen); // See item below
    std::vector<string> used(scramlen);
    

    此外,您在非指针类型上发出 delete [] 调用时出错 . 删除此行:

    delete [] match;
    

相关问题