Need some help

This is a discussion on Need some help within the C++ Programming forums, part of the General Programming Boards category; Hi, I'm trying to make a simple program that will take a command and run that system command, but am ...

  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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,473
    cin >> will stop reading input on the first white space
    if command includes parameters - you have to use getline to read it whole
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21