PDA

Click to See Complete Forum and Search --> : Dos Commands


Help
Jan 11th, 2001, 05:56 PM
Hello,

I am pretty new to C programming and I would like to know how to do the following.

User types in Ip addy and Then pings ip. Here is my code now. I know all basic commands, but the system() isnt allowing two parameters. So thats where im lost.


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

void main(void) {
float ip;

printf("Enter in ip addy: ");
scanf("%f",&ip);
system("ping %f", ip);

}

Any suggestions ?
Thanks

HarryW
Jan 11th, 2001, 05:59 PM
Are you sure an IP address can be expressed as a floating point number? I would have thought you would need a string for that.

Help
Jan 11th, 2001, 06:03 PM
Okay how about this.
But it still errors on the two parameters.

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

void main(void) {
char ip[50];

printf("Enter in ip addy: ");
fgets(ip,sizeof(ip),stdin);
sscanf(ip, "%s", ip);
system("ping %f", ip);

}

HarryW
Jan 11th, 2001, 06:23 PM
I think you have the wrong idea of format strings - you can't use them in every function, only some. Use sprintf() to create the string first and then send it to system(), like this:


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

void main(void)
{
char ip[50];

printf("Enter in ip addy: ");
fgets(ip,sizeof(ip),stdin);
sscanf(ip, "%s", ip);
sprintf(ip, "ping %s", ip);
system(ip);

}

Help
Jan 11th, 2001, 06:35 PM
See I learn something new every day.
I never knew about sprintf().
:)

Although it does not error , when it runs it just returns "unknown host command".
But at least now im on the right track :)

Thanks Harry

HarryW
Jan 11th, 2001, 06:37 PM
No problem :)