Loop 
Author Message
 Loop

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



Sun, 27 Jun 2004 19:32:34 GMT  
 Loop

Quote:
> Hello!

[snip code]

Quote:
> 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.

You can use the 'break' keyword (typically in conjunction
with some conditional test) to force the termination of
a loop.

while(some_condition)
{
   if(some_other_condition)
       break;

Quote:
}

-Mike


Mon, 28 Jun 2004 03:02:25 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. For loops into for loops

2. Loop or Loops in "C"

3. (Newbie) My Loop Isn't Looping - Aargh!

4. SEMA - WHILE loop in a FOR loop !

5. For loops into for loops

6. wanted - C and/or Fortran source for 24-loop Livermore Loops

7. My Loop Now Loops! Thanks!

8. process loops but 'context' doesn't show me where it loops

9. Loop of loops. Any nice solution?

10. hate the do-while loop (Re: ugly loop; hate the ! operator)

11. For loop design ( Brief Lesson )

12. For loop design part two

 

 
Powered by phpBB® Forum Software