1. Computer problem? Tech Support Guy is completely free -- paid for by advertisers and donations. Click here to join today! If you're new to Tech Support Guy, we highly recommend that you visit our Guide for New Members.

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

Discussion in 'Software Development' started by Codiah, Oct 7, 2008.

Thread Status:
Not open for further replies.
  1. Codiah

    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..
     
  2. mt2002

    mt2002

    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.
     
  3. Codiah

    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...
     
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 733,556 other people just like you!

Thread Status:
Not open for further replies.

Short URL to this thread: https://techguy.org/756867

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice