Thread: Need some help

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    12

    Need some help

    Hi, I'm trying to make a simple program that will take a command and run that system command, but am having a little trouble. Here's my code:

    Code:
    #include <iostream>
    using namespace std;
    int main()
    {
        string command;
        cout << "Please enter a system command: \n";
        cin >> command;
        system(command);
        system("Pause");
        return 0;
    }
    Does anyone know why this isn't working?

  2. #2
    Registered User
    Join Date
    May 2006
    Posts
    903
    There are two errors. You didn't include string (which is necessary for std::string) and you need to call command.c_str() to get the const char* member which system() takes.

  3. #3
    Registered User
    Join Date
    Feb 2008
    Posts
    12

    Red face

    Obviously this is wrong:


    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    int main()
    {
        std::string command;
        cout << "Please enter a system command: \n";
        cin >> command.c_str();
        system(command.c_str());
        system("Pause");
        return 0;
    }
    Again, I donno what I'm doing wrong. Sorry, I'm a complete noob but I am doing research before asking for help

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Code:
        cin >> command;
        system(command.c_str());

  5. #5
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,794
    cin >> will stop reading input on the first white space
    if command includes parameters - you have to use getline to read it whole
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    David J. Wheeler

  6. #6
    Registered User
    Join Date
    Feb 2008
    Posts
    12

    Smile

    Quote Originally Posted by vart View Post
    cin >> will stop reading input on the first white space
    if command includes parameters - you have to use getline to read it whole
    Ah, thanks. I was wondering why it was acting 'funny'. Thanks.

Popular pages Recent additions subscribe to a feed