Chris Chris - 3 months ago 18
C++ Question

C++ - Text file not being read by fstream

I'm using Windows 7 Home Premium and MS Visual 2010 Express. I have very limited experience with IDE's, though I've run simple, console-based, C++ code, in this IDE, in past without problems.

In this case the code, shown below, debugs without any issue, but hangs on the console window. It is supposed to display the "scrambled word" read from the "input.txt", preceded by the iteration number, that is being compared to the words in the "wordlist.txt". If a match is found, this in turn should be displayed in the same console window. An output text is ultimately created with the resulting unscrambled words.

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

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())
{
std::vector<bool> match(scramlen);
std::vector<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.at(y) == lttrscram) match[matchcount] = false;

else
{
used.at(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;
}
}
}
file.close();
}
}

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

int count = 0;
string scramtemp;
ifstream file("input.txt");
if (file.is_open())
{
while (file.good());
{
getline (file,scramtemp);
inputkey[count] = scramtemp;
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;
}


The file structure for the project at present can be seen below:

enter image description here

The issue of the console window hanging is as seen below:

enter image description here

I've read and tried the suggestions in this post
, and this one,
to no avail.

I don't think the code is faulty. So why then is the "input.txt" not being read and the comparison process not being shown in the console?

Answer

See, your code in main which you're reading from file:

while (file.good());
//                ^^^
{
    getline (file,scramtemp);
    inputkey[count] = scramtemp;
    count++;
}

See, you've a semicolon at the end of the while (file.good());, as the file is good and and because of the semicolon, it isn't reading from the file, so it will always be good, the condition will always be true, so the program stucks in an infinite loop. Remove this semicolon and you're good to go.

Comments