Thread: Debug Assertion Fail in letter guessing game

  1. #1
    Registered User
    Join Date
    Jun 2012
    Posts
    2

    Debug Assertion Fail in letter guessing game

    Hey guys, newbie programmer here taking his first programming class. I've been working on my assignment which is to make a simple letter guessing game. Well, after messing around with it for hours trying to reduce the number of errors, it finally ran. It wasn't until I entered the number of games that I wanted to play (the first thing the program asks you for) that I got the following error:

    Debug Assertion Fail in letter guessing game-eteoe-jpg

    Anyway, I've tried googling for help but to no result. BUT! I do think after much googling that it is related to the file that the program is attempting to read off of. What it means? I'm not entirely sure (I apologize for being so programming illiterate, I will try my best to follow you guys with whatever advice you have). Here is my code on codepad, I imagine it would help!: C code by kevinjaems - 167 lines - codepad

    Thanks in advance.

  2. #2
    Registered User antred's Avatar
    Join Date
    Apr 2012
    Location
    Germany
    Posts
    257
    Ok, first off, it seems you are a literate person. So why don't you do what the dialog suggests and press "Retry" to have the debugger point you to the problem?

    But anyhow, the most likely error is that this:

    Code:
    infile = fopen("inputLet.txt", "r");
    has failed to open the file. If fopen() fails, it returns a null pointer, so you must ALWAYS check its return value. Never assume that an API call will probably succeed. A programmer must be a pessimist and be prepared to deal with function calls that fail, even if only to report the error and exit the program.

  3. #3
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    When you open a file you need to check that it actually opened. Something like this:
    Code:
    #include <stdlib.h>  // add this to top of program (for exit())
    
    	infile = fopen("inputLet.txt", "r");
    	if (infile == NULL) {
    		fprintf(stderr, "Cannot open inputLet.txt\n");
    		exit(1); // return a non-zero value to indicate failure
    	}
    There seems to be quite a few other things wrong with the logic of your program, but that should fix the segfault.

    Also, it's best to post your code here on the site. Just remember to post it between "code" tags, like this:

    [code]
    your code here
    [/code]
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  4. #4
    Registered User
    Join Date
    Jun 2012
    Posts
    2
    Hi antred and oogabooga. Thanks for your replies!


    oogabooga, I followed your advice and added in the code in an effort to fix the error, however, this just results in the program returning "Cannot open inputLet.txt" and not running.
    Also, I imagine there are tons of problems with the logic! This is my first assignment after all.
    I did compile the code using GCC and ran the .exe through Command Prompt instead of Visual Studio C++ this time.
    The first result you can see is when I added the code you pasted above.
    The second is without. As you can see, the result is crap!

    Debug Assertion Fail in letter guessing game-1zuo2-jpg EDIT: I just realized the .txt file must be in the same folder as the .exe file. Ha! I'm dumb. I'm going to see what that results in now.
    Last edited by kevinjaems; 06-16-2012 at 09:25 PM.

  5. #5
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    That means that the file is not being found. Does it exist? Is it in the same directory as the exe?
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with letter guessing program
    By ltdec in forum C Programming
    Replies: 31
    Last Post: 10-05-2011, 12:29 AM
  2. Assertion Fail error
    By TheEngineer in forum C Programming
    Replies: 14
    Last Post: 05-13-2010, 10:22 AM
  3. debug assertion failed!
    By chintugavali in forum C++ Programming
    Replies: 5
    Last Post: 12-21-2007, 04:05 AM
  4. debug assertion failed!
    By chintugavali in forum C Programming
    Replies: 4
    Last Post: 12-11-2007, 06:23 AM
  5. debug assertion failure
    By talz13 in forum Windows Programming
    Replies: 2
    Last Post: 07-20-2004, 11:23 AM

Tags for this Thread