system commands visual studio 2010 C++

Hi,
I am using visual studio 2010. Im creating a program and i was wondering if it is possible to input a variable in with the system command for example user inputs a ip under a string variable how would i combine that in with the system command? would it be like this? Can i get an example of how to do it please?

system("ping")<<cout x;

Thanks
I think you want to use pipes. In that case, look at this example in MSDN:
http://msdn.microsoft.com/en-us/library/windows/desktop/ms682499(v=vs.85).aspx
I have no clue what that is:/
No need for esoteric uses of operator<<. Just prepare the command line as a string: Concatenate "ping " with x, and then call system().
webJose how would that look?
It's a really simple topic, really. Just look it up. You have the choice of using C strings, or the C++ std::string class. The latter is easier. You can check the reference in this site for detailed information about this C++ class.

If you decide to go the route of C strings, you can just use the C string functions found in the <cstring> header, such as strlen() to get the length of a string, or strcpy() to copy one string into another, or strcat() to concatenate two strings. Here's a very simple example with C strings (untested):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include <cstring>

using namespace std;

int main()
{
    char myString[10];
    strcpy(myString "Hi");
    cout << myString << endl;
    strcat(myString, " friend");
    cout << myString << endl;
    cin.get();
    return 0;
}
I need it where they input ip then it pings how do you do that ? sorry im complete beginner but thank you for helping me!
I gave you the key components. I usually don't give out complete solutions. You should be able to extrapolate my example to your particular needs, or investigate about std::string. Either way it is very simple.
your example has no inputs?
Last edited on
1
2
3
4
5
std::ostringstream str;
std::string x;
cin>>x; //input ip
str<<"ping "<<x;
system( str.str().c_str() );
doesnt work
whats wrong with it?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include <iostream>
#include <windows.h>
#include <string.h>

using namespace std;
std::ostringstream str;

int main()

{
	int x;
	

std::string ip;
	
	system("color 2F");
	system("title pingy 1.0");
	cout<< "pingy v1.0"<< endl<<endl;
	
menu:
	cout<< "Menu:\n";
		cout<< "1. Ping IP\n";
		cout<< "2. Net View\n";
		cout<< "3. IP Config\n"<<endl;
		cout<< "Command: ";
		cin >> x;
		cout<< endl;
		cin.get();
if (x=1) goto ping;
if (x=2) goto netview;
cin.get();

ping:


cin>>ip; //input ip
str<<"ping "<<x;
system( str.str().c_str() );



netview: 
	system("net view");
	goto end;

end:
	cout<< endl;
	goto menu;

	cin.get();
	return(0);
}
Last edited on
closed account (DSLq5Di1)
Too much copy'n'paste and not enough reading comprehension,

1
2
if (x=1) goto ping; // == tests equivalence, = assigns a value.
if (x=2) goto netview;

1
2
3
cin>>ip; //input ip
str<<"ping "<<x; // ???
system( str.str().c_str() );

goto and system() are evil.. and you're going to burn in hell.
http://www.cplusplus.com/doc/tutorial/control/
http://msdn.microsoft.com/en-us/library/ms682425
in my code, i wrote x. you changed x to ip. But you didn't changed x to ip in
 
str<<"ping "<<x //you must change x to ip 


So, learn c++ first.

@sloppy9
goto is evil but system is not :)
system is even more evil than goto. There are, under some very, very rare circumstances situations in which using goto might be a somewhat understandable idea. system is pretty much never useful.
Topic archived. No new replies allowed.