Thread: dos prompt disappearing

  1. #1
    Registered User
    Join Date
    Jul 2004
    Posts
    24

    Exclamation dos prompt disappearing

    I want to make a program similar to the dos prompt (i dont know the english name for it) in wich you can issue commands to your system (like 'netsat -n' or 'ping 127.0.0.1'). It works, but it should return to the main() function as soon as the command was executed. Instead, it closes. The code i have so far is:
    Code:
    #include <iostream.h>
    
    char command[99999];
    
    int main()
    {
        cout << "Command>";
        cin.getline(command, 99999, '\n');
        system(command);
        int main();
        return 0;
    }
    could anyone pease tell me what`s wrong with the code?

  2. #2

  3. #3
    Useless Apprentice ryan_germain's Avatar
    Join Date
    Jun 2004
    Posts
    76
    well to start off...you could use:
    Code:
    #include<iostream>
    using namespace std;
    Also, you can put char command[99999] at the begining of main. you can also use
    Code:
    cin >> command;
    instead of
    Code:
    cin.getline(command, 99999, '\n');
    You can also take out int main() inside main and replace by a do while or something. I think you can start with that.

    edit: and then add what thantos said to the end
    There is not the slightest indication that [nuclear energy] will ever be obtainable. It would mean that the atom would have to be shattered at will.

    -Albert Einstein, 1932

  4. #4
    Registered User
    Join Date
    Jul 2004
    Posts
    24
    Well, ive tried making the user input quit to exit:
    Code:
    #include <iostream.h>
    #include <string.h>
    
    char command[99999];
    
    int main()
    {
        cout << "Command>";
        cin.getline(command, 99999, '\n');
        if(!strcmpi("quit", char command)
        {
            return 0;system(command);
            int main();
        }
        
        else
        {
            system(command);
            int main();
        }
    }
    But in Dev-cpp it gives me the errors:

    parse error before `)' token

    parse error before `else'

    But i couldn`t find any missing semicolons, wrong letters, etc.

    And using
    Code:
    cin << command
    instead of using
    Code:
    cin.getline(command, 99999, '\n')
    would make ping 127.0.0.1 to be read by the program as ping.

  5. #5
    Registered User
    Join Date
    Jul 2004
    Posts
    24
    and what`s in the faq doesnt work

  6. #6
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Actually whats in the faq does work
    Heres a hint:
    Code:
    int main();
    Is not a function call.

  7. #7
    Useless Apprentice ryan_germain's Avatar
    Join Date
    Jun 2004
    Posts
    76
    what version of dev do u use? I wasnt working for me so i upgraded to 4.9.9.0 (the 5 beta or whatever) and it worked

    >>>>>You can also take out int main() inside main and replace by a do while or something.
    There is not the slightest indication that [nuclear energy] will ever be obtainable. It would mean that the atom would have to be shattered at will.

    -Albert Einstein, 1932

  8. #8
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Code:
    if(!strcmpi("quit", char command)
    1) missing a closing )
    2) Can't declare a variable there

  9. #9
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Try something like this:
    Code:
    #include <iostream>
    #include <string>
    
    int main()
    {
        using namespace std;
        string command;
        for (;;)
        {
          cout << "Command>";
          getline(cin, command);
          if ( command == "quit")
            break;
          system(command);
        }
    }

  10. #10
    Registered User
    Join Date
    Jul 2004
    Posts
    24
    Well, i`ve tried this:
    Code:
    #include <iostream.h>
    
    char command[99999];
    
    void exec();
    void call();
    
    int main()
    {
        void exec();
    }
    
    void exec()
    {
        cout << "Command>";
        cin.getline(command, 99999, '\n');
        if(command = "quit")
        {
            return;
        }
        
        else
        {
            void call();    
        }
        
    }
    
    void call()
    {
        system(command);
        void exec();
    }
    But it gives me the error:

    "incompatible types in assignment of `const char[5]' to ` char[99999]'"

    could somebody tell me what to do? If i use
    Code:
    if (!strcmpi("quit", command))
    {
              return;
    }
    
    else
    {
              void call();
    }
    doesn`t work

  11. #11
    Registered User
    Join Date
    Jul 2004
    Posts
    24
    it closes right away when i open the program

  12. #12
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    follow the standards and we can help you better... you need the <cstring> header for strcmpi, or in the older standards, <string.h>...

    and put cin.get(); everywhere it could possibly run off the end of main...
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  13. #13
    Registered User Draco's Avatar
    Join Date
    Apr 2002
    Posts
    463
    with your line
    Code:
    if(command = "quit")
    and the error you get, try changing that '=' to a '==' and see if it works

  14. #14
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    he can't do that anyway - he was right in trying strcmpi()
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  15. #15
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Quote Originally Posted by major_small
    follow the standards and we can help you better... you need the <cstring> header for strcmpi, or in the older standards, <string.h>...
    Well using the standard and strcmpi don't go together as the function is non standard.

    Again I'll refer you to my posting using std::string instead of a character array. It won't do insenstive comparison but with a little work it can.

    Edit: Just noticed one small mistake on the code in the std::string post but I can't edit that post.
    Code:
    system(command);
    Should be
    Code:
     system(command.c_str());
    My apologies for that mistake


    However if you are dead set on using a null terminated array of characters here is some advise:

    Using a loop to keep things alive while the person is executing commands. If you wish to use functions to get the input use return values to determine if you should continue or not.

    As to why your program fails:
    Code:
    #include <iostream.h>
    New standard specifies that the header should be <iostream>
    Code:
    char command[99999];
    Not invalid just not good practice to use global variables

    Code:
    void exec();
    void call();
    These are ok and act as a prototype for the functions
    Code:
    int main()
    {
    Still ok
    Code:
        void exec();
    And here we have a problem. You think this is a call but guess what? It isn't. Its acting as a prototype for exec. to make it a call just use exec();
    Code:
    }
    
    void exec()
    {
        cout << "Command>";
        cin.getline(command, 99999, '\n');
    This is ok
    Code:
        if(command = "quit")
    Here you are trying to assign the const array of characters "quit" to the array command which isn't valid
    Code:
        {
            return;
        }
        
        else
        {
    This is ok
    Code:
            void call();
    Same as above, its a prototype not a call
    Code:
        }
        
    }
    
    void call()
    {
        system(command);
    This is ok
    Code:
        void exec();
    Again a prototype not a call
    Code:
    }
    This is ok
    Last edited by Thantos; 09-02-2004 at 10:41 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling from DOS Prompt using Visual Studio Express 2005
    By The SharK in forum C++ Programming
    Replies: 8
    Last Post: 06-13-2006, 01:24 AM
  2. Dos prompt cursor position
    By bally1020 in forum C Programming
    Replies: 2
    Last Post: 02-17-2005, 05:10 PM
  3. Output in DOS Prompt
    By siumui in forum C Programming
    Replies: 4
    Last Post: 12-06-2002, 11:27 PM
  4. DOS prompt disapearing
    By Zahl in forum Tech Board
    Replies: 6
    Last Post: 11-17-2002, 06:59 PM
  5. Dos Prompt
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 01-05-2002, 10:14 AM