Chris Chris - 3 months ago 24
C++ Question

C++ - Getting values from text file into array for comparison

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;
}

Answer

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.