3 Replies - 40048 Views - Last Post: 25 October 2007 - 03:45 PM Rate Topic: -----

#1 Nidza  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 7
  • Joined: 18-March 07

Making a "Menu" in C.

Posted 25 October 2007 - 01:21 PM

I'm pretty much new here, but not a programming beginner.So, I'm gonna try and make a little program called "Library".Its gonna work like a real library with books and stuff.You can add members, write tickets, take books etc.This ain't a big problem (just few structures and voila !), but thing that concerns me is a "MENU".It should be a chained menu (every option has more options ie. if you select "Members" you can choose "Add new", "List of existing", "Modify memeber's data" etc.).I haven't work with trees and lists, not sure if those would be helpfull for me.I'd write a code if i'd have it, but the menu is a basic thing of this program, all other member/book structures are a piece of cake.

I'm working on this program in C (djgpp compiler).Some of you will ask : "This program in JAVA ? GUIs are piece of cake in there!" It prolly is, but at the first year of UNI (which will be in a year) I'll have to pass the C exam first, then I can switch to any object-oriented programming language I want.

This isn't my homework, I'm just trying to expand my knowledge.Thanks in advance.

Nidza

Is This A Good Question/Topic? 0
  • +

Replies To: Making a "Menu" in C.

#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4332
  • View blog
  • Posts: 12,127
  • Joined: 18-April 07

Re: Making a "Menu" in C.

Posted 25 October 2007 - 02:16 PM

Hello,

The one programming area you will want to look at is the "switch" statement. Once you see it you will know exactly how it can fit into your menu project. The idea is that it evaluates a value and compares it against a list of "cases". So for instance you ask them for a choice and they enter it (something like 1...2...3 etc) and then you evaluate it in the switch. Based on the number they typed, you can call code. Here is the idea...


int main() {
	// Display the choices to the user
	cout << "Please choose from the menu below..." << endl;
	cout << "1. Checkout a book" << endl;
	cout << "2. Return a book" << endl;
	cout << "3. Exit" << endl;

	// Get and store their choice
	int choice = 0;
	cin >> choice;

	// Evaluate teh choice and call the selected functions
	switch (choice) {
		case 1:
		 	cout << "Choice 1 selected" << endl;
		 	break;
		case 2:
		 	cout << "Choice 2 selected" << endl;
		 	break;
		case 3:
		 	cout << "We would exit here" << endl;
		 	break;
		default:
		 	cout << "Other choice selected" << endl;
	}
	return 0;
}



Now of course in one of the choices instead of just writing "choice 1 selected" you would call another function which would show another menu and again start the process of asking for a choice from the user. Or in the case of choice 3, you would exit your program.

Hope this makes some sense!

Enjoy! :)
Was This Post Helpful? 0
  • +
  • -

#3 Nidza  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 7
  • Joined: 18-March 07

Re: Making a "Menu" in C.

Posted 25 October 2007 - 02:41 PM

View PostMartyr2, on 25 Oct, 2007 - 02:16 PM, said:

Hello,

The one programming area you will want to look at is the "switch" statement. Once you see it you will know exactly how it can fit into your menu project. The idea is that it evaluates a value and compares it against a list of "cases". So for instance you ask them for a choice and they enter it (something like 1...2...3 etc) and then you evaluate it in the switch. Based on the number they typed, you can call code. Here is the idea...


int main() {
	// Display the choices to the user
	cout << "Please choose from the menu below..." << endl;
	cout << "1. Checkout a book" << endl;
	cout << "2. Return a book" << endl;
	cout << "3. Exit" << endl;

	// Get and store their choice
	int choice = 0;
	cin >> choice;

	// Evaluate teh choice and call the selected functions
	switch (choice) {
		case 1:
		 	cout << "Choice 1 selected" << endl;
		 	break;
		case 2:
		 	cout << "Choice 2 selected" << endl;
		 	break;
		case 3:
		 	cout << "We would exit here" << endl;
		 	break;
		default:
		 	cout << "Other choice selected" << endl;
	}
	return 0;
}



Now of course in one of the choices instead of just writing "choice 1 selected" you would call another function which would show another menu and again start the process of asking for a choice from the user. Or in the case of choice 3, you would exit your program.

Hope this makes some sense!

Enjoy! :)

As I said I have been working in C for the past year and I know how is function "case" working.I was just wondering is it possible to make a "tree" in a bit...different(maybe simplier) way.

Anyway, thanks alot on a fast response.
Was This Post Helpful? 0
  • +
  • -

#4 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4332
  • View blog
  • Posts: 12,127
  • Joined: 18-April 07

Re: Making a "Menu" in C.

Posted 25 October 2007 - 03:45 PM

Well the best you can do to achieving a tree like system is by putting several of the menus in each of their own functions and control the flow to those functions. Based on one choice or another call this menu or that menu. The problem with C here is that it is still a bit linear in fashion (objects in C++ helped break that because you could have made menu objects and establish a hierarchy of objects). But even then you still have to get a choice from the user and either call the right menu subsystem or, in the case of c++, create the right objects and call the right methods to show the objects.

So this format is kinda the best way you have of implementing trees. Just control the calls to the functions that show the menus and you can still do quite a bit. Like an array of function calls (aka array of function pointers) or put the function pointers in a linked list of nodes to form a binary tree or something. But I think that is getting a bit ridiculous.

Hope I have helped! :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1