You aren't concise with your problem. This:
int main( void ) {
int a=0, b=0;
printf("Intervaly: \n");
while(scanf("<%d;%d>",&a,&b) == 2){
printf("Ruznych kvadru: %d\n", capacity(a, b));
}
return 0;
}
does in fact what you want, either with a long line of <%d;%d>
or line by line - both will work. Your problem, hidden in the comments, that an EOF does not terminate this while immediately - rather you need to CTRL-D twice to stop it. The reason for that is also in the comments.
Once you scanf the way you do, you are going to be left with a \n
in the input stream (because you have to press return to send your input without terminating). There are two ways to deal with this - add it in the scanf
, which is less flexible and has some problems, or clear it in the while:
while(scanf("<%d;%d>",&a,&b) == 2){
printf("Ruznych kvadru: %d\n", capacity(a, b));
getchar();
}
You will find now you need to press CTRL-D once. If you tried input redirection from a file you would have seen this problem didn't exist either. Why does it happen? You had \n
in the input stream. You press EOF, so scanf gets a newline and ignores it - waiting for new input. Only now a new EOF will kill the scanf
.
I suggest searching this site for scanf
questions to see why not to use it, and other nice alternatives.
Addendum
A more elegant solution given in the comments and pointed out to me by @BluePixy and pointed out to me by @DavidBowling is to use for your scanf
:
scanf("<%d;%d>%*c", &a, &b);
This will naturally gobble up the extra characters, including the new space, without the need for an extra getchar
. This I think is the most elegant solution to this scanf
usage.
Even more
Following the comments by OP and again by David (thanks again!) in order to account for 0 or more spaces, you just need a space directive in the formatting:
scanf("< %d ; %d >%*c", &a, &b);
Now spaces may (or may not) be added around each integer.