exit a program at any point.

This is a discussion on exit a program at any point. within the C Programming forums, part of the General Programming Boards category; hello i have been trying to write a code that allows me to exit a program at any point during ...

  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    2

    exit a program at any point.

    hello i have been trying to write a code that allows me to exit a program at any point during that program by typing the word exit.

    you will have to bare with me i am extremely new to this.

    Code:
     
    #include <stdio.h>
     
    int main(void) 
    {
         char x[20];
         char person[20];
    
     
         printf("Please type your name: ");
         scanf("%s", person);
         
         if ((person) == "x")
         {
         printf();
         exit(1);
         }
         
         
     
         printf("After reading the January 1, issue of that demonstrated the Altair 8800, %s called the creators of the new\n", person);
     
    }
    this is what i have come up with so far, any suggestions on why this is not working?

  2. #2
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,411
    Reading strings with scanf is dangerous, so read my signature for more information about it.
    Second, you can't compare strings with == in C. Use strcmp.
    You're trying to call printf with no arguments, which won't compile.
    And lastly, you shouldn't exit the program just about anywhere. The program logic should actually say that it failed, so cleanup can be done and appropriate errors can be printed before returning to main which can exit the program. Don't use exit.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    Mar 2008
    Posts
    2
    ok thankyou for the help ill see what i can get done and repost

  4. #4
    Internet Superhero
    Join Date
    Sep 2006
    Location
    Denmark
    Posts
    964
    Quote Originally Posted by Elysia View Post
    The program logic should actually say that it failed, so cleanup can be done and appropriate errors can be printed before returning to main which can exit the program. Don't use exit.
    Don't use exit()? As in don't use it at all? What other alternatives are there?
    How I need a drink, alcoholic in nature, after the heavy lectures involving quantum mechanics.

  5. #5
    uint64_t...think positive xuftugulus's Avatar
    Join Date
    Feb 2008
    Location
    Pacem
    Posts
    355

    Lightbulb

    Quote Originally Posted by Neo1 View Post
    Don't use exit()? As in don't use it at all? What other alternatives are there?
    Code:
    int main()
    {
        if(some_error)
        {
             perror("ERROR!");
             return 1;
        }
    }
    Code:
    ...
        goto johny_walker_red_label;
    johny_walker_blue_label: exit(-149$);
    johny_walker_red_label : exit( -22$);
    A typical example of ...cheap programming practices.

  6. #6
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,411
    Quote Originally Posted by Neo1 View Post
    Don't use exit()? As in don't use it at all? What other alternatives are there?
    Avoid using it except for in some very special situations.
    Remember that main should be the one that terminates the program.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. For the numerical recipes in C types!
    By Smattacus in forum C Programming
    Replies: 5
    Last Post: 10-28-2008, 07:57 PM
  2. program crashes on exit
    By thestien in forum C++ Programming
    Replies: 19
    Last Post: 05-10-2008, 08:35 AM
  3. Class warfare
    By disruptivetech in forum C++ Programming
    Replies: 13
    Last Post: 04-22-2008, 01:43 PM
  4. Replies: 7
    Last Post: 05-20-2005, 02:48 PM
  5. How to exit the program?
    By bigben in forum C++ Programming
    Replies: 4
    Last Post: 05-31-2002, 09:06 AM

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