Thread: very basic program just exits

  1. #1
    Registered User
    Join Date
    Aug 2005
    Posts
    38

    very basic program just exits

    This is the same code from the tutorial, and i was reading it, but once i execute it, it just quits when i enter an age and press enter... why is that?

    Code:
    #include <stdio.h>
    
    int main ()
    
    {
        int age;
        printf ("please enter your age");
        scanf ( "%d", &age );
        if ( age < 40 ) 
        {
         printf ("You are not old enough!\n");
    }
     else if (age == 40 )
    {
          printf ("You are borderline!\n");
    }
     else
    {
    printf ("You're pushing it buddy!");
    }
     return 0;
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    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.

  3. #3
    Registered User
    Join Date
    Aug 2005
    Posts
    38
    cool thx

    but how would i make it so that after it tells me if i'm not old enough or w/e that it waits for me to press enter and doesnt just quit on its own?

    i put getchar () at the end but that didnt do anything

    oh and what is the \n that was put in the tutorial? if i take it out, it makes no diffrence
    Last edited by seal; 08-28-2005 at 02:09 AM.

  4. #4
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    I would imagine that the newline which the earlier call to scanf() did not read in (the newline which you entered by pressing <ENTER> after the number you entered) is still sitting in the buffer, and when the program gets to the getchar(), there is a newline there waiting for it, so it does not need you to press <ENTER> again.

    Try something like this:
    Code:
    #include <stdio.h>
    
    int main()
    {
            int c;
            int age;
            printf("please enter your age");
            scanf("%d", &age);
            while ((c = getchar()) != EOF && c != '\n') {
                    ;
            }
            if (age < 40) {
                    printf("You are not old enough!\n");
            } else if (age == 40) {
                    printf("You are borderline!\n");
            } else {
                    printf("You're pushing it buddy!");
            }
            getchar();
            return 0;
    }
    This code

    Code:
     while ((c = getchar()) != EOF && c != '\n') {
                    ;
            }
    makes repeated calls to getchar() as long as the condition is not EOF or a '\n' (newline) - it basically 'eats' every remaining character (including the newline) in the buffer. The ; on the line all by itself is a nulll statement - all of the actual work is done in the while statment one line above, but syntactically, the semicolon ';' is necessary so we add it - it could have been placed right after the while statement on the same line like so:

    Code:
    while ((c = getchar()) != EOF && c != '\n') ;
    But I prefer to have it out on its own line so there is no mistaking that it is indeed a null statement - but each to his own on style issues like that one.

    As to the '\n', that is your newline (as I mentioned above) - it is the way that you represent a character that does not really have a representation - similar characters would be '\t' for a tab, and '\b' for a backspace. A ' ' would be a space obviously.
    Last edited by kermit; 08-28-2005 at 06:35 AM.

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by kermit
    The ; on the line all by itself is a nulll statement - all of the actual work is done in the while statment one line above, but syntactically, the semicolon ';' is necessary so we add it - it could have been placed right after the while statement on the same line like so:

    Code:
    while ((c = getchar()) != EOF && c != '\n') ;
    But I prefer to have it out on its own line so there is no mistaking that it is indeed a null statement - but each to his own on style issues like that one.
    Code:
    while( (c=getchar()) != EOF && c != '\n' ){ }



    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Registered User
    Join Date
    Aug 2005
    Posts
    38
    man this stuff is confusing...

    programming is something that i've always wanted to do, but they just all seem so confusing...

  7. #7
    Registered User
    Join Date
    Aug 2005
    Posts
    38
    where exactly do i need these ?

    Code:
    { and }
    i thought it was like where your 'program' sits in... one in the beginning, and one in the end.... but apparently not lol

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    They define the beginning and end of a block (new scope) of code. They're also used for initializing arrays, and in the definition of structures. Here however, I'm just using them in place of a single ; operator, just to show that you don't really have to have the ;. I'm defining an empty block of code. It doesn't do anything at all. However, in this case it acts like an empty statement (the ; by itself).

    There's a decent number of us around here who like to be pedantic about things, so when someone says something like "never" or "always", and the like, we like to point out things which don't fit the statement.


    Quzah.
    Hope is the first step on the road to disappointment.

  9. #9
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Code:
    while ((c = getchar()) != EOF && c != '\n') ;
    is just the same as
    Code:
    while ((c = getchar()) != EOF && c != '\n') {}
    It just means while getchar does not return end of file and getchar returns new line do nothing => remove new lines from the buffer.
    Kurt

  10. #10
    Registered User
    Join Date
    Aug 2005
    Posts
    38
    ahhh i see....

    shouldnt it be

    Code:
    {
     while ((c = getchar()) != EOF && c != '\n') 
    }
    ?

    also what is %d ?

    sorry for such basic questions guys, this is how i learn, i ask and i finally get it...

    about 4 years ago i didnt know what an alternator on a car was for...
    now i have rebuilt motors from the block scratch using message boards' helps

  11. #11
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Quote Originally Posted by seal
    Code:
    {
     while ((c = getchar()) != EOF && c != '\n') 
    }
    This would give you a syntax error. ( missing semicolon )
    Kurt

  12. #12
    Registered User
    Join Date
    May 2005
    Posts
    207
    That goes back to what quzah said:

    Quote Originally Posted by quzah
    They define the beginning and end of a block (new scope) of code.
    The way you used the curly brackets means that the code inside will be executed unconditionally (ie: you won't see a difference).

    The curly brackets tells the computer "execute this code seperately from normal", and you have to define a condition above it so that it only executes when your condition is met.

    mw
    Last edited by Lionmane; 08-28-2005 at 02:08 PM.
    Blucast Corporation

  13. #13
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by ZuK
    It just means while getchar does not return end of file and getchar returns new line do nothing => remove new lines from the buffer.
    Kurt
    Actually, it removes everything from the buffer, one character at a time. It only stops on EOF or a newline.


    Quote Originally Posted by Lionmane
    That goes back to what quzah said:

    The way you used the curly brackets means that the code inside will be executed unconditionally (ie: you won't see a difference).

    The curly brackets tells the computer "execute this code seperately from normal", and you have to define a condition above it so that it only executes when your condition is met.

    mw
    To further the point, you can use { } nearly any time you like. For example, you could do this:
    Code:
    int main ( void )
    {
        {
            {
            }
        }
        {
            {
            }
        }
        {
            {
            }
        }
        return 0;
    }
    Now it doesn't do much of anything here, but it's fine to do so. One thing to note when defining new code blocks (scope blocks) is that you can define new variables at the beginning of scope. These variables end with the end of scope (there's a way around that, but ignore it for now). So, you could do this:
    Code:
    int main ( void )
    {
        int x;
        {
            int y; /*x and y are here */
            {
                int z; /* x, y, z are here */
            }
            /* z is gone */
        }
        /* y and z are gone */
        {
            int y; /* x and a different y are here */
            {
                int z; /* x and a the new y are here, along with a new z */
            }
            /* the new z is gone... */
        }
        /* and so on... */
        {
            int y;
            {
                int z;
            }
        }
        return 0;
    }

    Quzah.
    Hope is the first step on the road to disappointment.

  14. #14
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    To make things even more confusing you can even declare a new variable with the same name.

    Code:
    int main()
    {
      int x=3;
      {
        int x=5;
        printf("%d\n", x);  /* Prints 5 */
      }
      printf("%d\n", x); /* Prints 3 */
      return 0;
    }
    Oh the fun

  15. #15
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    No, it doesn't. You need <stdio.h>.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Client-server system with input from separate program
    By robot-ic in forum Networking/Device Communication
    Replies: 3
    Last Post: 01-16-2009, 03:30 PM
  2. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 12:27 PM
  3. IDEA: A basic drawing program
    By ygfperson in forum Contests Board
    Replies: 0
    Last Post: 08-12-2002, 11:15 PM
  4. Replies: 2
    Last Post: 05-10-2002, 04:16 PM