Thread: quick question.

  1. #1
    Registered User
    Join Date
    Jan 2006
    Posts
    31

    quick question.

    im pretty new so this is probably really simple, but i cant figure it out. i enter in--------------
    Code:
    #include <stdio.h>
    
    int main()
    {
        int age;
    
        printf( "Please enter your age." );
        scanf( "%d", &age );
        if( age <=50 ) {
            printf( "damn your young." );
        }
        else if( age >=51 ) {
             printf( "your old." );
        }
    }
    and the first printf comes up fine but once i put in a number, and press enter, the window and program closes. then i even copy and pasted this from the tutorial site----
    Code:
    #include <stdio.h> 
    
    int main()                            /* Most important part of the program!
    */
    {
        int age;                          /* Need a variable... */
      
        printf( "Please enter your age" );  /* Asks for age */
        scanf( "%d", &age );                 /* The input is put in age */
        if ( age < 100 ) {                  /* If the age is less than 100 */
         printf ("You are pretty young!\n" ); /* Just to show you it works... */
      }
      else if ( age == 100 ) {            /* I use else just to show an example */ 
         printf( "You are old\n" );       
      }
      else {
        printf( "You are really old\n" );     /* Executed if no other statement is
        */
      }
      return 0;
    }

    and it did the same thing. after i put in a number, and hit enter, the program window closed. i dont know if its a programming error or compiler error. my source code is set to console application in C(sure of it, checked 3 times lol). can anyone please give me some help on how to make the entire program run?

  2. #2
    Sr. Software Engineer filker0's Avatar
    Join Date
    Sep 2005
    Location
    West Virginia
    Posts
    235
    You're using Windows, programming a console app, and double-clicking the icon for the executable. The system notices that this is a console application and creates a "console" window for it to run in. When the program exits, the window is destroyed, as it's no longer needed.

    There is an FAQ entry about this. I don't have the link, but I'm sure someone (Salem?) will be happy to point you at it.
    Last edited by filker0; 01-09-2006 at 04:00 PM. Reason: make console out of command
    Insert obnoxious but pithy remark here

  3. #3
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    Quote Originally Posted by filker0
    There is an FAQ entry about this. I don't have the link, but I'm sure someone (Salem?) will be happy to point you at it.
    Certainly, here it is.

    Quote Originally Posted by chasingxsuns
    Code:
    #include <stdio.h>
    
    int main(void) /* using void to specify no arguments */
    {
        int age;
    
        printf( "Please enter your age." );
        scanf( "%d", &age );
        if( age <=50 ) {
            printf( "damn you're young." );
        }
        else if( age >=51 ) {
             printf( "you're old." );
        }
        return 0;
    }
    Although the C99 standard specifies that it is okay not to have a return statement in main(), it is good practice to put one in anyway. It doesn't cost much, and it makes your code much, much more portable.

  4. #4
    Registered User
    Join Date
    Jan 2006
    Posts
    31
    ehh.. i tried the above code and it still did the same thing. i dont really understand the faq about this. is there a getch or any command that will work??

  5. #5
    Sr. Software Engineer filker0's Avatar
    Join Date
    Sep 2005
    Location
    West Virginia
    Posts
    235
    The jist of the FAQ is this:

    If you start a command prompt window, use the "cd" command to set your working directory to the directory that contains your program executable, then type the name of that executable at the command prompt, the window won't go away when the program terminates.
    Insert obnoxious but pithy remark here

  6. #6
    Registered User
    Join Date
    Jan 2006
    Posts
    31
    sorry to keep asking questions, but how do i start a command prompt window? like can you go through all the steps to use the command prompt so the window wont go away? lol i know this is bothersome, but i would really appreciate it.

  7. #7
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    I'm not familiar with the Windows environment, so can't help you with regard to command prompts, but one of the things the FAQ suggests is see the FAQ on waiting for a keypress.

  8. #8
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    For pretty much every version of Windows currently in use, you should be able to go Start -> Run, and type in "command" to get one. It may take a minute to load in NT-based versions. However, it's better to just write a program in such a way as to solve the problem then to expect your users to go to all that trouble.

  9. #9
    Registered User
    Join Date
    Jan 2006
    Posts
    31
    nothing on that faq works except the system pause thing. but that makes 'please press any button to continue...' come on, which i do not want. i need a way for this to work---
    Code:
    #include <stdio.h>
    
    int main()
    {
        int age;
    
        printf( "Please enter your age." );
        scanf( "%d", &age );
        if( age <= 50 ) {
            printf( "Your gay." );
        }
        else if( age>= 51 ) {
             printf( "Your cool." );
        }
    }

    i also tried ---

    #include <stdio.h>
    #include <conio.h>

    int main()
    {
    int age;

    printf( "Please enter your age." );
    getchar( "%d", &age );
    if( age <= 50 ) {
    printf( "Your gay." );
    }
    else if( age>= 51 ) {
    printf( "Your cool." );
    }
    }

    [/code]


    and---

    Code:
    #include <stdio.h>
    #include <conio.h>
    
    int main()
    {
        int age;
    
        printf( "Please enter your age." );
        scanf( "%d", &age );
        getchar();
        if( age <= 50 ) {
            printf( "Your gay." );
        }
        else if( age>= 51 ) {
             printf( "Your cool." );
        }
    }

    none worked. it all just closes the window after i put in an integer and hit enter.

  10. #10
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    In your last example, getchar grabs the newline leftover after you enter an integer. Add a second one to wait for new input.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  11. #11
    Registered User
    Join Date
    Jan 2006
    Posts
    31
    it worked!woot! thanks a lot dude. this has stumped me for a long time lol. thanks a lot.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Very quick math question
    By jverkoey in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 10-26-2005, 11:05 PM
  2. very quick question.
    By Unregistered in forum C++ Programming
    Replies: 7
    Last Post: 07-24-2002, 03:48 AM
  3. quick question
    By Unregistered in forum C++ Programming
    Replies: 5
    Last Post: 07-22-2002, 04:44 AM
  4. Quick Question Regarding Pointers
    By charash in forum C++ Programming
    Replies: 4
    Last Post: 05-04-2002, 11:04 AM
  5. Quick question: exit();
    By Cheeze-It in forum C Programming
    Replies: 6
    Last Post: 08-15-2001, 05:46 PM