1

I'm trying to make a program that will execute an entered command. I've tried:

const char* command = "echo Hello, World!";
system(command);

And this works fine. But I'd like to do so that the user has to type the command. And here's the problem. I've tried so:

const char* command;
cin >> command;
system(command.c_str());

And this also works fine, but only for a one-word command like cls. But if I try to enter a command like color a, the program saves my input data as two separate commands and executes color and next it tries to execute a and obviously the output is: 'a' is not recognized as an internal or external command, operable program or batch file.

So how to save the input data as a single string, which contains spaces?

3 Answers3

2

The problem you are encountering is that cin tokenizes the input on whitespace. If you want to obtain an entire line of input including non-line-ending whitespace, you should use std::getline().

Code-Apprentice
  • 69,701
  • 17
  • 115
  • 226
1

I believe you're looking for std::getline() - this will process all input up until a specified delimiter, defaulting to \n:

std::string line;
if (std::getline(std::cin, line)) {
    ...
}
Barry
  • 247,587
  • 26
  • 487
  • 819
  • The if here isn't right. Getline returns the stream. – Rob L Feb 21 '16 at 22:25
  • @Rob Stream is convertible to `bool`. You don't do `if(stream >> x)`? – LogicStuff Feb 21 '16 at 22:25
  • That if statement implies that it is checking success or failure. The stream is always returned, even on failure. You shouldn't check it with an if (). – Rob L Feb 21 '16 at 22:27
  • 1
    @RobL It **is** checking success or failure. A stream is contextually convertible to `bool`. – Barry Feb 21 '16 at 22:28
  • @Rob Interesting... what check are you suggesting? – LogicStuff Feb 21 '16 at 22:29
  • 1
    @RobL See [`operator bool()`](http://en.cppreference.com/w/cpp/io/basic_ios/operator_bool) – Barry Feb 21 '16 at 22:29
  • Check [cppreference](http://en.cppreference.com/w/cpp/string/basic_string/getline). The return value is the *input*. It doesn't matter what you pass it, it will return the input. You can't check that for failure. – Rob L Feb 21 '16 at 22:31
  • @Rob You just said you can't check `std::basic_istream` for failure, is that right? And what does `bool func()` do? You can't use it like `if(func())` because it always returns `bool`? – LogicStuff Feb 21 '16 at 22:34
  • 1
    @RobL That is wrong. You can absolutely check for failure. See [this example](http://coliru.stacked-crooked.com/a/0999d7549f91cdb2). Yes, the return value is the input stream - but the state of the input stream can change as a result of the operation, and it's that state that you're checking in the `if()` – Barry Feb 21 '16 at 22:34
  • Crud, never used that before. I always called fail... thanks. – Rob L Feb 21 '16 at 22:36
  • @RobL See also some of the answers to [this question](http://stackoverflow.com/q/5605125/2069064) – Barry Feb 21 '16 at 22:38
0
std::getline(std::cin, command);
Rob L
  • 1,637
  • 9
  • 19