470,850 Members | 1,233 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,850 developers. It's quick & easy.

Not exiting the minishell results in printing the current working directory twice

I am trying to implement a minishell where if the user wants to exit the programm he is asked if he really wants to exit.

Expand|Select|Wrap|Line Numbers
  1. int main(int argc, char * argv[]){  
  2.  
  3.     char cwd[256]; char input[256]; 
  4.     char *command;  
  5.  
  6.     while(1) {     
  7.  
  8.         //return user-info and current working directory 
  9.         getcwd(cwd, sizeof(cwd));     
  10.          printf("%s@miniShell:%s$ ", getpwuid(getuid())->pw_name, cwd);      
  11.  
  12.         fgets(input, 256, stdin);     
  13.         command = strchr(input, '\n');      
  14.  
  15.         if(command){         
  16.             *command = '\0';
  17.         }      
  18.  
  19.         if(strncmp(input, "exit",4)==0){         
  20.             cout << "Do you want to quit (y/n) ?" << endl;          
  21.             string isExit = "";         
  22.             cin >> isExit;          
  23.             if(isExit == "y"){             
  24.                 exit(0);         
  25.             }         
  26.             continue;     
  27.         }  
  28.     }   
  29.     return 0;
  30. }
Exiting the programm works very well but if the user chooses to not to exit the shell it results in printing the working directory in the next line twice. What am I doing wrong and how can I fix this?
Apr 28 '21 #1
1 1604
dev7060
575 Expert 512MB
Exiting the programm works very well but if the user chooses to not to exit the shell it results in printing the working directory in the next line twice.
What environment and IDE are you using?
May 6 '21 #2

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

6 posts views Thread by Peter Maas | last post: by
1 post views Thread by Tony | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.