0

I need this code to read through every line for the loop, however it is only reading through the first line. It is pulling from a file that has nine scrambled words total and should read through all nine and unscramble them to make four letter words. Any suggestions?

    position = 0;


    test_word = puzzle;
    cout << "Is " << test_word << " a word? " << endl;
    while (!wordpuzzle.eof())
    {
        for (int i = 0; i < puzzle.length(); i++);
        {
            while (position <= puzzle.length() - 4)
            {
                test_word = puzzle.substr(position, 4);
                cout << "Is " << test_word << " a word? " << endl;

                while (!dictionary.eof())  // Loop through dictionary comparing each word.
                {
                    dictionary >> word;   // Get a word from the dictionary
                    counter++;
                    if (word == test_word)
                    {
                        cout << "The word " << test_word << " was found in the scramble. "
                            << " Word " << counter << endl;
                        found = true;
                        break; // Exit while loop if word is found
                    }
                }
                while (!dictionary.eof())  // Loop through dictionary comparing each word.
                {
                    dictionary >> reverseword;   // Get a word from the dictionary
                    counter--;
                    if (reverseword == test_word)
                    {
                        cout << "The word " << test_word << " was found in the scramble. "
                            << " Word " << counter << endl;
                        found = true;
                        break; // Exit while loop if word is found
                    }
                }
                if (!found)
                    cout << "The string " << test_word << " is not a word." << endl;
                dictionary >> word;
                // Reset everything for the next substring from the scramble.

                dictionary.clear();
                dictionary.seekg(0, ios::beg);
                position++;
                counter = 0;
                found = false;
            }
        }
    }
    cout << endl << endl;

    dictionary.close();
    system("pause");
    return 0;
}
Stormy
  • 49
  • 8
  • 1
    What is `wordpuzzle`? Please provide the full code and indicate where (which line, etc) your program doesn't behave as expected. Also, attach input file(s) if possible. The more information you provide, the better. As it is now, no one can even compile this. – nakiya Oct 01 '18 at 02:25
  • 3
    Well, there are a couple of suggestions. [`while (!...something.eof())` is always a bug](https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong). You can start by fixing that. Then, fix all the duplicated code. There's a bunch of logic that's duplicated. Duplicated code is a breeding code for bugs. Chances are that as part of the process of cleaning up these simple bugs, you will find and fix your real bug. – Sam Varshavchik Oct 01 '18 at 02:26
  • Also note that it is really hard to read a file to the end and then get anything meaningful out of it on subsequent reads without rewinding the file. – user4581301 Oct 01 '18 at 03:31
  • You are using wordpuzzle only once when you test in the beginning of the while loop. Should not you read susequent lines from wordpuzzle? – Cristi Oct 01 '18 at 06:55

0 Answers0