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
This is a discussion on reading chars with scanf within the C Programming forums, part of the General Programming Boards category; Im writing a program and in it I ask the user to enter a certain ammount of letters seperated by ...
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.
Hope is the first step on the road to disappointment.
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 ) { }
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
I support http://www.ukip.org/ as the first necessary step to a free Europe.