Thread: problem exiting program

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

    problem exiting program

    Hi, I'm writing a program for an assignment are that you are able to exit the program at anytime by typing exit. I have created a mockup where is passes the variable you want to check says exit into a function and if it says exit then another function is called which exits the program. This is the code below:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    int exit_check = 0;
    
    int continue_playing( char* );
    void end_game(void);
    
    int main( void )
    {
    	char username[22];
    	do
    	{
    	printf ("Please Enter Your Username ");
    	fgets(username, 20, stdin);
    	continue_playing(username);
    	}
    	while(exit_check == 0);
    }
    
    int continue_playing( char* checkvar )
    {
    	if ((strncmp(checkvar, "EXIT", 1) == 0)||(strncmp(checkvar, "exit", 1) == 0)||(strncmp(checkvar, "Exit", 1) == 0)){
    		end_game();
    	}
    	return (exit_check == 0);
    }
    
    void end_game( void )
    {
    	exit_check = 1;
    }
    However when I include this with my main code it compiles fine but doesn't work. The important parts of the code are shown below:

    Code:
    int exit_check = 0;
    int send_code( char*, int*, int*, int*);
    int continue_playing( char* );
    void end_game(void);
    
    main(int argc, char *argv[])
    {
    		
    	/* Create the variables which i will need to use */
    	char help[5];
    	char username[22];
    	char guess[42];
    	int i;
    	int formattedguess[4]= {0};
    	int valid = 0;
    	int serveroutput[4] = {0};
    	char formattedoutput [4];
    	int gameno;
    	int lastgameno = 3;
    	int status;
    	
    	
    	/* Welcome Message */
    	printf("---Welcome to Mastmind--- \n");
    	printf("Please enter your name: ");
    	/* fgets ensures the username entered is only 20 characters long */
    	fgets(username, 20, stdin);
    	continue_playing(username);  <----THIS IS WHERE IT CALLS THE FUNCTION
            printf("\nHello %s", username);
    
    int continue_playing( char* checkvar )
    {
    	if ((strncmp(checkvar, "EXIT", 1) == 0)||(strncmp(checkvar, "exit", 1) == 0)||(strncmp(checkvar, "Exit", 1) == 0)){
    		end_game();
    	}
    	return (exit_check == 0);
    }
    
    
    void end_game( void )
    {
    	exit_check = 1;
    }
    Can anyone see where im going wrong. When exit is entered it just goes onto the next line of code.

    Many Thanks

  2. #2
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    fgets takes the newline charater when you hit return too. So you should either remove it or do your strcmps something like:
    Code:
    if(strcmp(checkvar, "EXIT\n") == 0)
    That might fix it.

    Edit: also passing 'username' as the 'checkvar' seems confusing.

  3. #3
    Registered User
    Join Date
    Mar 2008
    Posts
    24
    I've just tried adding \n to the strncmp but it didn't make a diffrence. The reason it changes username to checkvar is becuase i will use the function to check variables other than username later in the code.

    Thanks anyway, anyone else have any ideas???

  4. #4
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    Oh I see you are using strncmp. You should be aware that your code only checks the first letter of the word.

    I dont quite understand what you are doing here:
    Code:
    return (exit_check == 0);
    And since exit_check is global there would be no need to return it from a function anyway. I'd just remove that line.

  5. #5
    Registered User
    Join Date
    Mar 2008
    Posts
    24
    I have changed the strncmp to check 4 letters but this has made no diffrence. I have also removed that line and it still has no effect, i'm attaching the full code to see if it would help.

  6. #6
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    I suspect your problem is expectations .....
    Code:
            fgets(username, 20, stdin);
    	continue_playing(username);  <----THIS IS WHERE IT CALLS THE FUNCTION
            printf("\nHello &#37;s", username);
    is guaranteed to print out username when continue_playing() returns, particularly as continue_playing() is guaranteed to return. Setting the value of exit_check does withing continue_playing() (which calls end_game()) does not change that ... particularly as your code never tests the value of exit_check.

    The only thing your code does with exit_check is initialise it, and later set the value to 1. There is no other code that checks what the value of exit_check is, or does anything depending on its value.

    Compilers are not omnipotent enough to work out that a program should exit simply because a variable named exit_check is set to 1. You have to write code to check the value of exit_check.

  7. #7
    Registered User
    Join Date
    Mar 2008
    Posts
    24
    ok i have made some changes, and from what I understand a C program will close when it gets to the end of the main function. The exit check is used in a loop around the program which carry's on so long as it's set to 0. What I expect to happen is when this is set to 1 it will stop. This works in the mock up which I have made but not in my code. Mockup:
    Code:
    int exit_check = 0;
    
    int continue_playing( char* );
    void end_game(void);
    
    int main( void )
    {
    	char username[22];
    	do
    	{
    	printf ("Please Enter Your Username ");
    	fgets(username, 20, stdin);
    	continue_playing(username);
    	}
    	while(exit_check == 0);
    }
    
    int continue_playing( char* checkvar )
    {
    	if ((strncmp(checkvar, "EXIT", 1) == 0)||(strncmp(checkvar, "exit", 1) == 0)||(strncmp(checkvar, "Exit", 1) == 0)){
    		end_game();
    	}
    	return (exit_check == 0);
    }
    
    void end_game( void )
    {
    	exit_check = 1;
    }
    I have attached the actual code, can anyone see why this works in the mockup but not in the real thing???

  8. #8
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    It's just as grumpy said (maybe that's why he's grumpy...too many people don't listen)
    Code:
    int main(void)
    {
       int exit_routine = 0;
       do { 
          int i = 0;
          exit_routine = 1;
          for (; i < 10; ++i)
             printf ("La di da\n");
       } while(exit_routine == 0);
    }
    Think about what you expect that to do, then compile it, execute it, and note what it does.

  9. #9
    Registered User
    Join Date
    Mar 2008
    Posts
    24
    ok so it never actualy stops. So is there something wrong with my loop then, I dont understand why it works in the mock up and not in my main code, can anyone explain this.

    Thanks
    Last edited by sweetorangepie; 03-01-2008 at 12:05 PM.

  10. #10
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    The while condition is not checked until the end of the do block. Like grumpy said, you need to check the value of exit_check after your call to continue_playing() and break out of the do...while loop if it's not 0.

  11. #11
    Registered User
    Join Date
    Mar 2008
    Posts
    24
    oh fair enougth I see, where should I put the break, should this go in the end game function as the program goes there if exit has been typed???

    Thanks for your help, its much appriciated.

  12. #12
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Don't use globals unnecessarily. exit_check should be a local in main, and should accept the return value from continue_playing:
    Code:
    int continue_playing( char* );
    
    int main( void )
    {
    	char username[22];
    	int exit_check = 0;
    	do
    	{
    		printf("Please Enter Your Username ");
    		fgets(username, 20, stdin);
    		exit_check = continue_playing(username);
    	}
    	while (exit_check == 0);
    }
    Now, why does this function even exist? It's so short that you may as well just inline it yourself, replacing the call to end_game(); with exit_check = 1;
    Then take a look of the effect of doing that. The only thing it changes is that continue_playing would then return true. So why not just simply return true explicitly at that point in continue_playing:
    Code:
    void end_game( void )	// delete me
    {			// delete me
    	exit_check = 1;	// delete me
    }			// delete me
    Code:
    int continue_playing( char* checkvar )
    {
    	if ((strncmp(checkvar, "EXIT", 1) == 0)||(strncmp(checkvar, "exit", 1) == 0)||(strncmp(checkvar, "Exit", 1) == 0)){
    		return 1;
    	}
    	return 0;
    }
    Then you can go even futhur and realise that the expresion in the if statement is already a boolean expression, and you could simply return that boolean result directly instead of beating around the bush:
    Code:
    int continue_playing( char* checkvar )
    {
    	return ((strncmp(checkvar, "EXIT", 1) == 0)||(strncmp(checkvar, "exit", 1) == 0)||(strncmp(checkvar, "Exit", 1) == 0));
    }
    Next you should find a case insensitive function to do the comparison so that it'll work for eXiT etc as well. I'll let you carry on with that part.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  13. #13
    Registered User
    Join Date
    Mar 2008
    Posts
    24
    thanks for the help with that and I understand how that would work and have added the changes that you have suggested but it still doesn't work, it just goes to the next line of the code. Do you know why this is?

    Thanks

  14. #14
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Reread my earlier reply, and grumpy's reply to which it refers. Really READ them and UNDERSTAND them...it's NOT rocket surgery!!!

  15. #15
    Registered User
    Join Date
    Mar 2008
    Posts
    24
    ok I understand now, sorry about my noobyness, I think I can get it to work now, is there anyway of breaking out of a loop while in another function???

    Thanks Guys, Appriciate it

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. problem w/ doubles in friend's program
    By mkylman in forum C Programming
    Replies: 16
    Last Post: 11-22-2008, 10:45 AM
  2. Program Termination Problem
    By dacbo in forum C Programming
    Replies: 3
    Last Post: 01-23-2006, 02:34 AM
  3. I have finished my program, one problem
    By sloopy in forum C Programming
    Replies: 4
    Last Post: 11-29-2005, 02:10 AM
  4. Console Program Problem
    By Breach23 in forum C++ Programming
    Replies: 3
    Last Post: 10-19-2001, 12:35 AM