Exiting

This is a discussion on Exiting within the C Programming forums, part of the General Programming Boards category; I'm having a problem with my program exiting when I want it to. I want it to exit the program ...

  1. #1
    Registered User
    Join Date
    Mar 2003
    Posts
    25

    Exiting

    I'm having a problem with my program exiting when I want it to. I want it to exit the program completely if 'q' is pressed, yet it goes on to the 'menu' function even if 'q' is pressed....Can someone help me with getting the program to exit completely if q is pressed. Here is the code I have and any help would be appreciated.

    Code:
     	if (Filename!="junk.txt")
    	{
    		printf("File does not exist\n");
    		printf("Press (c) to create one, or (q) to quit: ");
    		scanf("%d",&answer);
    	switch(answer)
    	{
    		case 'c' : WriteRecordsToFile(Guests);break;
    		case 'q' : exit(0);
    	}
    	}
    	TotalRecords = ReadRecordsFromFile(Guests);
    	Menu(Guests);

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Code:
    		scanf("%d",&answer); /* expects answer to be an int */
    		/* 'q' is not successfully converted to an int */
    	switch(answer)
    	{
    		case 'c' : WriteRecordsToFile(Guests);break;
    		case 'q' : exit(0);
    	}
    I believe you want one of the following two, depending on answer's type.
    Code:
    int answer;
    answer = getchar();
    Code:
    char answer;
    scanf("%c",&answer);
    Both should check return values before continuing on to use answer. (This question is a result of not doing so.)
    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.*

  3. #3
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Try to read a value as a character ( %c ) instead.
    Are you sure you're not having CAPS LOCK pressed?
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  4. #4
    Registered User
    Join Date
    Mar 2003
    Posts
    25

    Should have known

    Thanks for your help. I should have know it was something simple...I've got it working now.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. simple crash problem (exiting func)
    By te5la in forum C++ Programming
    Replies: 2
    Last Post: 09-16-2008, 03:15 PM
  2. Exiting the program.
    By Taka in forum C Programming
    Replies: 6
    Last Post: 10-14-2007, 09:24 AM
  3. Exiting windows through a C program.
    By Afro in forum C Programming
    Replies: 3
    Last Post: 06-23-2007, 08:29 AM
  4. Ask user to save file before exiting.
    By Bajanine in forum Windows Programming
    Replies: 3
    Last Post: 11-15-2004, 05:34 PM
  5. prevent exiting
    By Unregistered in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 12-12-2001, 01:57 PM

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