LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices



Reply
 
Search this Thread
Old 01-24-2004, 01:38 AM   #1
natalinasmpf
Member
 
Registered: Dec 2003
Distribution: Slackware 9.1
Posts: 309

Rep: Reputation: 30
system() function in nested if else/case!


I am writing an ncurses program using gcc so I can help my sibling who can't memorise programs, commands and directories by commandline (especially in a simple X enviroment or console mode), its under construction, having several tiers.

Now the first part is naturally some sort of menu gets displayed, which is the first case statement. It works fine. If I press F2 it gets F2, if I press F1 it goes to F1. (The other keys I haven't written for yet).

However in the second tier (ie. after you press F1 or F2 on the main menu), what happens is that using system(), if you press F2 for NetPanzer, it will execute the first case choice, in this case bzflag. For the second part (internet tools) as well. except pressing F1, F2, F3, or whatever keys go to amsn. Well at least I am glad the second part at least refers to within its own self. Rather than point all the way to the first part's bzflag.

I am deeply worried, because if the second tier doesn't work, what about the third tier? (since sometimes there will be more than 12 things in a single category).

I don't know, I looked throughout the code, put break statements where necessary, etc. but it won't resolve itself! If I put it in if-else statements as well. it will also do the same. Does system() not work in nested choice statements?

Thanks for any help!

Code:
#include <ncurses.h>
#include <stdlib.h>


int main()
{
	initscr();
        raw();
 	keypad(stdscr, TRUE);
	noecho();



	int twochara;

		start_color();  /* just for fun if you want colors */
		init_color(COLOR_RED, 850, 0, 0); /*defining colors*/
		init_color(COLOR_BLUE, 0, 250, 1000);
		init_color(COLOR_MAGENTA, 700, 0, 900);
		init_color(COLOR_CYAN, 0, 900, 900);
		init_color(COLOR_YELLOW, 900, 900, 0);
		init_color(COLOR_GREEN, 100, 900, 100);
		init_color(COLOR_WHITE, 1000, 1000, 1000);

        	init_pair(1, COLOR_RED, COLOR_BLACK); /*defining color pairs*/
		init_pair(2, COLOR_BLUE, COLOR_BLACK);
		init_pair(3, COLOR_MAGENTA, COLOR_BLACK);
		init_pair(4, COLOR_CYAN, COLOR_BLACK);
		init_pair(5, COLOR_YELLOW, COLOR_BLACK);
		init_pair(6, COLOR_GREEN, COLOR_BLACK);
		init_pair(7, COLOR_WHITE, COLOR_BLACK);

		attron(COLOR_PAIR(7));

	mvprintw(0, (COLS-65)/2, "\t\tWelcome to the Ncurses Shell Menu Replacement by Nat!\n\n");
		attroff(COLOR_PAIR(7));

			attron(COLOR_PAIR(2));
	printw("Press F1 for Games\n");
			attroff(COLOR_PAIR(2));

			attron(COLOR_PAIR(3));
	printw("Press F2 for Internet Tools, (instant messenging like AMSN, email, etc.)\n");
			attroff(COLOR_PAIR(3));

			attron(COLOR_PAIR(4));
	printw("Press F3 for Multimedia (music, videos, volume control, etc.)\n");
			attroff(COLOR_PAIR(4));

			attron(COLOR_PAIR(5));
	printw("Press F4 for Office Tools (word processors, editors, excel, etc.) \n");
			attroff(COLOR_PAIR(5));

			attron(COLOR_PAIR(6));
	printw("Press F5 for System Settings (prepare to use root access for some)\n");
			attroff(COLOR_PAIR(6));

			attron(COLOR_PAIR(1));
	printw("Press F6 for Programming and Development tools (ie. cvs)\n");
			attroff(COLOR_PAIR(1));
 int onechara;
	onechara = getch();
	int f1case=LINES;
	int f2case=LINES;
	switch(onechara)
	{

		case KEY_F(1): /*When F1 is Pressed */

			mvprintw(0,0,"\n"); /* clears....*/
			while (f1case != 0)
			{
				printw(" \n");
				f1case--;
				}               /*...screen for following menu */

			attron(COLOR_PAIR(7));
			mvprintw(0,(COLS-31)/2, "Games (More will be added soon!)\n");
			attroff(COLOR_PAIR(7));

			attron(COLOR_PAIR(2));
			printw("Press F1 for Bzflag\n");
			attroff(COLOR_PAIR(2));

			attron(COLOR_PAIR(3));
			printw("Press F2 for NetPanzer\n");
			attroff(COLOR_PAIR(3));

			attron(COLOR_PAIR(4));
			printw("Press F3 for Atlantik\n");
			attroff(COLOR_PAIR(4));

			attron(COLOR_PAIR(5));
			printw("Press F4 for Backgammon\n");
			attroff(COLOR_PAIR(5));

			attron(COLOR_PAIR(6));
			printw("Press F5 for Battlestar\n");
			attroff(COLOR_PAIR(6));

			attron(COLOR_PAIR(1));
			printw("Press F6 for Chess\n");
			attroff(COLOR_PAIR(1));

			int onetwochara;
			onetwochara = getch();

			switch(onetwochara)

			{
				case KEY_F(1):

					system("/usr/bin/bzflag");

					break;

				case KEY_F(2):

					system("/usr/bin/netpanzer");

					break;

				case KEY_F(3):

					system("/usr/bin/atlantik");

					break;

				case KEY_F(4):

					system("/usr/bin/backgammon");

					break;

				case KEY_F(5):

					system("/usr/bin/battlestar");

					break;

				case KEY_F(6):

					system("/usr/bin/gnuchess");

					break;

					default:

					printw("invalid key pressed!");

					break;
				}



		break;

		case KEY_F(2):

			mvprintw(0,0,"\n");
			while (f2case != 0)
			{
				printw(" \n");
				f2case--;
				}

	mvprintw(0, (COLS-14)/2, "Internet Tools\n");
		attroff(COLOR_PAIR(7));

			attron(COLOR_PAIR(2));
			printw("Press F1 for AMSN\n");
			attroff(COLOR_PAIR(2));

			attron(COLOR_PAIR(3));
	printw("Press F2 for Mozilla\n");
			attroff(COLOR_PAIR(3));

			attron(COLOR_PAIR(4));
	printw("Press F3 for Konqueror\n");
			attroff(COLOR_PAIR(4));

			attron(COLOR_PAIR(5));
	printw("Press F4 for Gaim\n");
			attroff(COLOR_PAIR(5));

			attron(COLOR_PAIR(6));
	printw("Press F5 for Xchat (IRC client)\n");
			attroff(COLOR_PAIR(6));

			attron(COLOR_PAIR(1));
	printw("Press F6 for Korn\n");
			attroff(COLOR_PAIR(1));

			int twochara;
			twochara = getch();

			switch(twochara)

			{
				case KEY_F(1):

					system("/usr/share/amsn/msn/amsn");

					break;

				case KEY_F(2):

					system("/usr/bin/mozilla");

					break;

				case KEY_F(3):

					system("/opt/kde/bin/konqueror");

					break;

				case KEY_F(4):

					system("/usr/bin/gaim");

					break;

				case KEY_F(5):

					system("/usr/bin/xchat");

					break;

				case KEY_F(6):

					system("/opt/kde/bin/korn");

					break;

					default:

					printw("invalid key pressed!");

					break;
				}
				break;

		default:
			printw("Rest coming soon, currently only F1 and F2 are available.");

			break;

}










/* Standard Termination */

	refresh();
        getch();
        endwin();

return 0;


}
 
Old 01-25-2004, 03:11 PM   #2
kev82
Senior Member
 
Registered: Apr 2003
Location: Lancaster, England
Distribution: Debian Etch, OS X 10.4
Posts: 1,263

Rep: Reputation: 50
can you possibly explain yourself a bit clearer because i dont understand from what youve written what your problem is, ive compiled your code and it works ok for me.
 
Old 01-25-2004, 03:33 PM   #3
natalinasmpf
Member
 
Registered: Dec 2003
Distribution: Slackware 9.1
Posts: 309

Original Poster
Rep: Reputation: 30
But see, if you go to the sub menus like Games or Internet Tools, and press the F keys (the ones defined like F1 F2) they will all execute as through you pressed F1 despite having pressed another key.
 
Old 01-25-2004, 03:50 PM   #4
h/w
Senior Member
 
Registered: Mar 2003
Location: New York, NY
Distribution: Debian Testing
Posts: 1,286

Rep: Reputation: 45
i can't test this on my current machine - but does selecting F2 and further selections all select F2 (like it does F1?)
 
Old 01-25-2004, 03:52 PM   #5
natalinasmpf
Member
 
Registered: Dec 2003
Distribution: Slackware 9.1
Posts: 309

Original Poster
Rep: Reputation: 30
It doesn't. I don't know whether its system() or ncurses, but luckily the problem stays within its own switch statement or nested if else.
 
Old 01-25-2004, 03:59 PM   #6
h/w
Senior Member
 
Registered: Mar 2003
Location: New York, NY
Distribution: Debian Testing
Posts: 1,286

Rep: Reputation: 45
so, everything works fine (any-tier down) after selecting any key except F1 first?
 
Old 01-25-2004, 04:03 PM   #7
natalinasmpf
Member
 
Registered: Dec 2003
Distribution: Slackware 9.1
Posts: 309

Original Poster
Rep: Reputation: 30
No, the following tiers, when calling the system() function, pressing F2, or F3, etc. seems to execute the same command as F1, seeing as it calls the same program, but within its own tier.

Although if you press a, or F9 or undefined keys, it will just give an invalid key error...
 
Old 01-25-2004, 04:15 PM   #8
h/w
Senior Member
 
Registered: Mar 2003
Location: New York, NY
Distribution: Debian Testing
Posts: 1,286

Rep: Reputation: 45
ok, again, just lookin at your code here -
may i ask how the ncurses libs loop through your routine (i haven't used ncurses yet)? for instance, i would have a while(1) loop for getting the inputs, while your code doesn't have anything but a routine to handle the first key interrupt (your first level). how does it advance to the next level without there being a loop?

i guess what i wanted to say was that your functions for the key-binding should be in a loop?
in case those initscr() and the other funcs provided by ncurses provides for that inherently, pls disregard my post.

Last edited by h/w; 01-25-2004 at 04:18 PM.
 
Old 01-25-2004, 04:21 PM   #9
natalinasmpf
Member
 
Registered: Dec 2003
Distribution: Slackware 9.1
Posts: 309

Original Poster
Rep: Reputation: 30
It patiently waits for the user to enter something. Which is the getch() function, which I think also ties in to refresh(). Ncurses is interesting, since I think its built in. When you press something, it refreshes, then comes back.

I don't see why I would neet a while loop, unless you mean something other than getch()? If the variable getch() gets = F1, F2, etc. it will advance depending on what the variable is. If its invalid, it will halt. After a time, it will execute the system() function, (after which ncurses will terminate having served its task, which is where the whole thing malfunctions.
 
Old 01-25-2004, 04:45 PM   #10
h/w
Senior Member
 
Registered: Mar 2003
Location: New York, NY
Distribution: Debian Testing
Posts: 1,286

Rep: Reputation: 45
from "man getch" - Depending on the setting of cbreak, this is after one character (cbreak mode), or after the first newline (nocbreak mode).
and from "man cbreak" - initially getch is in echo mode, so characters typed are echoed.

can u try using that function in <curses.h> (it's defined as 'int cbreak(void)')? mabe u can include a "cbreak()" before your getch()?

argghh, sorry i cant help more cos theres only so much i can do without running the code and testing. this would be my last post on this topic - so last thing - try out that cbreak() func.
 
Old 01-26-2004, 07:52 AM   #11
kev82
Senior Member
 
Registered: Apr 2003
Location: Lancaster, England
Distribution: Debian Etch, OS X 10.4
Posts: 1,263

Rep: Reputation: 50
by natalinasmpf
But see, if you go to the sub menus like Games or Internet Tools, and press the F keys (the ones defined like F1 F2) they will all execute as through you pressed F1 despite having pressed another key.

not on either of the machines ive compiled it on, it works fine. is your $TERM set up correctly for your type of terminal, are you running it remotely or locally?
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Why are all my upper case files being shown as lower case?? [Kernel 2.6.9-1.667 FC3] t3gah Fedora 4 03-11-2005 05:09 PM
Darwin Case System zaicheke *BSD 6 07-21-2004 11:47 PM
Perl exec function in linux (and system-function) nazula Programming 1 04-19-2004 01:21 PM
The case of the pulsing file system StonedZealot Slackware 1 05-03-2003 12:11 AM
Is there a way to duplicate my system to a CD so just in case i have to format the ma ForumKid Linux - General 3 12-28-2001 01:24 AM


All times are GMT -5. The time now is 10:46 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration