Skip to content

Commit 11ccda3

Browse files
committed
is_float check to threshold
1 parent 61c9561 commit 11ccda3

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

parser.c

+23
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,25 @@ int is_digit(char* str){
3838
return 1;
3939
}
4040

41+
int is_float(char* str){
42+
unsigned int i;
43+
char ch;
44+
int dot = 0;
45+
for(i=0; i<strlen(str);i++){
46+
ch = str[i];
47+
if(ch == '.'){
48+
dot++;
49+
}
50+
if(dot > 1){
51+
return 0;
52+
}
53+
if((ch != '.') && (ch < '0' || ch > '9')){
54+
return 0;
55+
}
56+
}
57+
return 1;
58+
}
59+
4160
/* interpret user input and call actions
4261
return num op or 0 if invalid command/error */
4362
int parse_command(char *command, Board *board, Curr_move move)
@@ -271,6 +290,10 @@ int parse_command(char *command, Board *board, Curr_move move)
271290
return 0;
272291
}
273292
threshold = atof(token);
293+
if(!is_float(token)){
294+
print_error_number("first");
295+
return 0;
296+
}
274297

275298
next = (strtok(NULL, " \t\r\n"));
276299
if(next != NULL)

0 commit comments

Comments
 (0)