#include <stdio.h>

int main()
{
    int age;

   printf("Please enter your age:");
   scanf("%d", &age);

   if(age<100)
   {
   printf("You are pretty young!!!\n");
   }
    else{
            if(age==100)
                {
                printf("You are old\n");
                }
                else {
                            if(age>100)
                        {
                            printf("You are really old\n");
                            }
                        }
          }
          getchar();
          return 0;
}                        

the program just flashes wen i click run and doesnt stay on screen. help please..

Recommended Answers

All 7 Replies

I compiled it, and ran it. It works fine for all three outputs. It works also without the 'getchar();' at the end. Compiler issue?

I compiled it, and ran it. It works fine for all three outputs. It works also without the 'getchar();' at the end. Compiler issue?

maybe its a compiler issue. becuz im using a 32bit version on a 64bit processor so its saying it might not work well on start up.

is there a 64bit verion anyway?

What are you using as an IDE? Always best to get the right architecture..

What are you using as an IDE? Always best to get the right architecture..

remember im a newbie plaease break that down for me ok

maybe its a compiler issue.

It is a normal result of stream I/O in C. The problem is that there is still a character left in the stream after calling scanf(), and getchar() eats it without blocking. The IDE you are using probably closes the window after main() returns, so the window goes away. Calling getchar() is supposed to stop that, but it only works if the stream is clean when getchar() is called.

You can clean the stream with repeated calls to getchar() until '\n' is found. This will produce the right behavior if the stream is not clean, and it will only require an extra [Enter] to be typed if the stream is already clean:

#include <stdio.h>

int main()
{
    int age;

    printf("Please enter your age:");
    scanf("%d", &age);

    if(age<100)
    {
        printf("You are pretty young!!!\n");
    }
    else
    {
        if(age==100)
        {
            printf("You are old\n");
        }
        else
        {
            if(age>100)
            {
                printf("You are really old\n");
            }
        }
    }

    /* clean the stream */
    while (getchar() != '\n') {}

    /* pause */
    getchar();

    return 0;
}

It is a normal result of stream I/O in C. The problem is that there is still a character left in the stream after calling scanf(), and getchar() eats it without blocking. The IDE you are using probably closes the window after main() returns, so the window goes away. Calling getchar() is supposed to stop that, but it only works if the stream is clean when getchar() is called.

You can clean the stream with repeated calls to getchar() until '\n' is found. This will produce the right behavior if the stream is not clean, and it will only require an extra [Enter] to be typed if the stream is already clean:

#include <stdio.h>

int main()
{
    int age;

    printf("Please enter your age:");
    scanf("%d", &age);

    if(age<100)
    {
        printf("You are pretty young!!!\n");
    }
    else
    {
        if(age==100)
        {
            printf("You are old\n");
        }
        else
        {
            if(age>100)
            {
                printf("You are really old\n");
            }
        }
    }

    /* clean the stream */
    while (getchar() != '\n') {}

    /* pause */
    getchar();

    return 0;
}

thanx man i appreciate the help

u no need to put open braces after else statement and wt s the need for getchar()

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.