Hello, I am a novice in C language and have been trying to find a method of reading/scanning input from GSM, then comparing it against a text (e.g OK or ERROR). Then if true set a break. I have tried scanf with no avail. I have also tried reading string which works but am unsure how to compare with my text.Has anyone any advice with what function would be useful? Any replies greatly appreciated. Andy
I tried using the section of code below to read a string and then compare it. I get 3 warnings and this may be why I am not getting correct results. If i type ok press carriage return the result will be zero which is what I am looking for. If i type anything else it is not zero which is fine but when i then try to type OK it still gives error. Any ideas if this is due to warnings? I may try _getchar , is this a better method of getting input? { char arr[50]; printf("Enter up to 50 characters, with spaces\n"); gets(arr); printf("\nInput read into array was:\n"); puts(arr); } { char *string[1] = {"OK"} ; int result; char *arr = puts(arr); result = strcmp(string[1], puts(arr)); printf("Result comparing OK to input is %d\n", result); }
Should:
result = strcmp(string[1], puts(arr));
result = strcmp(string[0], puts(arr));
"Any ideas if this is due to warnings?" As you never said what warnings they are, it's impossible to tell! (unless you're telepathic)
Warnings are; warning 96: 'gets' too few actual parameters warning 40: 'int' converted to near pointer get warning 40 twice.
I think the following will do what you want: char arr[5]; int result; gets(arr,4); //Note non-standard Keil gets() puts(arr); result=strcmp("Ok",arr); printf("Result=%d\n",result); There are many mistakes in the code you posted - you really need to work on your 'C' a bit with a good book. Stefan
Thankyou very much for taking the time to help me. I have several books on C I have got out of uni and have been using the PDF files. Do you recommend any books that are more Keil specifc? Thanks again
Take a look at: http://www.hitex.co.uk/c51primer/c51primer.html Stefan