-
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
-
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.
-
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???
-
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.
-
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.
-
I suspect your problem is expectations .....
Code:
fgets(username, 20, stdin);
continue_playing(username); <----THIS IS WHERE IT CALLS THE FUNCTION
printf("\nHello %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.
-
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???
-
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.
-
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
-
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.
-
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.
-
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.
-
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
-
Reread my earlier reply, and grumpy's reply to which it refers. Really READ them and UNDERSTAND them...it's NOT rocket surgery!!!
-
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