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