Another C++ question.. System commands...

Status
This thread has been Locked and is not open to further replies. Please start a New Thread if you're having a similar issue. View our Welcome Guide to learn how to use this site.

Codiah

Thread Starter
Joined
Sep 27, 2008
Messages
116
Is it possible to make a command console app that will allow you to use all the windows command prompt commands as well... such as (dir, del, type, cd, md) and the various others, but still maintain the ability to use costom commands defined in your application?

basically make a custom command prompt using c++,

the problem i'm facing is having it display certian information, like the current directory and things that would normally show with an "ECHO off" command in the command prompt...

if i need to describe this a little better let me know...


::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
OK so here's an update of what i have so far, and what i'm trying to do...
keep in mind i'm new to C++ and not worried about portability if its an
apparent issue, i'll be using the app on this comp only for now...
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::


This is the first part of the app, thanks to mt2002 for the help on that one... this is just a basic idea of how i'm trying to put custom command together...

#include "stdafx.h"
#include <iostream>
#include <string>

std::string commands[] = {
"print",
"print1",
"print2",
"print3",
"exit"
};

enum {
CMD_PRINT,
CMD_PRINT1,
CMD_PRINT2,
CMD_PRINT3,
CMD_EXIT
};

void cmdPrint () {

std::cout << "testing one..." << std::endl;
}

void cmdPrint1 () {

std::cout << "testing two.." << std::endl;
}
void cmdPrint2 () {

std::cout << "testing three..." << std::endl;
}

void cmdPrint3 () {

std::cout << "testing four..." << std::endl;
}
bool cmdTest (std::string& cmd) {

bool ret=true;

if (cmd==commands[CMD_PRINT])
cmdPrint();
else if (cmd==commands[CMD_PRINT1])
cmdPrint1();
else if (cmd==commands[CMD_PRINT2])
cmdPrint2();
else if (cmd==commands[CMD_PRINT3])
cmdPrint3();

else if (cmd==commands[CMD_EXIT])
;

else
ret=true;

return ret;
}

int main()
{
std::string strCmd;
std::cout << "Type \"exit\" to quit\n" << std::endl;

do {
std::cout << "command >";
std::cin >> strCmd;

if (! cmdTest (strCmd) )
std::cout << "unkown command \'" << strCmd << "\', ignored" << std::endl;

}
while ( strCmd != commands[CMD_EXIT]);
}

::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

This is the second part, i figured out a way to use the command line actions through this method, not ure if there is a better way to do it.. if so i'm all ears.. but basically i want to combine the two so when you type in a command that is not listed in the program it runs off the origonal command prompt so i can still use all the default command line actions..

// test running command line actions...

#include "stdafx.h"
#include <windows.h>
#include <iostream>

using namespace std;

int main()
{
char command[50];
cout<<"\n Command: ";
cin.getline(command, 50);
system(command);
return 0;
}

::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

Ok, so another update, i've been working at this for a bit, and its not working out well for me.. i've tried to mix the code in and put..

char command[50];
cout<<"\n Command: ";
cin.getline(command, 50);
system(command);

in place of this line..

std::cout << "unkown command \'" << strCmd << "\', ignored" << std::endl;

the result compiles just fine without errors, but when i try and run it i'll get an error when trying to use that portion of the code, i can also safely mix the code in as its own seperate function, but we run into the problem of me not knowing how to utalize that as a function in this code..

anyway... i've stayed awak long enough for this go-round, looking forward to seeing what everyone has to say... thanks in advance..
 
Joined
Sep 14, 2005
Messages
919
I am going to recommend using std::string over character arrays for strings. Character arrays have alot of problems associated with them that are not existent when using std::string.

You can determine if it is your command or not by simply testing it with the commands:
Code:
int main()
{
   std::string command;
   std::cout << "\n Command: ";
   std::cin >> command;

   //! simple sequential search--test if its any of our commands

   bool bFound=false;
   for (int i=0; i<LAST_COMMAND; i++)
      if (command==commands[i]) {
         bFound=true;
         break;
      }

   if (bFound)
      //! do something fancy :) Its our command
   else
      system(command);     //! Not our command, try system

   return 0;
}
Doing this with character arrays adds on extra complexity that you do not need to worry about with std::string.

This will work so long as LAST_COMMAND is the last constant in the enum. You dont need to add anything else:

Code:
enum {
   CMD_PRINT,
   CMD_PRINT1,
   CMD_PRINT2,
   CMD_PRINT3,
   CMD_EXIT,
   LAST_COMMAND
};
Also, with regards to system() you should take note that its paramater is operating system specific thus it is not portable. Its still the best method though as system()'s interface is standard so I would stick with using it.
 

Codiah

Thread Starter
Joined
Sep 27, 2008
Messages
116
Thanks again mt2002, i'll experiment with that for a while an see if i can't get it to work, i've already been playing around with it for about half an hour with no luck, i'm a little bit stuck, but i'd like to try and figure it out on my own before i ask for any more help...

see you later mt2002...
 
Status
This thread has been Locked and is not open to further replies. Please start a New Thread if you're having a similar issue. View our Welcome Guide to learn how to use this site.

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 807,865 other people just like you!

Latest posts

Staff online

Top