Im writing a program and in it I ask the user to enter a certain ammount of letters seperated by a space (i.e: "a b c"). How can I do this while making sure the input is valid (separated by a space)?
thanx
Printable View
Im writing a program and in it I ask the user to enter a certain ammount of letters seperated by a space (i.e: "a b c"). How can I do this while making sure the input is valid (separated by a space)?
thanx
How about reading one character at a time and checking alternately for a letter and then a space?
Quzah.
isnt there a way to format scanf and then check the return value?
something like :
res = scanf("/*whatever*/", ***);
Well you would use fgets() to read a line, then sscanf to validate it.
I suppose something like
Code:char buff[BUFSIZ], t1[BUFSIZ], t2[BUFSIZ], t3[BUFSIZ]
fgets( buff, BUFSIZ, stdin );
if ( sscanf( buff, "%s %s %s", t1, t2, t3 ) == 3 &&
strlen( t1 ) == 1 && strlen( t2 ) == 1 && strlen( t3 ) == 1 ) {
}