quick termination of program

This is a discussion on quick termination of program within the C Programming forums, part of the General Programming Boards category; I am just learning c, and i was wondering how to stop my program from ending. The getchar () works ...

  1. #1
    Registered User
    Join Date
    Jan 2002
    Posts
    77

    Unhappy quick termination of program

    I am just learning c, and i was wondering how to stop my program from ending. The getchar () works fine with just the printf command, put what works with the scanf command?
    Hooked On Phonics Didn't Work For Me!

  2. #2
    Registered User
    Join Date
    Sep 2001
    Posts
    752
    If you really want a

    Press any key to continue...

    type thing, then you should check your compiler's keyboard or bios functions. If you need help with how to look up your compiler's documentation, just let us know what compiler you have.

    Once you know the function, it's easy enough to implement..
    Code:
    printf ("Press any key to continue...\n");
    for (;!kbhit(););
    Callou collei we'll code the way
    Of prime numbers and pings!

  3. #3
    Hamster without a wheel iain's Avatar
    Join Date
    Aug 2001
    Posts
    1,385
    this should work and is the method i use

    int wait = getch();
    Monday - what a way to spend a seventh of your life

  4. #4
    Registered User
    Join Date
    Jan 2002
    Posts
    77
    Well i tryed the getch(); but it doesnt work either and i use the software dev-c++.

    Here is the code--

    #include <windows.h>
    #include <stdio.h>
    #include <stdlib.h>

    int main ()
    {
    printf("current age?\n");
    scanf("%a");
    printf("%a year olds are dumb\n");
    getchar ();
    return 0;
    }

  5. #5
    Registered User
    Join Date
    Jan 2002
    Posts
    13

    Lightbulb

    jobolikescake,
    Try this code it should work for you.


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

    //prototypes (your functions)
    void pressKey();
    int getAge();

    //Global var
    int a;

    int main (){
    a = getAge();

    while(a < 1)
    a = getAge();

    if(a == 31){
    printf("You are %d years old. Hey we are the same age!\n",a);
    pressKey();
    return 0;
    }

    if(a < 31)
    printf("You are %d years old and you are still younger than me!\n",a);
    else
    printf("You are %d years old and you are now older than me!\n",a);

    pressKey();
    return 0;
    }


    int getAge(){
    clrscr();
    printf("\nWhat is your current age?\n");
    scanf("%d",&a);
    return(a);
    }

    void pressKey(){
    printf("\nHit any key to continue...");
    getch();
    }
    Thanks,
    Cyberpuck52

    "If you know the enemy and know yourself, you need not fear the result of a hundred battles"
    -Sun Tzu, Art of War

  6. #6
    Registered User
    Join Date
    Jan 2002
    Posts
    77
    I cant even get that to run. It gives me a linker error saying permission denied.

  7. #7
    Registered User
    Join Date
    Jan 2002
    Posts
    13
    What compiler are you using?

    C or C++?

    What OS?






    I'm using Borlands C/C++ on WIN ME. And it works fine for me. what was the error message?
    Thanks,
    Cyberpuck52

    "If you know the enemy and know yourself, you need not fear the result of a hundred battles"
    -Sun Tzu, Art of War

  8. #8
    Registered User
    Join Date
    Jan 2002
    Posts
    77
    i use a c compiler on windows xp
    Hooked On Phonics Didn't Work For Me!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program Termination Problem
    By dacbo in forum C Programming
    Replies: 3
    Last Post: 01-23-2006, 02:34 AM
  2. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 07:39 AM
  3. Visual C++ Program Termination
    By Padawan in forum C++ Programming
    Replies: 15
    Last Post: 11-26-2003, 11:05 PM
  4. Date program starts DOS's date
    By jrahhali in forum C++ Programming
    Replies: 1
    Last Post: 11-24-2003, 05:23 PM
  5. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 12:39 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21