473,320 Members | 2,158 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,320 software developers and data experts.

Menu Hierarchy in C: avoid stack issues

Hello everyone, thanks for looking. I'll get right to the point: I'm having problems trying to figure out a hierarchy of menus in C; command-line style.

Say, for example the program analyzes a file of names/letters to figure out the patterns of letters within the names. The user should be able to do many things with the information such as add names, delete names, and add new letters (a letter is defined as any subset of a group of consonants or vowels) just to name a few.

I have all the underlying functions done (successfully analyzing the file various ways and saving the info).

The main problem I have is that I don't want the user to be able to navigate the menus in a way that it places a ton of menus on the stack.

An ideal use case stack would look like this:

Main Menu
--Names Database Menu
----Sort Names Menu
------Sort Names by length function
------(Original Sort Names Database removed from function stack)
----Sort Names Menu
----*Return to Main Menu option*
----(Original Names Database Menu removed from function stack
----(Second Sort Names Menu removed from function stack)
Main Menu

The problem I'm having is to figure out an intuitive method that would avoid a situation that has the potential to create a huge stack like this:

Main Menu
--Names Database Menu
----Sort Names Menu
------Sort Names by length function
----Sort Names Menu
------*Return to Main Menu Option chosen*
------Main Menu
--------etc, etc

I'm wondering if I should have an array of function pointers for each menu available (which is a lot) or if I should have different switch functions for each menu display function, or the other 6 options I can think of that I can't seem to work an easily understandable bit of pseudocode for.

Any help at all would be appreciated... I don't need working code. I'm just not sure how menus are really done in the real world and figured it would be an easy suggestion from those more experienced.
Apr 19 '07 #1
6 2009
Savage
1,764 Expert 1GB
Hi,

well,on your place I would create functions for menu creation with variable
number of options.With this you would be able to disable some previous option.

So we will have next situation:


menu;

choice 1;

menu;//with some options disbanded

choice 2;

.
.
.


If u are not sure or u don't know how to create such a function post here and
ask a specific question.

And when posting code please use code tags (# on main toolbar in message window)

Savage
Apr 19 '07 #2
Thanks for the fast response, :) I wasn't sure if the code segments applied to what I was typing before. I'll try to use it more often to make things clearer

As for the problem, in main I have:

Expand|Select|Wrap|Line Numbers
  1.       do
  2.       {
  3.          exit = mainMenu();
  4.       } while(exit != 1);
And mainMenu():

Expand|Select|Wrap|Line Numbers
  1. int mainMenu()
  2. {
  3.  
  4.      char choice;
  5.  
  6.      printf("a.  Enter Names Database\n");
  7.      printf("b.  Enter Letters Database\n");
  8.      printf("c.  Start Random Name Generator\n");
  9.      printf("q.  Quit Program\n");
  10.  
  11.      scanf("%c", &choice);
  12.  
  13.      switch(choice)
  14.      {
  15.        case 'a':
  16.             {
  17.                  nameMainMenu();
  18.                  return 0;
  19.                  break;
  20.             } //end case a
  21.        case 'b':
  22.             {
  23.                  letterMainMenu();
  24.                  return 0;
  25.                  break;
  26.             } //end case b
  27.        case 'c':
  28.             {
  29.                  printf("Starting Random Generator.\n");
  30.                  //I plan on having the Name Generator as a separate program that starts up on this call... I don't have a clue how to do it so haven't got to that yet.
  31.                  return 0;
  32.             } //end case c
  33.        case 'q':
  34.             {
  35.                  printf("Quitting program now...\nPress any key to continue...\n");
  36.                  return 1;
  37.                  break;
  38.             } //end case q
  39.        default:
  40.             {
  41.                  printf("Incorrect option chosen.  Please choose another option.");
  42.                  return 0;
  43.                  break;
  44.             } //end default
  45.      } //end switch
  46.  
  47. } //end function mainMenu
With underlying menus:
Expand|Select|Wrap|Line Numbers
  1. char nameMainMenu()
  2. {
  3.      char choice;
  4.  
  5.      printf("a.  Add Names File to Database\n");
  6.      printf("b.  Search For a Name\n");
  7.      printf("c.  Add a Name to Database\n");
  8.      printf("d.  Delete Name From Database\n");
  9.      printf("e.  Erase Names Database\n");
  10.      printf("f.  Modify Name in Database\n");
  11.      printf("g.  Sort Names for Display and Saving\n");
  12.      printf("s.  Save Names Database\n");
  13.      printf("m.  Return to Main Menu\n");
  14.  
  15.      scanf("%c", &choice);
  16.  
  17.      switch(choice)
  18.      {
  19.        case 'a':
  20.             {
  21.               analyzeNameMenu();
  22.               break;
  23.             } //end case a
  24.       /*snipped a bunch of case statements*/
  25.        case 'm':
  26.             {
  27.               mainMenu();
  28.               break;
  29.             } //end case m
  30.        default:
  31.             {
  32.               printf("Incorrect option chosen.  Choose another option.\n");
  33.               nameMainMenu();
  34.               break;
  35.             } //end default
  36.      } //end switch
  37.  
  38. } //end function nameMainMenu
And continuing on from there to other sub-menus. The problem I saw with the switch statements is that they just keep piling on top of each other since the previous function hasn't returned any value nor completed the call.

Will it correct itself if I set each menu to have a return to a loop similar to main in another function? Are these switch statements that spaghetti fairly quickly the best way to make menu hierarchies? Thanks a bunch!
Apr 19 '07 #3
Savage
1,764 Expert 1GB
1)Is it practical?


So let me get it clear:

with: do{
exit=menu();

}while(exit!=1);

u will call this function and if it return 0 u will again call that function that's
very practible if u menu will have a constant number of options but in your case
you want to dismiss some options so this wont work.

So instead u will need some type of identifer which will in depandacne of it's value call menu with different options.

2.)
Here comes experiance of those quite experienced pogrammers.On ur place
they would build up a totaly modular menu function with variable number of options,which will quite lower the code number of lines.

So instead of ur it would take a look as:

int menu(int nOfOptions,char*text,....);

with such menu function there is a great posibility of changing number of options
and text for each option using opereator '...'

and now to the code:

Expand|Select|Wrap|Line Numbers
  1. i=0;//this represent indentifer.
  2. do{
  3.       //because ur menu has need of changing options it will require:
  4.       if(i==0) call ur previous menu;
  5.       else call other menu.
  6.  
  7. }while(!exit);   
3.This is also a answe to this question.

Savage
Apr 19 '07 #4
Savage
1,764 Expert 1GB
About this topic:

printf("Starting Random Generator.\n");
//I plan on having the Name Generator as a separate program that
starts up on this call... I don't have a clue how to do it so haven't got to
that yet.
return 0;
Use execlp or spawnlp:

if the program name is generator.exe then it would be like:

Expand|Select|Wrap|Line Numbers
  1. spawnlp(P_OVERLAY,"generator.exe",argv[1],argv[2],NULL);
Note: Must define main as int main( int argc,char *argv[]) and run programm from DOS command line.

Savage
Apr 19 '07 #5
Awesome, this explains a bunch. Thanks a lot!
Apr 19 '07 #6
Savage
1,764 Expert 1GB
Allways a pleasure to help!!!!

:D

Savage
Apr 19 '07 #7

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

Similar topics

0
by: Humpty Dumpty | last post by:
Hi folks, here's a challenge: I have a dynamically created cascading menu in Tkinter that can be quite large because it is created from a file. I tried using lazy creation so only the menu item...
12
by: MP Multimedia | last post by:
Hello everyone, I need help. I'm using a hierarchical menu made in javascript. When I used it in a one frame page, it came out fine. But now I need to change my page to 3 frames: a top frame,...
28
by: Lachlan Hunt | last post by:
Hi, I've been trying, but failing to work out what is causing Opera to render my drop down menu incorrectly on my site. http://www.lachy.id.au/ For some reason, there seems to be extra margin...
6
by: | last post by:
I have a class with overloading operator new. (Because, if an identical object exists, return a pointer to existed object instead of a new pointer) It has no sense (it is dangerous) to allocate an...
7
by: Chuck Hartman | last post by:
I have a Windows service that requests web pages from a site using an HttpWebRequest object. When I try to request a page from an ASP.NET 2 site, I get a WebException with message "The remote...
2
by: Lorenzo Castelli | last post by:
This is an old problem of mine. Basically I have an abstract base class which represents a generic iterator over a collection of elements, and various derived classes that implement the...
0
by: Martin | last post by:
Hi, I want to get the following navigation effect. Can I do it with the new Menu control? Say I have a menu hierarchy like 1 1.1 1.2 1.3
5
by: rover8898 | last post by:
Hello all, I have a programming problem/challenge. Let explain my scenario: I have a C program (distributed accross many files and functions) that is responsible for handling hardware. This...
7
by: amit.atray | last post by:
Environement : Sun OS + gnu tools + sun studio (dbx etc) having some Old C-Code (ansi + KR Style) and code inspection shows some big size variable (auto) allocated (on stack) say for ex. char...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.