jackpf Posted April 14, 2009 Share Posted April 14, 2009 Hi all, I'm trying to make a ping program in C++ using system(), pretty simple, but I can't seem to get the syntax right (I'm pretty new to C++). Here's what I've got. #include <iostream> using namespace std; int main() { printf("Hello world...welcome to jackpf's internet connection checker."); cout << endl << "Ping : "; string ping; cin >> ping; //const char * cmd = "ping "+ping; for(int i = 0; i == i; i++) { system("ping "+ping); } } I get the error error: cannot convert `std::basic_string<char, std::char_traits<char>, std::allocator<char> >' to `const char*' for argument `1' to `int system(const char*)' for the line that executes the system() command. Any help would be greatly appreciated. Thanks, Jack. Quote Link to comment https://forums.phpfreaks.com/topic/154077-solved-c-conversion-errors/ Share on other sites More sharing options...
corbin Posted April 15, 2009 Share Posted April 15, 2009 In C++, a string (as in the class std::string) is not simply an array of characters. When it comes down to it, it is, but it has a wrapper, dynamic allocation and so on. That said, you cannot concatenate an std::string to a const char* (which is what "ping " would be). The solutions: The easy way: Instead of a string, have ping be a char array. The problem? If the cin buffer size is > the size of the ping variable there will be a memory overflow. You could use a stream reading function to read a max byte size from cin like a stream. Or: You could use a different way to concatenate the two variables a different way. You could convert ping to a char array and then merge the two arrays, but I don't feel like explaining that, so you could do the easy way: string tmp = string("ping ").append(ping); system(tmp.c_str()); Quote Link to comment https://forums.phpfreaks.com/topic/154077-solved-c-conversion-errors/#findComment-810380 Share on other sites More sharing options...
jackpf Posted April 15, 2009 Author Share Posted April 15, 2009 You have my gratitude kind sir. Finished product #include <iostream> using namespace std; int main() { system("title Jackpf - Check Connection"); printf("Hello world...welcome to jackpf's internet connection checker."); cout << endl << "Ping : "; string ping; cin >> ping; ping = (strlen(ping.c_str()) >= 11) ? ping : "127.0.0.1"; string cmd = string("ping ").append(ping); for(int i = 0; i == i; i++) { system(cmd.c_str()); } } Quote Link to comment https://forums.phpfreaks.com/topic/154077-solved-c-conversion-errors/#findComment-810506 Share on other sites More sharing options...
corbin Posted April 16, 2009 Share Posted April 16, 2009 No problem. But uhmmm.... I have a question: for(int i = 0; i == i; i++) { system(cmd.c_str()); } Do you mean for that to be an infinite loop? If so, why not just use while(true). If not....? Also, if you're making an "Internet Connection Checker", you might should open a socket. That way you would know an actual connection is made, and you could check a specific port. Quote Link to comment https://forums.phpfreaks.com/topic/154077-solved-c-conversion-errors/#findComment-811177 Share on other sites More sharing options...
jackpf Posted April 16, 2009 Author Share Posted April 16, 2009 Do you mean for that to be an infinite loop? If so, why not just use while(true). Good point lol, I never thought of that. Just updated my code. And yeah...ideally. But as I said, I'm pretty new to C++. I've had a quick look on google...I can't really find anything explanatory about sockets though; only samples. You wouldn't happen to know any good tutorials would you? Yeah, you seem pretty knowledgeable about C++. Where/how did you start learning, may I ask? Quote Link to comment https://forums.phpfreaks.com/topic/154077-solved-c-conversion-errors/#findComment-811281 Share on other sites More sharing options...
corbin Posted April 21, 2009 Share Posted April 21, 2009 I'm quite mediocre at C++ actually ;p. Do you plan on using your app on *nix or Windows? Or both? If you plan on using it solely on Windows, you could just use Winsock. On linux, there's a winsock-ish type library. If you plan on developing for Windows and Linux, I suggest using a library or framework unless you feel like coding compatibility stuff. You could use asio from the Boost library for example. (It's designed for asynchronous use, and the learning curve for Boost can be pretty steep. That was just the first thing that came to mind.) Quote Link to comment https://forums.phpfreaks.com/topic/154077-solved-c-conversion-errors/#findComment-815149 Share on other sites More sharing options...
jackpf Posted April 21, 2009 Author Share Posted April 21, 2009 Just windows tbh. I'll have a look a winsock tonight - I'm at school atm. I just had a look at some of the source on codeproject.com, and I must say it looks quite hard I'll have a go tonight though. Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/154077-solved-c-conversion-errors/#findComment-815370 Share on other sites More sharing options...
corbin Posted April 22, 2009 Share Posted April 22, 2009 Winsock is pretty easy unless you get super complex with it. Wow... that made no sense at all. I basically just said "It's easy unless you make it hard." Hrmmm... lol. Quote Link to comment https://forums.phpfreaks.com/topic/154077-solved-c-conversion-errors/#findComment-816049 Share on other sites More sharing options...
jackpf Posted April 22, 2009 Author Share Posted April 22, 2009 Generally the case with most things lol. Yeah, I didn't get a chance to have a go last night. I'll have a go today. I'll probably post back here when I screw it up. Cheers for your help. Quote Link to comment https://forums.phpfreaks.com/topic/154077-solved-c-conversion-errors/#findComment-816253 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.