473,396 Members | 2,099 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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 1884
dev7060
636 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

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

Similar topics

6
by: Shaun Heveron | last post by:
Hi, I'm working on a portable ANSI C++ application designed to compile and execute under Windows and LINUX/UNIX. Is there a portable way of obtaining the current working directory? Sorry, if...
6
by: Peter Maas | last post by:
My goal is to have the top level of a directory tree in the Python path without touching anything outside the directory. I tried to create .pth files with the top level path in every subdirectory...
1
by: Tony | last post by:
Guys How do I find the directory that my current application is running in? e.g. if i wanted to find the current working directory of regedit internally (i.e. within the regedit program) it...
2
by: ssjassal | last post by:
Is there a way to instruct Python to import modules from standard library even if there is one with the same name in the current working directory? I was trying to import BaseHTTPServer.py from...
4
by: Jeff | last post by:
I feel like I should have been able to figure this out but I can't seem to find any references on this topic. It seems like my current working directory is consistently a few directories up from...
2
by: Jon | last post by:
It appears that (windows) python searches in the current working directory before looking in the local site-packages directory, or that '.' comes first in sys.path? The problem arises when I made...
14
by: gio | last post by:
I have a problem and the solution should works under windows and unix OS. Suppose I have a program ex.c in the directory X (so the current working directory of ex.c is X). Also suppose I have...
3
by: duyanning | last post by:
I have written a pyhton script that will process data file in current working directory. My script is in an different directory to data file. When I debug this script using pdb within emacs, emacs...
2
by: Kakkanattu | last post by:
Hi, In desktop application, we use different ways to get the current execution directory. I. AppDomain.CurrentDomain.BaseDirectory II. Environment.CurrentDirectory...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.