I have the following code:
struct punto richiedi_punto () {
static int count=1;
struct punto point;
do {
printf("Inserire coordinate del punto %i:", count);
scanf("%d;%d",&point.x,&point.y);
} while (point.x<0 || point.x>9 || point.y<0 || point.y>9);
count++;
return point;
}
Gcc doesn't find errors, but I get this warning:
Warning: ignoring return value of ‘scanf’, declared with attribute warn_unused_result [-Wunused-result]
I tried to find out a solution on google, but I didn't understand what causes this warning.
Thanks in advance.
EDIT: I noticed just now that if I run my program in MonoDevelop console, I cannot insert my coordinates (why?), but if I run it in gnome-terminal it works normally.
scanf()
returns the number of fields successfully converted, for you to check
int fields;
do {
printf("Inserire coordinate del punto %i:", count);
fields = scanf("%d;%d",&point.x,&point.y);
} while (fields != 2 || point.x<0 || point.x>9 || point.y<0 || point.y>9);
As @chux points out, the above is not good. Here is a version using sscanf
instead of scanf
.
#include <stdio.h>
int main()
{
int fields, x, y;
char inp[100];
do {
printf("Inserire coordinate:");
fgets(inp, 100, stdin);
fields = sscanf(inp, "%d;%d",&x,&y);
} while (fields != 2 || x<0 || x>9 || y<0 || y>9);
printf("x=%d, y=%d\n", x, y);
return 0;
}