What's Here?
- Members: 340,029
- Replies: 920,146
- Topics: 154,875
- Snippets: 4,853
- Tutorials: 1,256
- Total Online: 4,085
- Members: 111
- Guests: 3,974
|
Welcome to Dream.In.Code |
|
|
Become an Expert!
Join 340,029 Programmers for FREE! Get instant access to thousands  of experts, tutorials, code snippets, and more! There are 4,085 people online right now. Registration is fast and FREE... Join Now!
Chat LIVE With a Expert
|
System command & cin.get?
System command & cin.get?
Rate Topic:
   
Posted 26 November 2008 - 11:50 AM
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.
Posted 26 November 2008 - 11:54 AM
#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.
Posted 26 November 2008 - 12: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);
Posted 26 November 2008 - 01:05 PM
Have you tried concatenating into one phrase to pass to command?
Posted 26 November 2008 - 01: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.
Posted 26 November 2008 - 01:17 PM
KYA, 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.
Posted 26 November 2008 - 01: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 - 01:25 PM
Posted 26 November 2008 - 01: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 - 01:29 PM
Posted 26 November 2008 - 01: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.
Posted 26 November 2008 - 01: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;
}
Posted 26 November 2008 - 01:47 PM
OR do both!
joking
Posted 26 November 2008 - 01: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 - 01:57 PM
Posted 26 November 2008 - 01: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.
Posted 26 November 2008 - 02: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 - 02:42 PM
Posted 26 November 2008 - 03: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 - 03:18 PM
Posted 26 November 2008 - 03:18 PM
yes thats exactly what I was going for or to just temporarly make the screen black.
Any info? or any info on the keybd event stuff?
Posted 26 November 2008 - 03:19 PM
This is a console program right? Why not just clear the screen as it transitions? Or does still give the effect of minimizing that you want to avoid?
Posted 26 November 2008 - 03:21 PM
It is and yah it does. In my code it does a system CLS, changes the background color and switches to full screen. But it shows the change in color and size to much.
and holy crap your fast went to get a drink came back and bam another reply O>O
Posted 26 November 2008 - 03:45 PM
Heh, I haven't had much to do all day
Finals aren't for another two weeks \0/
on topic:
Could you maybe post a screenshot of before, during and after so i can get a better idea of what you're describing?
edited for a typo
This post has been edited by KYA: 26 November 2008 - 03:45 PM
Posted 26 November 2008 - 03:52 PM
ehh, Its giving me hell trying to get a screenshot of it but I can post the code itself if that would work?
EDIT: yah i'm using 2 pcs to do this 1 to do all my research and to control the other, the other I'm using to run and write the code. I tried using both computers to capture what happens when it runs the code but it won't capture it.
This post has been edited by Blade2021: 26 November 2008 - 03:56 PM
1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users
|
Be Social
Programming
Web Development
Reference Sheets
Bye Bye Ads
Monthly Drawing
Top Contributors
Top 10 Kudos This Month
|