1

I'm using Microsoft Visual C++ 2010 Express. Running debug of my code results in the following errors:

    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 ==========

I'm essentially trying to create a word unscrambler, by using a boolean array of size equal to the string length of the word passed into the function from the "input.txt" file. This is then compared to the "wordlist.txt" content for matching characters.

The compared strings, and those that are successfully matched should be shown via the console window, and exported to an "output.txt".

I have placed the "wordlist" and "input" text files in the working directory (i.e. the same as .vc++proj file) but judging from the failed debug, I don't think ifstream is accessing these text files.

Here is a screenshot of the IDE: enter image description here

And here is the code:

#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;
}
Albert F D
  • 529
  • 2
  • 13
  • 33

2 Answers2

1

This is not legal C++:

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

Arrays in C++ must use compile-time constants to denote the number of entries, not a variable. This syntax may work with a compiler that supports Variable Length Arrays (VLA's), but this is a compiler extension, and thus is non-standard.

This extension is supported by compilers such as g++, but it is not and has never been supported by the Visual C++ series of compilers (and there is no need to support it, as again, it is not legal C++ to have arrays declared this way).

In any event, I would advise against using VLA's even if the compiler did support them. Instead, if you want to have a dynamic array, use std::vector. It is standard C++ (thus will work with all compilers), and gives you benefits such as checking for array bounds (using vector::at()), something that VLA's cannot do.

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

In addition, you are making a mistake issuing a delete [] call on a non-pointer type. Remove this line:

 delete [] match;
PaulMcKenzie
  • 34,698
  • 4
  • 24
  • 45
  • Thank you very much. I did not know about the vector class in the standard library, and I now understand the error I was getting at compile time. I have adapted my code accordingly, and it now compiles without issue... but I've encountered another bug! :( I will try work it out myself. Thanks again!!! :) – Albert F D Aug 12 '16 at 21:51
0

Your problem is that scramlen is not initialized. So bool match[scramlen]; and string used[scramlen]; will prevent your from compiling.

Consider using std::vector from the library #include <vector> instead of array. You will be able to access the elements exactly like arrays but to resize dynamically. And the usage is a routine similar to this:

int scramlen = scram.length();
std::vector<bool> match(scramlen);
std::vector<int> used(scramlen);

// ..

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

Edit:

From reading the comments, It looks Like I got confused in one thing. You can not initialize an array with a variable and here is why: Array[n] vs Array[10] - Initializing array with variable vs real number. You will need a constant integer. Using std::vector is however the solution.

Community
  • 1
  • 1
Khalil Khalaf
  • 9,259
  • 11
  • 62
  • 104
  • The reason for the compilation error has nothing to do with the variable not being initialized. – PaulMcKenzie Aug 12 '16 at 06:43
  • I read your answer and I learned nothing. Could you please tell me what is the reason then? – Khalil Khalaf Aug 12 '16 at 12:12
  • What is not understood in my answer? It is quite clear -- the OP is attempting to declare an array using a variable as the number of items. That is not legal C++. You claimed that the reason is that `scramlen` wasn't initialized -- that is patently false. It doesn't matter if it was or was not initialized, you can't declare arrays using a variable as the number of items. – PaulMcKenzie Aug 12 '16 at 12:23
  • Oh no no it is clear absolutely.. explained well and it has a logical flow. But at first impression I did not see the new thing or/and learn from it (maybe for the last part which is fine) but overall, it matched my answer. So I had to check and I think you were right [Initializing array with variable vs real number](http://stackoverflow.com/questions/15013077/arrayn-vs-array10-initializing-array-with-variable-vs-real-number). Now I see it and thanks for the lesson :)! – Khalil Khalaf Aug 12 '16 at 13:21
  • 1
    Sorry to bother you, but please, don't use (nor emphasize) the expression _real_ number, it may be misleading (one could end up with `int arr[3.14159];`...). Constant or _integer_ literal would be enough. – Bob__ Aug 12 '16 at 15:24
  • @Bob__ very useful comment. Not bothered at all ! Thanks for pointing that out :) – Khalil Khalaf Aug 12 '16 at 15:34
  • Thanks guys, I understand the issue now. It certainly seems vectors are more versatile than arrays, it's the first time I've encountered them, so getting used to the syntax. You've been very helpful! :D – Albert F D Aug 12 '16 at 21:53
  • @Chris glad we were able to help :) – Khalil Khalaf Aug 12 '16 at 22:12