Hello!
Here is a small program to demonstrate my problem.
*** BEGIN
/* Program: menu_test.c
* Date: 9 January 2002
*/
/* headers */
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define QUIT 3 /* number of choices */
/* func proto */
int file_menu_choice (int quit);
int get_menu_choice (int quit);
/* MAIN */
int
main (void) {
int x, mol = 0;
int choice = 0;
int f_choice = 0;
int total_time;
time_t t_begin, t_end;
time(&t_begin);
while ((choice = get_menu_choice(QUIT)) != QUIT) {
switch (choice) {
case 1:
while ((f_choice = file_menu_choice(2)) != 2) {
switch (f_choice) {
case 1:
puts ("Extracting single...");
puts ("Returning to main menu...");
f_choice = 2;
break;
default:
puts ("Enter number 1 - 2");
break;
}
}
break;
case 2:
puts ("\tData printed! Bonds must be analyzed\n");
break;
default: printf ("Invalid choice (1 - %d)\n\n", QUIT);
break;
}
}
time(&t_end);
total_time = t_end - t_begin;
printf ("Total runtime is %4d sec\n\n", total_time);
fflush (stdout);
return 0;
Quote:
}
int
get_menu_choice (int quit) {
int selection = 0;
do {
puts ("\n=====================");
puts ("1 - Enter coordinates");
puts ("2 - Print data");
printf ("%1d - Quit\n", quit);
puts ("=====================");
puts ("\nEnter selection");
printf (">>> ");
scanf ("%d", &selection);
} while (selection < 1 || selection > quit);
return selection;
Quote:
}
int
file_menu_choice (int quit) {
int selection = 0;
do {
puts ("\n=====================");
puts ("1 - Enter single file");
printf ("%1d - Back\n", quit);
puts ("=====================");
puts ("\nEnter selection");
printf (">>> ");
scanf ("%d", &selection);
} while (selection < 1 || selection > quit);
return selection;
Quote:
}
*** END
The problem is: in the second choice, I would like to exit if I select
choice 1. Program should do what it has to do and then exit to main
menu. But insted it remains in that menu and I have to push 2 to return.
bash-2.02$ menu_test
=====================
1 - Enter coordinates
2 - Print data
3 - Quit
=====================
Enter selection
Quote:
>>> 1
=====================
1 - Enter single file
2 - Back
=====================
Enter selection
Quote:
>>> 1
Extracting single...
Returning to main menu...
=====================
1 - Enter single file
2 - Back
=====================
Enter selection
Quote:
>>> 2
=====================
1 - Enter coordinates
2 - Print data
3 - Quit
=====================
Enter selection
Quote:
>>> 3
Total runtime is 4 sec
I know that the problem lies in assigning the f_choice, where while
calls the function "file_menu_choice" every time. How can I solve or
override this selection to force while loop to fall through.
Thanx for the answers.
CCCao
--
miha