Sinisterly
Need help in C ( Detect char) - Printable Version

+- Sinisterly (https://sinister.li)
+-- Forum: Coding (https://sinister.li/Forum-Coding)
+--- Forum: C, C++, & Obj-C (https://sinister.li/Forum-C-C-Obj-C)
+--- Thread: Need help in C ( Detect char) (/Thread-Need-help-in-C-Detect-char)



Need help in C ( Detect char) - chemicalrage - 03-16-2013

-------------------------


RE: Need help in C ( Detect char) - Deque - 03-16-2013

Read a char instead of int:

Code:
char ch; //... scanf(" %c", &ch); if(ch == '1') { //...



RE: Need help in C ( Detect char) - chemicalrage - 03-17-2013

-------------------------


RE: Need help in C ( Detect char) - Deque - 03-17-2013

I don't understand what the problem with the char is, but you can read the return code of scanf instead. If it is greater than 0, reading was successful.

Code:
int rc; rc = scanf("%d", &ch); if(rc > 0) { //success }

Look into the c reference for scanf to get more information: http://www.cplusplus.com/reference/cstdio/scanf/

Quote:On success, the function returns the number of items of the argument list successfully filled. This count can match the expected number of items or be less (even zero) due to a matching failure, a reading error, or the reach of the end-of-file.

If a reading error happens or the end-of-file is reached while reading, the proper indicator is set (feof or ferror). And, if either happens before any data could be successfully read, EOF is returned.

If an encoding error happens interpreting wide characters, the function sets errno to EILSEQ.