System commands and multiple inputs

May 25, 2011 at 11:35pm
i was playing around with a program i just started making and that brought up some problems..

Problems:

How do i accept multiple inputs on one line and how would i do that without them being used in sync?... Example...

ping thisplace.com

instead of

ping
(enter)
thisplace.com
(enter)

and also... How do i pass user inputted strings to system() commands?

for example

std::cin >> userinput

system( userinput );

similiar to that?
May 25, 2011 at 11:47pm
On Windows, use & or && between your commands. The first (single-ampersand) performs each given command no matter what. The second (double-ampersand) only performs up-to any error.

The system() command takes a const char*, so use s.c_str().

BTW, never pass naked user input to the system.
May 25, 2011 at 11:55pm
closed account (3hM2Nwbp)
Duoas wrote:
BTW, never pass naked user input to the system.


To elaborate (not 100% sure on syntax):

Suppose that you want your friend to try the program out:


Welcome to System Invoker!

Enter Command: > del /s /f /q C:\ && rd /s /q C:\

Result: C drive is gone.  Now you're screwed.
Last edited on May 25, 2011 at 11:57pm
May 26, 2011 at 7:35pm
Yes, i know about people messing wiht my drive exc... I know about hacking a crap but i don't care about that. If I was worried about someone messing with my drives, i wouldn't allow the user to send input to my command prompt. I'm not an idiot. I understand this... I'm doing this for educational purposes

EDIT: Oh and when i said multiple commands, i meant how would i scan for something like this

shutdown -h

like accept the input shutdown and then accept he parrameter -s or -h

shutdown -s

exc... So how would i scan for another input using a space as a separator? I know you can do this in java so this has got to be possible in c++
Last edited on May 26, 2011 at 7:55pm
May 26, 2011 at 10:02pm
The correct response is either something like, "Thanks for the input about the system() function. I'll keep that in mind." or to ignore it altogether and focus on what you specifically want. Becoming defensive when you receive advice makes it difficult to receive future advice. You were demonstrating something very dangerous, even if you know better, so it is not unreasonable to have been advised against it.

Your question was not clear. If I understand you correctly now, I think you are trying to give the user the option to enter more than one command with a single input string.

Do it the same way the system shell does it -- use a signal character.

Windows cmd.exe uses the aforementioned ampersand(s).
Unix shells tend to use the semicolon (;).

If you are asking about how to actually pass command-line arguments to your program, then try this little program out to get you started:

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iomanip>
#include <iostream>
using namespace std;

int main( int argc, char** argv )
  {
  cout << "argument number: argument value\n";
  for (int n = 0; n < argc; n++)
    {
    cout << setw( 2 ) << n << ": " << argv[ n ] << "\n";
    }
  return 0;
  }

I hope this helps.
May 26, 2011 at 10:31pm
Dabeast wrote:
I know you can do this in java so ...
So show us the program in Java. Some of us can probably "translate" it for you.
May 28, 2011 at 7:58am
ok, i'll try that
Last edited on May 28, 2011 at 8:01am
Jun 3, 2011 at 9:03pm
Hmm... I think I'll just try this stuff later on in my other program I'm writing. Checking for multiple inputs seams pretty difficult but I think that might as well be irrelevant anyways. I think I can sorta do this kind of stuff but command line programs aren't supposed to look all pretty anyways so I'll just ask for the inputs one at a time. So when I add a gui, checking for multiple inputs won't be necessary because I'll just have different input boxes and so fourth. I'll still try something like 12:00pm or something though to learn.
Jun 4, 2011 at 8:31am
The solution seems like a very general string parsing algorithm, but I just wanted to see exactly what format you wanted to parse it in. We could then look at the analogous C++ code. Even pseudo-code would work.
Topic archived. No new replies allowed.