#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    3
    Rep Power
    0

    Program Closes before Complete


    Hi

    I am new to programming and have started working through the Dummies guide to C.

    One of the programs I have done in the book is the Madlibs program below.

    /*
    Madlib1.c Source Code
    Written by David Milne
    */

    #include <stdio.h>

    int main()
    {
    char adjective[20];
    char food[20];
    char chore[20];
    char furniture[20];

    /* Get the words to use in the Madlib */

    printf("Enter an adjective:");
    scanf("%s",&adjective);
    printf("Enter a food:");
    scanf("%s",&food);
    printf("Enter a household chore:");
    scanf("%s",&chore);
    printf("Enter an item of furniture:");
    scanf("%s",&furniture);
    printf("Don't touch that %s %s!\n",adjective,food);
    printf("Im just %s the %s!\n",chore,furniture);

    return(0);
    }

    The problem I am having with it is it runs fine when run in a command prompt window that is already open however when it is run from the .exe file it closes the program after hitting enter after entering an item of furniture and I am not sure why.

    Thanks

    Dave
  2. #2
  3. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Location
    India
    Posts
    95
    Rep Power
    4
    its because of when ever u press enter string is taken by scanf but new line character is already in buffer so in next scanf will be skipped.

    to solve it add space before %s.

    and u made another mistake when ever u use character array or pointer ie. string in scanf no need to use & operator.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    159
    Rep Power
    19
    Please stop using SMS speak, use proper English.

    For example it's you not u.

    Comments on this post

    • eramit2010 agrees : sorry i will take care about it next time thanks...
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    3
    Rep Power
    0

    Program Closes before Complete


    Could you show me where I need to add the space? I have tried putting it in front of every %s but the program still closes after you enter an item of furniture.

    Thanks

    Dave
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    31
    Rep Power
    2
    From what I remember,for making reliable IO, you need to use the gets+sscanf functions. Otherwise nasty things can happen because scanf has a poor way to deal with the buffer (and with the position of the pointer in the buffer).

    here for example what can you do to put a string in adjective:
    Code:
    char * line;
    gets(line);
    sscanf(line, " %s", adjective);
    you do the same for food, chore, furniture...
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    3
    Rep Power
    0

    Program Closes before Complete


    I have found out that the program was running fine it was just displaying the output for a millisecond and then closing the window. I added

    system("PAUSE");

    and it is now staying open until the enter key is pressed

    Thanks

    Dave
  12. #7
  13. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,172
    Rep Power
    2222
    If you were to have looked in the Commonly Asked C/C++ Questions sticky thread (meaning that it's always there at the top), then you would have found your question to be the third one listed. It points to a 2004 message with answers and a bit of discussion at http://forums.devshed.com/c-programm...ds-157960.html. For example, your solution of running PAUSE would work on a Windows platform, but undoubtedly not on Linux and who knows about Macs.

    You already found that if you run your program as it was originally intended to be run -- namely from the command line -- that it works just fine. The problem is when you try to run it from a GUI. The reason is that the program is intended to run in a shell (on Windows AKA "console", AKA "DOS window"). When you run it from the command line, you're already in a shell so it works just fine.

    But if you're running a GUI app (eg, Windows Explorer) that then launches your program, part of that process is to create a shell for the program to run in. But then when your program terminates, the shell closes automatically, which creates the problem of the output going away before you can read it.

    The solution is to find some method to keep the shell from closing until you are ready to let it close. These methods can include:

    1. Just before exiting the program, add a request for user input. system("PAUSE") is one possibility, though it's OS-dependent. getchar() is another, as is scanf, but you can experience other problems because of characters left over in the input buffer.

    These methods all fall under the heading of "KLUDGE". They are jury-rigged, quick-and-dirty, inelegant, and clumsy quick fixes. They are aesthetically displeasing.

    2. Write a proper command-line app. Such as a program with a menu of actions that you can choose from, one of which is to exit the program (AKA "quit"). At the very least, prompt your user to enter a command to quit, even it involves pressing the "any key".

    3. Research into finding a setting for your shells to not close automatically. There does appear to be a way to do this, as I've witnessed certain IDEs using this approach.

    4. Run it from the command line as it was intended to be run.

    If you go with the kludge in #1 or even with #2, you need to know how to read input. The answers you got at first alluded to a problem you would have with scanf("%c,&ch).

    When you call scanf, it reads what's in the input buffer and converts it as you specify in the format string. But since C programs were originally run from terminals, many of which were line-oriented, the program doesn't recognize any input until you've hit the Enter key. scanf then scans and converts the input as per the format string and when it's done it stops, leaving any unconverted characters in the input buffer. The next scanf starts from there, converting what it can and/or waiting for the next line of input to continue. Hence you could have typed all four of your input values on one line and the four successive scanf's would have read them all in correctly.

    scanf normally skips white space, which includes newlines, but %c is different. If you write scanf("%c",&ch), then scanf would read in the next character regardless of what it is, even white space. So if the next character in the input buffer were the newline character(s) from the Enter key, then that would be read in and scanf would be satisfied. That would cause a common complaint we receive of the program just blowing right past the scanf.

    The solution to that would be to tell scanf to expect and skip any white space that may precede that character, thus skipping the newline from the last scanf. That would be written thus:
    scanf(" %c", &ch);
    This was the leading space that the others were telling you about, though I am sure that it's not necessary for their examples (doesn't do any harm, but still not needed). It is needed when dealing with %c .

    Please note that scanf(" %c", &ch) requires a printable character to satisfy scanf. Hitting the space bar simply will not work, since a space is white space and will be skipped by scanf.

    Please also note that getchar will create the same problem as scanf("%c",&ch).

IMN logo majestic logo threadwatch logo seochat tools logo