0

I am writing code for a project in my computer science course and I am testing my algorithm to see if it works and how fast it is. I know that the algorithm works because when I launch the program in Visual Studio 2013, I get the correct output. But, when I launch the .exe from the Visual Studio projects folder in the command line or from windows explorer, the first two cout statements are displayed correctly, but the cout statements during the for loop are not displayed at all. Again this only happens when I launch the .exe outside of Visual Studio. It isn't a big problem, but I wonder what's going on here. Thanks.

Here is int main() (The first 2 couts work and the others don't):

int main() {
    // declare input stream for reading dctnryWords.txt
    ifstream inFile;
    // create a pointer to memory in the heap
    // where each word in the dictionary will be stored
    string* words = new string[DICTIONARY_SIZE];
    // create a vector of forward_lists to hold
    // adjacent words for each word in the dictionary
    vector< list<string> > adjacents(DICTIONARY_SIZE);
    // open dctnryWords.txt
    inFile.open("dctnryWords.txt");
    // load words into RAM
    cout << "Loading words into RAM took: "
        << time_call([&] { copyDictionary(inFile, words); })
        << "ms\n";

    cout << "Finding adjacent words took: "
        << time_call([&] { searchAdjacents(words, adjacents); })
        << "ms\n";

    for (int i = 0; i < DICTIONARY_SIZE; i++) {
        if (adjacents[i].size() >= 25) {
            cout << words[i] << "(" << adjacents[i].size()
                << "): ";
            for (list<string>::const_iterator j = adjacents[i].cbegin(); j != adjacents[i].cend(); j++) {
                cout << *j << " ";
            }
            cout << endl << endl;
        }
    }

    return 0;
}
Barmar
  • 741,623
  • 53
  • 500
  • 612
Keith Larson
  • 13
  • 1
  • 4
  • Try printing `adjacents[i].size()` before the `if`. – Barmar Nov 05 '15 at 22:04
  • 1
    Is the working directory different? It probably isn't finding the file – Kevin Nov 05 '15 at 22:05
  • 1
    Could you try to flush cout by `cout.flush();` ? http://www.cplusplus.com/reference/ostream/ostream/flush/ See http://stackoverflow.com/questions/22026751/c-force-stdcout-flush-print-to-screen – francis Nov 05 '15 at 22:07
  • Interesting, I tried cout << adjacents[i].size() << " "; right above the if statement and I got the size of each list in the vector when I launched it in VS2013, but in command line, the size of each list was 0. – Keith Larson Nov 05 '15 at 22:10
  • What you need to do is called [debugging](https://en.wikipedia.org/wiki/Debugging). Adding additional `cout` statements to track the status of your program and its data in greater detail is an easy first step. – Brent Bradburn Nov 05 '15 at 22:14
  • This question was resolved in a manner unlikely to help future readers. The title has nothing to do with the solution. The OP just needed basic debugging assistance. – Brent Bradburn Nov 05 '15 at 22:31

1 Answers1

1

I'll bet a nickel your program isn't finding "dctnryWords.txt" when you launch it elsewhere... because it's going to look in the current directory, which is likely different when you run it outside of VS.

kcraigie
  • 1,252
  • 6
  • 13