Thread: Beginner Needing Help

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

    Exclamation Beginner Needing Help

    Hi There,
    I'm A C Progamming begginer and have never done any c programming at all.

    I'm using the great C tutorials on this site, but ive come across a problem.

    I've got to Here
    and im on the "Else If" section.

    Code:
    #include <stdio.h>
    
    int main()
    {
        int age;
        printf( "Please Enter Your Age\n" );
        scanf( "%d", age );
        if ( age < 100 ) {
             printf( "Wow Your Pretty Young !\n" );
             }
             else if ( age == 100 ) {
                  printf( "Your Really Old\n" );
                  }
                  else {
                       printf( "Your Really Old And Crinkley\n" );
                       }
                       return 0;
                       }
    I've typed that code from the "Else If" section into my compiler (Dev-C++) and it compiles fine, but the problem is, when you enter your age and press "Enter" the program preforms Illegal Operation and Closes....

    Please Help Me With This Problem

  2. #2
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    Code:
    #include <stdio.h>
    
    int main(void)
    {
            int age;
            printf("Please Enter Your Age\n");
            scanf("%d", &age);
            if (age < 100) {
                    printf("Wow Your Pretty Young !\n");
            } else if (age == 100) {
                    printf("Your Really Old\n");
            } else {
                    printf("Your Really Old And Crinkley\n");
            }
            return 0;
    }
    Try adding the & operator as indicated.

    edit::

    If one of the mods is reading this, perhaps we could get that tutorial corrected - there should be some error checking on scanf() as well.
    Last edited by kermit; 08-08-2005 at 04:23 AM.

  3. #3
    Registered User
    Join Date
    Aug 2005
    Posts
    2
    Thank you Very much

    Code:
    #include <stdio.h>
    
    int main(void)
    {
        int age;
        printf( "Please Enter Your Age\n" );
        scanf( "%d", &age );
        if ( age < 100 ) {
             printf( "Wow Your Pretty Young !\n" );
             }
             else if ( age == 100 ) {
                  printf( "Your Really Old\n" );
                  }
                  else {
                       printf( "Your Really Old And Crinkley\n" );
                       }
                       getchar();
                       return 0;
                       }
    I've added the getchar(); witch in the tutorial on C, says that it should halt the program and wait for the user to press enter....but when i put getchar(); in this code, it doesnt work and won't pause the program.

    Also when i enter a number over 100 it Closes without saying "Your Really Old And Crinkley"

    I've only started learning C yesterday so i don't know if the error is through something i've done, or wether the compiler is compiling wrong So i really need help in these 1st few stages to get me used to the code
    Last edited by MadProfessor; 08-08-2005 at 04:04 PM.

  4. #4
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    The problem is that scanf() leaves a '\n' in the input buffer from when you hit ENTER after typing the age. So when you call getchar() it reads that '\n' immediately and continues on. There's information in the FAQ about scanf() and clearing the input buffer. For a quick and dirty fix you can just do 2 getchar(); calls in a row to make it wait for you to press ENTER before exiting.
    If you understand what you're doing, you're not learning anything.

  5. #5
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    Quote Originally Posted by MadProfessor
    Thank you Very much


    I've added the getchar(); witch in the tutorial on C, says that it should halt the program and wait for the user to press enter....but when i put getchar(); in this code, it doesnt work and won't pause the program.
    I would imagine that your call to scanf() left the newline in the stream, and is waiting there when you make your call to getchar(). Welcome to the world of C programming. You are starting to learn the things that happen when you start mixing calls to scanf() and getchar(). In order to clear the stdin, you could do something like the following:

    Code:
    #include <stdio.h>
    
    int main(void)
    {
            int age;
            int c;
            printf("Please Enter Your Age\n");
            scanf("%d", &age);
            while ((c = getchar()) != '\n' && c != EOF) {
                   ;
            }
            if (age < 100) {
                    printf("Wow Your Pretty Young !\n");
            } else if (age == 100) {
                    printf("Your Really Old\n");
            } else {
                    printf("Your Really Old And Crinkley\n");
            }
            getchar();
            return 0;
    }
    Of course, as you learn more ways to get numbers from the user, you may wish to just leave scanf() alone.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Same old beginner question...
    By Sharmz in forum C Programming
    Replies: 15
    Last Post: 08-04-2008, 11:48 AM
  2. Best Free Beginner Online Books/Tutorials?
    By Zeusbwr in forum C++ Programming
    Replies: 2
    Last Post: 10-12-2004, 05:52 PM
  3. Windows programming for beginner (Absolute beginner)
    By WDT in forum Windows Programming
    Replies: 4
    Last Post: 01-06-2004, 11:21 AM
  4. Beginner needing help in C language
    By chauncey005 in forum C Programming
    Replies: 2
    Last Post: 09-26-2003, 02:04 PM