0

I'm working on a shell project in c++. All the functionality I need is there. There is one problem, however. My exit condition is when the user inputs "exit", which works fine unless the previous command is a random string (ex: asldjkf). When a random string is entered and then exit is entered at the next prompt, it loops once more and then exits upon user entering "exit" again.

I've just been debugging using cout and I know the conditions to exit are met. Can someone tell me what might be happening here?`

here is what i'm calling in main()

UNIX_shell myShell;
char * args[100];

string check = "";           
argsIndex = 0;              

string userIn;
while(myShell.exitstatus == false)
{
    std::cin.getline(myShell.userIn, 256);   
    check = string(myShell.userIn);
    cout << check << endl;

    myShell.getArgs(check, args);
    if(myShell.exitstatus == false && string(myShell.userIn) != "exit")
        cout << myShell.userName + "@" + myShell.hostName + ":~" + getcwd(NULL, 0) + "/";
}

return 0;

and here is where the exit condition is being met immediately after calling getArgs():

void UNIX_shell::getArgs(string check, char * args[])
{
    cout << "check is" << check << endl;

    if(check[0] == 'e' && check[1] == 'x' && check[2] == 'i' && check[3] == 't')
    { 
        cout << "shadoom" << endl;
        this->exitstatus = true;
        cout << "exit = "<< exitstatus << endl;
        return;
    }

...

So if I run my program and enter something like ls, or ps -aef, and then type exit, everything runs fine, and the program exits.

But if i run my program, enter "shit" in the command line, then enter "exit" at the next prompt, i get a second prompt, then when i type exit again, it exits. I've been trying to debug this for over an hour. Thanks for any help in advance.

LogicStuff
  • 19,397
  • 6
  • 54
  • 74
Mars01
  • 79
  • 2
  • 10
  • 2
    http://stackoverflow.com/help/mcve – user657267 Feb 24 '15 at 08:22
  • 2
    Your code doesn't look like it actually compiles - did you just copy and paste various bits together that kind of belong together, but aren't actually the program as it stands? You seem to be mixing `std::string` with `char *` in ways that shouldn't be necessary... Which may or may not be the problem. – Mats Petersson Feb 24 '15 at 08:46
  • You should compile with all warnings & debug info (`g++ -Wall -Wextra -g`) then **use the debugger** (`gdb`) – Basile Starynkevitch Feb 24 '15 at 10:21
  • @MatsPetersson can you elaborate on mixing std::string with char * in ways that shouldn't be necessary...? I come from using java primarily and I'd like some pointers...on pointers – Mars01 Feb 24 '15 at 11:03
  • Well, typically (in my view), C++ has very little reason to use `char*` for string handling. If you are passing a C style string into an API, sure - but that only needs `std::string::c_str` [that's the whole point of that function!] – Mats Petersson Feb 24 '15 at 20:56
  • thanks for your input all, I've narrowed it down, im getting stuck on a wait() call. I've created a new thread with code to reproduce. Thanks again. http://stackoverflow.com/questions/28713400/program-stuck-on-wait – Mars01 Feb 25 '15 at 07:29

1 Answers1

1

As suggested by Mats, it is preferable to avoid char* and stick to strings.

Here is a sample code doing the same thing you do and using strings.

The following function will split a string at every char delim (e.g. spaces) and fill elems with the resulting pieces. I obtained it from here https://stackoverflow.com/a/236803/75517.

std::vector<std::string> &split(const std::string &s, char delim, std::vector<std::string> &elems) 
{
    elems.clear();
    std::stringstream ss(s);
    std::string item;
    while(std::getline(ss, item, delim)) 
        elems.push_back(item);
    return elems;
}

This would be your main code looping over the input strings provided by the user.

std::string inputStr;
std::vector<std::string> args;

for(;;)
{
    std::getline(std::cin, inputStr);
    if(inputStr == "exit")
       break;
    split(inputStr, ' ', args);
    ...
}
Community
  • 1
  • 1
chmike
  • 20,922
  • 21
  • 83
  • 106
  • Thanks for the input, I appreciate it. I think I'll have to leave what I have here unfortunately and take this as a lesson. In the main bulk of code (not posted here obviously) I'm using an istringstream to pass strings to args. The book this project is coming from gave examples using char* which is why it's sort of thrown in here initially. – Mars01 Feb 24 '15 at 11:09
  • Ok. The code you show seams correct to me. The problem is elsewhere. – chmike Feb 24 '15 at 15:56
  • Thanks again, I've narrowed it down and the problem is being stuck on a wait() call. I've created a new thread. http://stackoverflow.com/questions/28713400/program-stuck-on-wait – Mars01 Feb 25 '15 at 07:30