Jump to content

[SOLVED] C++ Conversion Errors


Recommended Posts

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.

Link to comment
Share on other sites

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());

Link to comment
Share on other sites

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());
    }
}

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.)

Link to comment
Share on other sites

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 :P

 

I'll have a go tonight though.

 

Thanks.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.