newbie question on scanf( )

K

Kelly B

I would like to put the routine below in a function and call it whenever
i call scanf (in a code where i need multiple scanfs with different
number of arguments) I was wondering if it was possible to just pass the
number of arguments to the function and the function could parse the
desired no of arguments for me?
For example
if(num==1)
scanf()parses one integer
if(num==2)
scanf() parses two integers an so on...


#include<stdio.h>
#include<stdlib.h>

int main(void)
{
int test,res,ch,res1;
test=scanf_s("%d %d",&res,&res1);
while(test!=2)
{
while (('\n' != (ch = getchar())) && (EOF != ch));
puts("Enter an integer value");
test=scanf_s("%d %d",&res,&res1);
}
while (('\n' != (ch = getchar())) && (EOF != ch));

return 0;
}
 
K

Kelly B

Kelly said:
#include<stdio.h>
#include<stdlib.h>

int main(void)
{
int test,res,ch,res1;
test=scanf_s("%d %d",&res,&res1);
while(test!=2)
{
while (('\n' != (ch = getchar())) && (EOF != ch));
puts("Enter an integer value");
test=scanf_s("%d %d",&res,&res1);
}
while (('\n' != (ch = getchar())) && (EOF != ch));

return 0;
}
I am using Visual Studio and hence the scanf_s (The function declared
deprecated warning is annoying..)
This is the proper code :)

#include<stdio.h>
#include<stdlib.h>

int main(void)
{
int test,res,ch,res1;
test=scanf("%d %d",&res,&res1);
while(test!=2)
{
while (('\n' != (ch = getchar())) && (EOF != ch));
puts("Enter an integer value");
test=scanf("%d %d",&res,&res1);
}
printf("\n%d %d",res,res1);
return 0;
}
 
C

CBFalconer

Kelly said:
#include<stdio.h>
#include<stdlib.h>

int main(void)
{
int test,res,ch,res1;
test=scanf_s("%d %d",&res,&res1);
while(test!=2)
{
while (('\n' != (ch = getchar())) && (EOF != ch));
puts("Enter an integer value");
test=scanf_s("%d %d",&res,&res1);
}
while (('\n' != (ch = getchar())) && (EOF != ch));
return 0;
}

I am using Visual Studio and hence the scanf_s (The function
declared deprecated warning is annoying..)

Don't bother. The warning is false.

--
<http://www.cs.auckland.ac.nz/~pgut001/pubs/vista_cost.txt>
<http://www.securityfocus.com/columnists/423>
<http://www.aaxnet.com/editor/edit043.html>

"A man who is right every time is not likely to do very much."
-- Francis Crick, co-discover of DNA
"There is nothing more amazing than stupidity in action."
-- Thomas Matthews
+
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,260
Messages
2,571,038
Members
48,768
Latest member
first4landlord

Latest Threads

Top