I'm in an introductory CS course and our task is to build the best ideal boggle board (one that would get you the most points). We are allowed to use arrays, c-strings, and reference parameters (no structs/pointers since we are just starting to learn about those).

Step one: I built my boggle board with randomly generated letters. (It is not part of this source file, I made another source specifically to try to read in a dictionary).
Step two: Read in a dictionary. I'm trying to output it to make sure it works, however, that is not part of the program, so as soon as I see it outputs I'll be taking that part of the code out.

I'm stuck on the second part of my program. Everytime I run this code I get "ConsoleApplication4.exe has stopped working".

I am using MSVS 2012 on Windows 7 with 12GB RAM and i7-930 CPU (idk if this matters).

Can someone tell me what I am doing wrong and how I can fix it? The dictionary.txt file is in the same file as the source file. Thanks alot!

#include <iostream>
#include <cstring>
#include <fstream>
#include <cassert>
using namespace std;

const int Size = 17;                // 16 characters for the line + 1 for the '\0'
const int MaxNumberOfWords = 253633;    // maximum number of words to be stored

int main()
    ifstream inStream;                          // declare an input stream for my use
    char theWords[MaxNumberOfWords][ Size];    // Array to store words from input line
    int wordRow = 0;                            // Row for the current word

    inStream.open( "dictionary.txt");
    assert( ! inStream.fail() );  // make sure file open was OK

    // Keep repeating while input from the file yields a word
    while (inStream >> theWords[wordRow])

    // Go through the array of words, displaying each one in turn.
    // Note that we only display the words read, not the empty array
    // rows.
    cout << "\n\n";
    for (int i=0; i < wordRow; i++)
        cout << i << ". " << theWords[ i] << endl;     // display this word

    cout << "Done.\n";

    return (0);

Recommended Answers

All 2 Replies

assert( ! inStream.fail() ); // make sure file open was OK

And if the file wasn't opened okay you'll hard stop the process with a cryptic error? Sounds like the pinnacle of user-friendliness. :rolleyes: assert() is intended to help the programmer debug impossible errors at development time; it's not meant to handle likely runtime errors that are out of the programmer's control (ie. user input, file existence, etc...).

I'm stuck on the second part of my program. Everytime I run this code I get "ConsoleApplication4.exe has stopped working".

Your process hard stopped with a cryptic error... It's a curse being right all of the time. ;) I have a better idea, check for a successfully open file with an if statement and print a useful diagnostic if it fails:

inStream.open( "dictionary.txt");

if (!inStream) {
    perror("Error opening dictionary.txt");
    return 0;

perror() will likely give you a more useful error message as to why the file couldn't be opened, but you'll need to include <cstdio> for its declaration.

Thanks for your input.

It had nothing to do with the cryptic error. I needed to allocate dynamic memory because the dictionary file was so big that's why I kept getting an error.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.