C++ School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become a C++ Expert!

Join 306,811 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,698 people online right now. Registration is fast and FREE... Join Now!




System command & cin.get?

2 Pages V  1 2 >  

System command & cin.get?

Blade2021

26 Nov, 2008 - 11:50 AM
Post #1

D.I.C Head
**

Joined: 31 Oct, 2008
Posts: 185


My Contributions
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.


User is offlineProfile CardPM
+Quote Post


KYA

RE: System Command & Cin.get?

26 Nov, 2008 - 11:54 AM
Post #2

#include <nerd.h>
Group Icon

Joined: 14 Sep, 2007
Posts: 11,487



Thanked: 507 times
Dream Kudos: 2875
Expert In: C, C++, Java

My Contributions
cpp

#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.
User is online!Profile CardPM
+Quote Post

Blade2021

RE: System Command & Cin.get?

26 Nov, 2008 - 12:41 PM
Post #3

D.I.C Head
**

Joined: 31 Oct, 2008
Posts: 185


My Contributions
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.

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

User is offlineProfile CardPM
+Quote Post

KYA

RE: System Command & Cin.get?

26 Nov, 2008 - 01:05 PM
Post #4

#include <nerd.h>
Group Icon

Joined: 14 Sep, 2007
Posts: 11,487



Thanked: 507 times
Dream Kudos: 2875
Expert In: C, C++, Java

My Contributions
Have you tried concatenating into one phrase to pass to command?
User is online!Profile CardPM
+Quote Post

JackOfAllTrades

RE: System Command & Cin.get?

26 Nov, 2008 - 01:07 PM
Post #5

I exist to Google your problems.
Group Icon

Joined: 23 Aug, 2008
Posts: 5,311



Thanked: 453 times
Dream Kudos: 50
Expert In: Being annoyed with lazy people.

My Contributions
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.
User is offlineProfile CardPM
+Quote Post

Blade2021

RE: System Command & Cin.get?

26 Nov, 2008 - 01:17 PM
Post #6

D.I.C Head
**

Joined: 31 Oct, 2008
Posts: 185


My Contributions
QUOTE(KYA @ 26 Nov, 2008 - 01:05 PM) *

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.
User is offlineProfile CardPM
+Quote Post

KYA

RE: System Command & Cin.get?

26 Nov, 2008 - 01:23 PM
Post #7

#include <nerd.h>
Group Icon

Joined: 14 Sep, 2007
Posts: 11,487



Thanked: 507 times
Dream Kudos: 2875
Expert In: C, C++, Java

My Contributions
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 Nov, 2008 - 01:25 PM
User is online!Profile CardPM
+Quote Post

Blade2021

RE: System Command & Cin.get?

26 Nov, 2008 - 01:25 PM
Post #8

D.I.C Head
**

Joined: 31 Oct, 2008
Posts: 185


My Contributions
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 Nov, 2008 - 01:29 PM
User is offlineProfile CardPM
+Quote Post

KYA

RE: System Command & Cin.get?

26 Nov, 2008 - 01:28 PM
Post #9

#include <nerd.h>
Group Icon

Joined: 14 Sep, 2007
Posts: 11,487



Thanked: 507 times
Dream Kudos: 2875
Expert In: C, C++, Java

My Contributions
cpp

#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. smile.gif
User is online!Profile CardPM
+Quote Post

JackOfAllTrades

RE: System Command & Cin.get?

26 Nov, 2008 - 01:46 PM
Post #10

I exist to Google your problems.
Group Icon

Joined: 23 Aug, 2008
Posts: 5,311



Thanked: 453 times
Dream Kudos: 50
Expert In: Being annoyed with lazy people.

My Contributions
Or go for the all C++ solution wink2.gif
cpp

#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;
}

User is offlineProfile CardPM
+Quote Post

KYA

RE: System Command & Cin.get?

26 Nov, 2008 - 01:47 PM
Post #11

#include <nerd.h>
Group Icon

Joined: 14 Sep, 2007
Posts: 11,487



Thanked: 507 times
Dream Kudos: 2875
Expert In: C, C++, Java

My Contributions
OR do both! wink2.gif


joking
User is online!Profile CardPM
+Quote Post

Blade2021

RE: System Command & Cin.get?

26 Nov, 2008 - 01:55 PM
Post #12

D.I.C Head
**

Joined: 31 Oct, 2008
Posts: 185


My Contributions
ok, cool thank you both, up for a new challenge? wink2.gif

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)

CODE


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





This post has been edited by Blade2021: 26 Nov, 2008 - 01:57 PM
User is offlineProfile CardPM
+Quote Post

KYA

RE: System Command & Cin.get?

26 Nov, 2008 - 01:57 PM
Post #13

#include <nerd.h>
Group Icon

Joined: 14 Sep, 2007
Posts: 11,487



Thanked: 507 times
Dream Kudos: 2875
Expert In: C, C++, Java

My Contributions
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. wink2.gif
User is online!Profile CardPM
+Quote Post

Blade2021

RE: System Command & Cin.get?

26 Nov, 2008 - 02:40 PM
Post #14

D.I.C Head
**

Joined: 31 Oct, 2008
Posts: 185


My Contributions
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 wink2.gif

This post has been edited by Blade2021: 26 Nov, 2008 - 02:42 PM
User is offlineProfile CardPM
+Quote Post

KYA

RE: System Command & Cin.get?

26 Nov, 2008 - 03:15 PM
Post #15

#include <nerd.h>
Group Icon

Joined: 14 Sep, 2007
Posts: 11,487



Thanked: 507 times
Dream Kudos: 2875
Expert In: C, C++, Java

My Contributions
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 Nov, 2008 - 03:18 PM
User is online!Profile CardPM
+Quote Post

Blade2021

RE: System Command & Cin.get?

26 Nov, 2008 - 03:18 PM
Post #16

D.I.C Head
**

Joined: 31 Oct, 2008
Posts: 185


My Contributions
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?
User is offlineProfile CardPM
+Quote Post

KYA

RE: System Command & Cin.get?

26 Nov, 2008 - 03:19 PM
Post #17

#include <nerd.h>
Group Icon

Joined: 14 Sep, 2007
Posts: 11,487



Thanked: 507 times
Dream Kudos: 2875
Expert In: C, C++, Java

My Contributions
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?
User is online!Profile CardPM
+Quote Post

Blade2021

RE: System Command & Cin.get?

26 Nov, 2008 - 03:21 PM
Post #18

D.I.C Head
**

Joined: 31 Oct, 2008
Posts: 185


My Contributions
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
User is offlineProfile CardPM
+Quote Post

KYA

RE: System Command & Cin.get?

26 Nov, 2008 - 03:45 PM
Post #19

#include <nerd.h>
Group Icon

Joined: 14 Sep, 2007
Posts: 11,487



Thanked: 507 times
Dream Kudos: 2875
Expert In: C, C++, Java

My Contributions
Heh, I haven't had much to do all day wink2.gif

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 Nov, 2008 - 03:45 PM
User is online!Profile CardPM
+Quote Post

Blade2021

RE: System Command & Cin.get?

26 Nov, 2008 - 03:52 PM
Post #20

D.I.C Head
**

Joined: 31 Oct, 2008
Posts: 185


My Contributions
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 Nov, 2008 - 03:56 PM
User is offlineProfile CardPM
+Quote Post

2 Pages V  1 2 >
Reply to this topicStart new topic

Time is now: 11/20/09 09:54PM

Live C++ Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month