Welcome to HBH! If you had an account on hellboundhacker.org you will need to reset your password using the Lost Password system before you will be able to login.

C++ Using System ()


ghost's Avatar
0 0

I recently found the ability to use the system () command in programs through an online tutorial however I am trying to figure out how to incorporate strings and intergers in the system command with the system functions. In this 30 second code I have quickly devised as an example, I am attempting to ping the ipaddress that the user has entered, the code is working however it displays an error which seems to mean it is not using the ipaddress i am entering. I could do with some help here guys :(


using namespace std;

string ipaddress;
string ping;

main(){
cout<<"Welcome to the pinger v1"<<endl;
cout<<"please enter an IP address"<<endl;
cin>> ipaddress;
cout<<"please enter ping to ping the IP address or again to re-enter an IP"<<endl;
cin>> ping;
if (ping == "ping"){ 
system ("ping ipaddress");
}
else if (cout<<"woah");
 {
        cout<<"please enter and IP address"<<endl;
cin>> ipaddress;
}
}

ghost's Avatar
0 0

you need stdlib :) also why not use sprintf to format it. I'm no c++ programmer though


ghost's Avatar
0 0

Ah, I really need to look into what commands require what librarys and i have never used the sprintf command before - guess i've got a lot to learn yet aye! :P


ghost's Avatar
0 0

It's basically the same has printf but it puts it in a char array which you could pass to system :)


ghost's Avatar
0 0

@wolfmankurd: he really doesn't need that.

@OP: is that supposed to be pseudocode or not? Because if not, then you really need to relearn a lot of things, heck, even if it is pseudocode you'll need to brush up on how the flow of a program works… and also brush up on your style (poor int and return being abandoned hurts me at my heart).

Oh and try to leave off the system command if possible. Some people get addicted to it and do disgusting things like system("pause")


ghost's Avatar
0 0

COM wrote:

Oh and try to leave off the system command if possible. Some people get addicted to it and do disgusting things like system("pause")

I was speaking to someone about not using system("pause"), but I couldn't come up with a good replacement to stop the window closing once the program exits in cmd. What would you recommend?


ghost's Avatar
0 0

wolfmankurd wrote: I was speaking to someone about not using system("pause"), but I couldn't come up with a good replacement to stop the window closing once the program exits in cmd. What would you recommend? cin.get();

or, if they're working with C only, then I think getchar();

I presume they've already tried this though and can't get it to stop and thus have turned to system("pause");, so I'll just go ahead and explain the reason as to why their program won't stop and what to do bout it.

If they read in with some standard cin for instance, then it's going to read in up to the return value, which'll be stuck in the buffer. Next time you try to read in, if you use something like cin.get() which isn't that needy and is satisfied with just a return value, it'll take that and be done. So this means that you can pause it with two cin.get() or two getchar() functions after each other. However, the better thing to get used to is to follow every cin or similar which you know leaves a return value in the buffer, with a cin.get(), getchar() or cin.ignore() so the buffer will always be clean and empty.

Hope that helps and explains it well enough, it's an easy beginner's mistake to make and I've had to explain it a billion times.

Edit: I assume of course that you meant "how to stop the window from closing once the program has reached its end", because once a program exits then there's obviously not much you can put into it that should stop it from closing. If you want the pretty black box to be there after the program's finished I recommend actually running it from there to begin with.


ghost's Avatar
0 0

That's what I suggested he wanted it to stay open so people who didn't know how to use command line could see the results of the program.