System command & cin.get?

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

33 Replies - 3835 Views - Last Post: 27 November 2008 - 12:30 AM Rate Topic: -----

#1 Blade2021  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 204
  • Joined: 31-October 08

System command & cin.get?

Post icon  Posted 26 November 2008 - 12:50 PM

This is a really easy question for those who are experienced.

How would you get a program to run a system command of whatever the user types. Example: Run nslookup (whatever user types)

Whenever I tried it came up an overload error.
Is This A Good Question/Topic? 0
  • +

Replies To: System command & cin.get?

#2 KYA  Icon User is offline

  • su wtf -am -i /doing/with/my/life
  • member icon

Reputation: 2979
  • View blog
  • Posts: 19,033
  • Joined: 14-September 07

Re: System command & cin.get?

Posted 26 November 2008 - 12:54 PM

#include <iostream>
#include <string>
using namespace std;

int main()
{
	
	char command[255];
	cout <<" Enter a string to send to the command interface: ";
	cin.getline(command, 255);
	system(command);

	return 0;
}



I tested it with "pause" for example.
Was This Post Helpful? 0
  • +
  • -

#3 Blade2021  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 204
  • Joined: 31-October 08

Re: System command & cin.get?

Posted 26 November 2008 - 01:41 PM

That works if you type in a command thats in windows programming. I'm looking for just typing the web address without having to type nslookup.

 cout << "type the web address" << endl;
cin.getline
?
system("nslookup (what user typed);


Was This Post Helpful? 0
  • +
  • -

#4 KYA  Icon User is offline

  • su wtf -am -i /doing/with/my/life
  • member icon

Reputation: 2979
  • View blog
  • Posts: 19,033
  • Joined: 14-September 07

Re: System command & cin.get?

Posted 26 November 2008 - 02:05 PM

Have you tried concatenating into one phrase to pass to command?
Was This Post Helpful? 0
  • +
  • -

#5 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 5667
  • View blog
  • Posts: 22,511
  • Joined: 23-August 08

Re: System command & cin.get?

Posted 26 November 2008 - 02:07 PM

Like KYA said, use the system() command, but pass it the string "nslookup whatever.the.user.types".

Now, if you're planning to work with what's returned from the command, i.e., you want to parse the returned data, you should use popen(the command) and read from the FILE* that it returns.
Was This Post Helpful? 0
  • +
  • -

#6 Blade2021  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 204
  • Joined: 31-October 08

Re: System command & cin.get?

Posted 26 November 2008 - 02:17 PM

View PostKYA, on 26 Nov, 2008 - 01:05 PM, said:

Have you tried concatenating into one phrase to pass to command?


How would you concentrate it into one command without it running nslookup before it gets the address from the user?

and no JackallTrades i'm not planning on getting it to do anything with the info but to run the command. Thanks for the info though.
Was This Post Helpful? 0
  • +
  • -

#7 KYA  Icon User is offline

  • su wtf -am -i /doing/with/my/life
  • member icon

Reputation: 2979
  • View blog
  • Posts: 19,033
  • Joined: 14-September 07

Re: System command & cin.get?

Posted 26 November 2008 - 02:23 PM

Perhaps I don't fully understand the intent. Doesn't nslookup require the information before the command is run?

I ran nslookup by itself to verify, it asks for information there as well. So you want to pass an argument to look up before lookup is run and have it do it automatically?

edit: If the latter is the case, concatenating works just fine.

This post has been edited by KYA: 26 November 2008 - 02:25 PM

Was This Post Helpful? 0
  • +
  • -

#8 Blade2021  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 204
  • Joined: 31-October 08

Re: System command & cin.get?

Posted 26 November 2008 - 02:25 PM

Yah I think you have the right idea. (get nslookup to run what the user types)

EDIT: How would you concentrate it? Example?

This post has been edited by Blade2021: 26 November 2008 - 02:29 PM

Was This Post Helpful? 0
  • +
  • -

#9 KYA  Icon User is offline

  • su wtf -am -i /doing/with/my/life
  • member icon

Reputation: 2979
  • View blog
  • Posts: 19,033
  • Joined: 14-September 07

Re: System command & cin.get?

Posted 26 November 2008 - 02:28 PM

#include <iostream>
#include <string>
using namespace std;

int main()
{

	char command[100];
	cout <<" Enter a string to send to the command interface: ";
	cin.getline(command, 100);
	char look[255] = "nslookup ";
	strcat(look, command);
	system(look);

	return 0;
}



I tested with 127.0.0.1, returns home, so it appears to work. :)
Was This Post Helpful? 0
  • +
  • -

#10 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 5667
  • View blog
  • Posts: 22,511
  • Joined: 23-August 08

Re: System command & cin.get?

Posted 26 November 2008 - 02:46 PM

Or go for the all C++ solution ;)
#include <iostream>  
#include <string>  
using namespace std;  
   
int main()  
{  
    string command;
    cout <<" Enter a string to send to the command interface: ";  
    cin >> command;
    string look("nslookup " + command);  
    system(look.c_str());  
    return 0;  
} 

Was This Post Helpful? 1
  • +
  • -

#11 KYA  Icon User is offline

  • su wtf -am -i /doing/with/my/life
  • member icon

Reputation: 2979
  • View blog
  • Posts: 19,033
  • Joined: 14-September 07

Re: System command & cin.get?

Posted 26 November 2008 - 02:47 PM

OR do both! ;)


joking
Was This Post Helpful? 1
  • +
  • -

#12 Blade2021  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 204
  • Joined: 31-October 08

Re: System command & cin.get?

Posted 26 November 2008 - 02:55 PM

ok, cool thank you both, up for a new challenge? ;)

Why does input from the user and system get disabled for the program after this line of code is used to turn off the monitor?

EDIT: and is there any other way to get it to not disable input to the program or to turn off the screen for a set amount time? ( to allow changes to the program window without the user seeing)


SendMessage(HWND_BROADCAST, WM_SYSCOMMAND, SC_MONITORPOWER, (LPARAM) 2);



This post has been edited by Blade2021: 26 November 2008 - 02:57 PM

Was This Post Helpful? 0
  • +
  • -

#13 KYA  Icon User is offline

  • su wtf -am -i /doing/with/my/life
  • member icon

Reputation: 2979
  • View blog
  • Posts: 19,033
  • Joined: 14-September 07

Re: System command & cin.get?

Posted 26 November 2008 - 02:57 PM

Spitballing here, but how can you do anything if the monitor is off? Even if the program did respond in some fashion you wouldn't be able to tell. ;)
Was This Post Helpful? 0
  • +
  • -

#14 Blade2021  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 204
  • Joined: 31-October 08

Re: System command & cin.get?

Posted 26 November 2008 - 03:40 PM

Well in my script i'm changing from regular size to full screen prompt but I don't want the user to see it switch. which if you watch when executing it it shows it like your minimizing a window kind of thing.

Also I'm having some trouble with the keybd commands trying to find the keyboard event for pressing down the Y key.

Sorry if this topic is very random and excessive. Just trying to get awnsers to multiple problems without spamming the forum with multiple topics ;)

This post has been edited by Blade2021: 26 November 2008 - 03:42 PM

Was This Post Helpful? 0
  • +
  • -

#15 KYA  Icon User is offline

  • su wtf -am -i /doing/with/my/life
  • member icon

Reputation: 2979
  • View blog
  • Posts: 19,033
  • Joined: 14-September 07

Re: System command & cin.get?

Posted 26 November 2008 - 04:15 PM

It's ok. Random is more interesting. More specifically, are you calling something to turn the monitor back on after you perform you action(s)?

The monitor being turned "off" should not affect the rest of the program's execution.

edited for a typo

This post has been edited by KYA: 26 November 2008 - 04:18 PM

Was This Post Helpful? 0
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3