So I was given this function to read options to perform. I understand the execution of the prompts but I am having trouble passing the 'c' back to the program. I figure it has something to do with pointers I have yet to grasp.
int nextCommand(int *x, int *y)
{
char c;
while(1){
scanf("%c", &c);
if (c == 's' || c == 'p') break;
if (c == 'f' || c == 'F') {scanf("%d", x); break;}
if (c == 'u') {scanf("%d", x); scanf("%d", y); break;}
}
return c;
}
To actually describe my question. When I call this function the program is at a standstill. I try to enter one of the appropriate commands (i.e. s or p) and the program ends. Sorry I put this question up 开发者_StackOverflow中文版at the end of a long night.
what is the use of scanf of x and y? the program seems to be correct. What is the problem you are encountering? can you give details of what problem are you facing?
The function simply returns a char (i.e. 1 byte of data on many platforms, but not all) to whoever called it.
So for example in your main function:
int main(int argc, char **argv)
{
int a = 0, b =0;
char result;
result = nextCommand(&a, &b);
/* at this point result will contain the single character 's', or 'u', or... etc. */
return 0;
}
精彩评论