My first C-program - scanf() does not what I want...
I'm trying to learn C, and writing my first small program. I have this function (in a bigger program):
Code:
char player_move(char *raw_data, char player){ Code:
X Go to: |
That's why you shouldn't use scanf. fgets and sscanf are you friends. But do check the return value of (s)scanf, it is important.
|
Quote:
I did some googling for sscanf and fgets, but I get the impression they are for strings. How do I convert them to integer? I'm completely new at this... |
Quote:
scanf only reads what fits the specification you gave it. It can get stuck on something that doesn't fit and keep reading nothing, because nothing valid precedes the unreadable pending input. When the user has given you wrong input, you should first flush the pending input, then display a prompt for the user to try again, then try scanf again. |
Quote:
That makes your program automatically aware of the line boundaries in the input. If you don't do that, then any scanf you try might be trying to read something from remains of a line that was typed for an earlier scanf. Flushing any pending input after each answer (before the next prompt) is an alternative that lets you address the same problem without using fgets+sscanf. But that might mess the user up if the user types ahead, and it really messes you up if the input is coming from a script rather than an interactive user. You can't read the user's mind, so there is no 100% bullet proof way to find where one answer ends and the next begins. Give some thought to what behaviors you want for which cases of correct and incorrect user input. The model that each line is one answer, is a pretty good model and combining fgets+sscanf rather than directly using scanf is an easy way to force one line equals one answer. Flushing pending input after any wrong answer, but not after a correct answer, is another choice that is often a good fit to the range of situations you would like to cover. |
> How do I check the return value?
eg: Code:
if (sscanf (str, "%d", &n) != 1) { /* failed */ fgets reads a line as a string, then sscanf parses that string |
Quote:
There is plain old scanf(), which reads from standard input (normally your console/keyboard). Then there is sscanf(), which reads from the string variable that is specified in its first argument, rather than scanning stdin like its cousin scanf(). There is also the sibling fscanf(). Like sscanf(), it has an additional first argument, a FILE *, allowing you to specify an open file from which to scan input. All three forms of the scanf() family obey the same rules for the use of the format string and conversion of input character data to other data types. Don't wory about it for now, but there is also the vscanf(), vsscanf(), and vfscanf() functions with the same consistent nature. For output, the printf() family has the same consistent nomenclature. --- rod. |
I agree with the above. I know they always teach C programming students to use scanf, but avoid it if you value your sanity. Use fscanf+sscanf. Read a string with fscanf from stdin, then parse it using sscanf. You can also use fgets instead of fscanf, it's your choice.
|
After som more reading, and some trial and error I now have this function:
Code:
char player_move(char *raw_data, char player){ But at the same time, this revealed another problem. I had scanf() in the main function, wich seemed to work before, but for some reason it started to behave strange after this function was fixed. So I could need some help to understand how to fix that one also. This is what I have now, but "it doesn't work" - if I enter 'y' it doesn't begin another game, but the program stops with exitcode 0. Code:
int main(void) { Code:
tictactoe.c:29:19: warning: comparison between pointer and integer [enabled by default] |
Quote:
If it is a char, you need to parse it with %c, not %s and compare it to 'y' not "y" If again were a char array used as a null terminated string, you could parse it with %s, but when you compare it to "y", you would need to use strcmp, not == |
Thanks, I think I got it working now.
The relevant part of the code: Code:
char input[512]; |
All times are GMT -5. The time now is 06:17 AM. |