Can't figure out why code is hanging up

This is a discussion on Can't figure out why code is hanging up within the C Programming forums, part of the General Programming Boards category; I know I've been asking 1000 questions and most of them are simple answers, but I've tested this one extensively ...

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    12

    Can't figure out why code is hanging up

    I know I've been asking 1000 questions and most of them are simple answers, but I've tested this one extensively and still cannot figure out what the deal is (cue still simple answer).

    My code hangs up after I ask "How many characters per line?: " and the user inputs a number and hits enter. Nothing shows up after that, but the code doesn't crash to the command line. As you can see from the code, I've inserted a number of printf statements to try to catch where it crashes, and not even the one right after the last input is printed. That leads me to believe it hangs up right after that, but I haven't changed that section of the code for a long time. I figured it would be my justify function, but it's not even getting there. Any help would be much appreciated.
    Attached Files Attached Files

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,452
    A few points.
    1. You're not being explicit about the return type of main - say int.

    2. You have a few needless global variables.

    3. You don't check the return result of fopen.

    4. You don't flush output, eg.
    printf("here7");
    should be
    printf("here7\n");
    or
    printf("here7");fflush(stdout);
    to guarantee that the text will be seen.

    Consider learning how to use the debugger, then you can single step and examine the state of the variables and watch what is happening.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Oct 2007
    Posts
    12
    1. Fixed that now, thanks.
    2. I cleaned this up. It had been that way because of how some of my functions worked, but I see now that they no longer need to be global.
    3. This is an assignment for a class, and we were told to assume that input file is always valid, though I see your point.
    4. Changed this, and can now see that they all printed.

    I will go try to use the debugger. I had trouble working with it in the past, but I guess now is a good as time as any to learn.

    Quick semi-related question:
    Is there any quick way to insert a value in the middle of a string without individually moving all characters past the index point one to the right?

    Thanks!

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,452
    > Is there any quick way to insert a value in the middle of a string.
    Not really, you have to do what you describe.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 03-21-2006, 06:52 AM
  2. Updated sound engine code
    By VirtualAce in forum Game Programming
    Replies: 8
    Last Post: 11-18-2004, 11:38 AM
  3. True ASM vs. Fake ASM ????
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 04-02-2003, 03:28 AM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM
  5. Replies: 4
    Last Post: 01-15-2002, 11:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21