Thread: Question about the System() function...

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    36

    Question about the System() function...

    I am writing a program that acts like a shell. I used the System() function to compute the commands, however, I realized that exit and cd do not work. I figured out how to make my own exit, however, I cannot figure out how to get cd to work...if anyone could help that'd be great, I appreciate it in advance....here's my code...

    Code:
    #include <iostream>
    #include <stdlib.h>
    #include <string>
    
    
    void main()
    {
            char cmd[100] = " ";
    
            cout << "Please enter command: ";
            cin.getline(cmd, 100);
    
            while (strcmp (cmd, "exit") !=0)
                    {
    
                            system (cmd);
                            cout << "Please enter command: ";
                            cin.getline(cmd,100);
                    }
                    exit(1);
    }

  2. #2
    Registered User
    Join Date
    Nov 2005
    Posts
    36
    this is the updated code...

    Code:
    #include <iostream>
    #include <stdlib.h>
    #include <string>
    #include <unistd.h>
    
    void main()
    {
            char cmd[100] = " ";
            char * pch;
    
            cout << "Please enter command: ";
            cin.getline(cmd, 100);
    
            while (strcmp (cmd, "exit") !=0)
            {
                    if((cmd[0] == "c") && (cmd[1] == "d"))
                    {
                            pch = strtok(cmd," ");
                            pch = strtok(NULL," ");
                            chdir(pch);
                    }
                    else
                    {
                            system (cmd);
                            cout << "Please enter command: ";
                            cin.getline(cmd,100);
                    }
            }
            exit(1);
    }
    and there are the errors im recieving from my linux compiler

    test.cpp:16: ISO C++ forbids comparison between pointer and integer
    test.cpp:16: ISO C++ forbids comparison between pointer and integer

    any ideas?

  3. #3
    Me -=SoKrA=-'s Avatar
    Join Date
    Oct 2002
    Location
    Europe
    Posts
    448
    You're getting those errors because you're comparing a string (char pointer) and an int.

    You should change the line that says
    Code:
    if((cmd[0] == "c") && (cmd[1] == "d")
    to
    Code:
    if((cmd[0] == 'd') && (cmd[1] == 'd')
    and there you go. No more warning, and a working piece of code.

    Also, please change the return value of main to int. Void is just wrong (see the FAQ or search the forums for the reason(s)).

    It might be better to fork() and then exec(), but that's a bit more complicated.
    SoKrA-BTS "Judge not the program I made, but the one I've yet to code"
    I say what I say, I mean what I mean.
    IDE: emacs + make + gcc and proud of it.

  4. #4
    Registered User
    Join Date
    Sep 2004
    Posts
    197
    Im wondering why you wouldn't be getting errors with this, since cin and cout are in the std namespace, and I don't see a "using namespace std;" or any thing like that in your code. Also, its int main, not void.
    If any part of my post is incorrect, please correct me.

    This post is not guarantied to be correct, and is not to be taken as a matter of fact, but of opinion or a guess, unless otherwise noted.

  5. #5
    Me -=SoKrA=-'s Avatar
    Join Date
    Oct 2002
    Location
    Europe
    Posts
    448
    He's probably using some very permissive compiler. My g++ erros out on void main().

    Also, I suspect this is not the code verbatim, because the line where he's got the error and it's actual line in the code he posted are different.
    SoKrA-BTS "Judge not the program I made, but the one I've yet to code"
    I say what I say, I mean what I mean.
    IDE: emacs + make + gcc and proud of it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. question abt function system();
    By errtime in forum C Programming
    Replies: 2
    Last Post: 10-01-2007, 01:24 PM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. measuring system resources used by a function
    By Aran in forum C Programming
    Replies: 1
    Last Post: 03-13-2006, 05:35 PM
  4. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 02:28 PM
  5. Replies: 3
    Last Post: 06-13-2005, 07:28 AM