Results 1 to 6 of 6
Hi,
I am currently working in C in PuttY server.I have been asked to write a program that can display a clock on the right hand corner of the PuttY ...
- 08-18-2009 #1Just Joined!
- Join Date
- Jun 2009
- Posts
- 20
An interactive menu using C
Hi,
I am currently working in C in PuttY server.I have been asked to write a program that can display a clock on the right hand corner of the PuttY server and an interactive menu that interacts with the user.The menu may include certain LINUX commands.
I happened to come up with this program
However this is just a simple example of a switch case program.Code:#include <stdio.h> #include <unistd.h> #include <sys/types.h> int main() { int choice; do { printf("Main Menu \n"); printf("Please select an option that you need \n"); printf("1.Execute ls command \n"); printf("2.Execute pwd command \n"); printf("3.Execute who command \n"); printf("4.Execute hostname command \n"); printf("0. exit \n"); printf("Your choice: \n"); scanf("%d", &choice); //while (getchar() != ' '); switch (choice) { case 1: if (fork()) wait(0); else execlp("ls", "ls", (char *)NULL); break; case 2: if (fork()) wait(0); else execlp("pwd", "pwd", (char *)NULL); break; case 3: if (fork()) wait(0); else execlp("who", "who", (char *)NULL); break; case 4: if (fork()) wait(0); else execlp("hostname","hostname", (char *)NULL); case 0: break; default: printf("Please enter only 0-4"); } } while (choice != 0); }
So can someone help me out with this.
Thanks
- 08-18-2009 #2Linux Guru
- Join Date
- Apr 2009
- Location
- I can be found either 40 miles west of Chicago, or in a galaxy far, far away.
- Posts
- 8,974
School exercise? Please read the Terms of Service for this site. Asking for help with school work is not allowed.
That said, to do what you said you have been tasked with, you need to use a screen-formatting library such as ncurses so you can display the clock in the upper right corner of the putty terminal window, and such. Also, since main() returns in int, you need to return an integer value after the while loop.Sometimes, real fast is almost as good as real time.
Just remember, Semper Gumbi - always be flexible!
- 08-19-2009 #3Just Joined!
- Join Date
- Jun 2009
- Posts
- 20
Thanks for your advice....I'll look up the libraries....and its not a school exercise!!!
- 08-19-2009 #4Linux Guru
- Join Date
- Apr 2009
- Location
- I can be found either 40 miles west of Chicago, or in a galaxy far, far away.
- Posts
- 8,974
Ok. I know a lot of stuff I did in my early career looked equally "sophomoric", especially when I was trying to get a handle on these things, so I'll definitely give you the benefit of the doubt.

Using printf() statements to output a number of options for a program is a viable approach in the following conditions:
1. The program has a very limited set of options.
2. The program is "static", in that you won't be allowing much in the respect of user configuration (such as adding new commands).
3. You don't mind such an antique "green screen" dumb terminal look and feel.
Using a text-based screen formatting library such as ncurses will allow you to put output on the terminal where you want it, as well as the ability to use the cursor keys to select between menu items. Later, you can adapt it to a GUI programming API such as wxWindows or Qt to actually use the graphics displays so prevalent today.
Now, a couple of comments about your code. Instead of this construct:
use thisCode:case 1: if (fork()) wait(0); else execlp("ls", "ls", (char *)NULL); break;
That does the fork/exec that you are using much more simply for this sort of situation. You might want the excplicit fork/exec code in the future for some things, such as allowing more complex user input option processing, but leave that for later.Code:case 1: system("ls"); break;
As I said before, when a function (and main() is a function) returns a value, be sure that all exits from the function return a value of the correct type. In this case, main() returns an integer value. Return 0 when there is no problem, -1 when there is an unrecoverable error, some other value if you need to inform the calling program/shell of a specific situation or value you want to "export".
There are other minor issues, but I'll leave them for now, other than I don't see where you are outputting the date/time as you said you wanted to do.Sometimes, real fast is almost as good as real time.
Just remember, Semper Gumbi - always be flexible!
- 08-20-2009 #5Just Joined!
- Join Date
- Jun 2009
- Posts
- 20
Hi,
Thanks for your advice,but wat i was trying to say was that the program i had posted was not quite the solution.I'm looking for a program using graphics.h that can draw a border on the PuttY server screen and the menu options should appear at the centre of the screen and the clock at the top right corner.
So can someone help me out.I'm looking for an equivalent header for graphics.h in LINUX.I know ncurses is the solution but i still dont know how to implement it.Can anyone help me out.
Thanks
- 08-20-2009 #6Linux Guru
- Join Date
- Apr 2009
- Location
- I can be found either 40 miles west of Chicago, or in a galaxy far, far away.
- Posts
- 8,974
To do that (draw border around putty screen, etc) you would use ncurses for the character graphics, cursor positioning, etc. Look at the ncurses man page for some more information as to what it does. If it isn't installed on your system, you should be able to install it with your package manager. Since you didn't mention what distribution of Linux you are using I don't know which specifically you should use.
Sometimes, real fast is almost as good as real time.
Just remember, Semper Gumbi - always be flexible!


Reply With Quote
