Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

completely new to this... need little help.

Options
  • 08-07-2009 7:50pm
    #1
    Registered Users Posts: 6,053 ✭✭✭


    hey guys/girls.

    I'm starting computer science in september, trying to see what this programming lark is about beforehand.

    so, i downloaded dev-C++ from bloodshed or something.

    then i went in search of online tutorials for C, because this is what well be starting with next year.

    i found my way to cprogramming.com (not REALLY surprising), and joy of joys i made a start with a simple program, displaying what i typed into the compiler. All good so far.

    Now, when i moved on to slightly more advanced programs, with a bit of interaction, things got annoying. here is the program , comments from site included.

    #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;
    }

    this opens the same box as before, when i type in my age and click enter, the program closes.

    im aware this is very very basic, but any help would be massively appreciated.

    cheers guys.


Comments

  • Closed Accounts Posts: 580 ✭✭✭karlr42


    You're running on Windows? Then what's happening is that the program is running and printing whatever message, but the window is closed as soon as the program ends. I know the way around is to open a command prompt window and run the executable from there, don't just double click it.


  • Registered Users Posts: 2,297 ✭✭✭Ri_Nollaig


    if you put another scanf before the return 0 it will stay open till you hit enter again.


  • Registered Users Posts: 1,916 ✭✭✭ronivek


    Good to see you're not going to be one of the many who walk into Computer Science without the first clue of what it entails.

    A handy trick for keeping the window open so you can read the output is to add the following to the end of your main function;
        char exit;
        scanf("%c", &exit); 
    

    Just hit enter or indeed just about any key and the window will close. The program just sits there waiting for a character to be input from stdin; i.e. waits for you to hit a key on the keyboard and then allows the main function to exit and thus the window to close.


  • Closed Accounts Posts: 580 ✭✭✭karlr42


    Ri_Nollaig wrote: »
    if you put another scanf before the return 0 it will stay open till you hit enter again.
    That works, but it's a potential vulnerability and would make the app look like it had hung on other platforms. The best solution really is to open a command prompt(start->run->type "cmd" or just type "cmd" into the vista start menu), then cd to the directory the executable is in, and run it with (for example) "./a.out".
    See here for an excellent guide:
    http://sandbox.mc.edu/~bennet/cs220/running.html#win


  • Registered Users Posts: 2,379 ✭✭✭toiletduck


    this opens the same box as before, when i type in my age and click enter, the program closes.

    im aware this is very very basic, but any help would be massively appreciated.

    cheers guys.

    The guys above have explained it. My own personal way of keeping the window open is to add
    System("pause");
    

    at the end of the program.


  • Advertisement
  • Closed Accounts Posts: 33 JamesAlex


    Well you have you answer, but maybe you would like to download eclipse and give java a go, its much easier for beginners and would be more like whats covered in college.

    Well done on giving it a go before you start, if only more people did this.


  • Registered Users Posts: 1,006 ✭✭✭WithCheesePlease


    JamesAlex wrote: »
    Well done on giving it a go before you start, if only more people did this.

    Have to agree, fair play. Shame more people aren't encouraged to before walking in blind the first day. Ah well.


Advertisement