From an input of n numbers(value of n is known) separated by spaces, eg(here n is 6): 3 5 8 9 13 2
If I wish to accept only the 2nd and the 5th number and ignore the rest, how do I do it using scanf?
I found accepting the numbers in an array and using only the required ones a bit redundant, so I'm looking for a smarter alternative.
Try this
int num,number1,number2;
for(int i = 0; i < 6; i++)
{
scanf("%d", &num);
if(i == 1)
number1 = num;
if(i == 4)
number2 = num;
}
scanf("%*d%d%*d%*d%d%*d", &firstNumber, &secondNumber);
Try this.
%*d
reads the value, but ignores it in the name of good will.
If the knwon number n
is a fix number (n=6 always)
then you can use the following scanf
int a2, a5;
scanf("%*d %d %*d %*d %d %*d", &a2, &a5 );
I would read every number using scanf
. Read not required ones in some temp
variable and others in array or any variable (as required)
scanf("%*d %d %*d %*d %d %*d", &i, &j)
int v[6];
scanf( "%d %d %d %d %d %d", &v[0], &v[1], &v[2], &v[3], &v[4], &v[5] );
v[1]
and v[4]
contain the answers from your example.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.