468,242 Members | 1,767 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,242 developers. It's quick & easy.

Creating Menus with Sub-menus

The following program allows a user to select a nursery rhyme from a menu. My task is to incorporate sub-menus (nursery rhyme and fairy tales) into the program. I've tried to do this but with no success as you can see below this program. Any help with this would be greatly appreciated.
If anyone has a non-related menu program i could have a look at as a guide to help me learn how to do this, i would be greatful.

I use Dev-C++
programming in C

[code]
#include<stdio.h>
#include<stdlib.h>

/* Prototype */
int f_menu();
void f_mary ();
void f_jack ();
void f_bopeep ();

int main (void)
{
int choice_d;
do
{
choice_d = f_menu ();
switch (choice_d)
{

case 1: f_mary ();
break;

case 2: f_jack ();
break;

case 3: f_bopeep ();
break;

case 4: printf ("Bye Bye\n\n");
break;

}
}
while (choice_d != 4);
system ("Pause");
return 0;
}

/* ************************************************** ******************* */
int f_menu ()
{
int select_d;

do
{
printf ("This is a nursery rhyme program\n");
printf ("You can choose to display three different nursery rhymes\n\n");

printf ("(1) Mary had a little lamb\n(2) Jack and Jill\n(3) Little Bo Peep\n(4) Quit\n\n");

printf ("Enter a number that corresponds to your choice > ");
scanf ("%d", &select_d);
printf("\n");

if (( select_d < 1 ) || ( select_d > 4));
printf("You have entered an invalid choice. Please try again\n\n\n");
}
while (( select_d < 1) || ( select_d > 4));

return select_d;
}

/* ************************************************** ************************** */
void f_mary ()
{
printf("Mary had a little lamb\n fleese white as snow\n\n\n");
}
/* ************************************************** ************************** */
void f_jack ()
{
printf("Jack and jill went up the hill to fetch water\nThen they fell down\n\n\n");
}
/* ************************************************** ************************** */
void f_bopeep ()
{
printf("Little bo peep had sheep\nthen they came home\n\n\n");
}
[code]

My Attempt


[code]

#include<stdio.h>
#include<stdlib.h>

/* Prototype */
int f_menu();
int f_rhymemenu();
int f_rhymemenu2();
int f_fairymenu();
int f_fairymenu2();



int main (void)
{
int choice_d;
do
{
choice_d = f_menu ();
switch (choice_d)
{

case 1: f_rhymemenu ();
break;

case 2: f_fairymenu ();
break;

case 3: printf ("Bye Bye\n\n");
break;

}
}
while (choice_d != 3);
system ("Pause");
return 0;
}

/* ************************************************** ******************* */

int f_menu ()
{
int select_d;

do
{
printf ("This is a nursery rhyme and fairy stories program\n");
printf ("You can choose to display a number of different nursery rhymes and fairy stories\n\n");

printf ("(1) Nursery rhymes\n(2) Fairy stories\n(3) Quit\n\n");

printf ("Enter a number that corresponds to your choice > ");
scanf ("%d", &select_d);
printf("\n");

if (( select_d < 1 ) || ( select_d > 3));
printf("You have entered an invalid choice. Please try again\n\n\n");
}
while (( select_d < 1) || ( select_d > 3));

return select_d;
}

/* ************************************************** ************************** */
int f_rhymemenu ()
{
int f_rhymemenu2();
int choicerhyme_d;
void f_mary ();
void f_jack ();
void f_bopeep ();
do
{
choicerhyme_d = f_rhymemenu2();
switch (choicerhyme_d)
{

case 1: f_mary ();
break;

case 2: f_jack ();
break;

case 3: f_bopeep ();
break;

case 4: printf ("Bye Bye\n\n");
break;

}
}
while (choicerhyme_d != 4);
}
/* ************************************************** ******************* */
int rhymemenu2()
{
int select_d;

do
{

printf ("(1) Mary had a little lamb\n(2) Jack and Jill\n(3) Little Bo Peep\n(4) Quit\n\n");

printf ("Enter a number that corresponds to your choice > ");
scanf ("%d", &select_d);
printf("\n");

if (( select_d < 1 ) || ( select_d > 4));
printf("You have entered an invalid choice. Please try again\n\n\n");
}
while (( select_d < 1) || ( select_d > 4));

return select_d;
}

/* ************************************************** ************************** */
int f_fairymenu ()
{
int choicefairy_d;
void f_zub();
void f_bfg();
void f_aliens();
do
{
choicefairy_d = f_fairymenu2 ();
switch (choicefairy_d)
{

case 1: f_zub ();
break;

case 2: f_bfg ();
break;

case 3: f_aliens ();
break;

case 4: printf ("Bye Bye\n\n");
break;

}
}
while (choicefairy_d != 4);
}
/* ************************************************** ******************* */

int f_fairymenu2 ()
{
int select_d;

do
{

printf ("(1) The zub zubs\n(2) The BFG\n(3) Aliens\n(4) Quit\n\n");

printf ("Enter a number that corresponds to your choice > ");
scanf ("%d", &select_d);
printf("\n");

if (( select_d < 1 ) || ( select_d > 4));
printf("You have entered an invalid choice. Please try again\n\n\n");
}
while (( select_d < 1) || ( select_d > 4));

return select_d;
}

/* ************************************************** ************************** */
void f_mary ()
{
printf("Mary had a little lamb\n fleese white as snow\n\n\n");
}
/* ************************************************** ************************** */
void f_jack ()
{
printf("Jack and jill went up the hill to fetch water\nThen they fell down\n\n\n");
}
/* ************************************************** ************************** */
void f_bopeep ()
{
printf("Little bo peep had sheep\nthen they came home\n\n\n");
}
/* ************************************************** ************************** */
void f_zub ()
{
printf("zub zub zub zub zub zub zub zub\n\n\n");
}
/* ************************************************** ************************** */
void f_bfg ()
{
printf("im big and fat and a git\n\n\n");
}
/* ************************************************** ************************** */
void f_aliens ()
{
printf("take us to your leader\n\n\n");
}
[code]
Nov 21 '06 #1
1 11827
sivadhas2006
142 100+
Hi,

I did some small changes in your code to make it work.

Expand|Select|Wrap|Line Numbers
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3.  
  4. /* Prototype */
  5. int ShowMainMenu();
  6. int ShowRhymeMenu();
  7. int ShowRhymeSubMenu();
  8. int ShowFairyMenu();
  9. int ShowFairySubMenu();
  10.  
  11. void PrintZub();
  12. void PrintBigFatGit();
  13. void PrintAliens();
  14.  
  15. void PrintMary();
  16. void PrintJack();
  17. void PrintLittleBoPeep();
  18.  
  19. int main(void)
  20. {
  21.    int 
  22.       nChoice = 0;
  23.  
  24.    do
  25.    {
  26.       nChoice = ShowMainMenu ();
  27.       switch (nChoice)
  28.       {
  29.          case 1:
  30.          {
  31.             ShowRhymeMenu ();            
  32.          }
  33.          break;
  34.  
  35.          case 2: 
  36.          {
  37.             ShowFairyMenu ();
  38.          }
  39.          break;
  40.  
  41.          case 3: 
  42.          {
  43.             printf ("Bye Bye\n\n");
  44.          }
  45.          break;
  46.       }
  47.    }
  48.    while (nChoice != 3);
  49.    system ("Pause");
  50.    return 0;
  51.  
  52. /* ************************************************** ******************* */
  53.  
  54. int ShowMainMenu ()
  55. {
  56.    int 
  57.       nSelected = 0;
  58.  
  59.    do
  60.    {
  61.       printf ("This is a nursery rhyme and fairy stories program\n");
  62.       printf ("You can choose to display a number of different nursery rhymes and fairy stories\n\n");
  63.  
  64.       printf ("(1) Nursery rhymes\n(2) Fairy stories\n(3) Quit\n\n");
  65.  
  66.       printf ("Enter a number that corresponds to your choice > ");
  67.       scanf ("%d", &nSelected);
  68.       printf("\n");
  69.  
  70.       if (( nSelected < 1 ) || ( nSelected > 3))
  71.       {
  72.          printf("You have entered an invalid choice. Please try again\n\n\n");
  73.       }
  74.    }
  75.    while (( nSelected < 1) || ( nSelected > 3));
  76.  
  77.    return nSelected;
  78. }
  79.  
  80. /* ************************************************** ************************** */
  81. int ShowRhymeMenu ()
  82. {   
  83.    int 
  84.       nChoice = 0;   
  85.    do
  86.    {
  87.       nChoice = ShowRhymeSubMenu();
  88.       switch (nChoice)
  89.       {
  90.  
  91.          case 1:
  92.          {
  93.             PrintMary ();
  94.          }
  95.          break;
  96.  
  97.          case 2: 
  98.          {
  99.             PrintJack ();
  100.          }
  101.          break;
  102.  
  103.          case 3: 
  104.          {
  105.             PrintLittleBoPeep ();
  106.          }
  107.          break;
  108.  
  109.          case 4: 
  110.          {
  111.             printf ("Bye Bye\n\n");
  112.          }
  113.          break;
  114.       }
  115.    }
  116.    while (nChoice != 4);
  117.  
  118.    return nChoice;
  119. }
  120.  
  121. /* ************************************************** ******************* */
  122. int ShowRhymeSubMenu() 
  123. {
  124.    int 
  125.       nSelected = 0;
  126.  
  127.    do
  128.    {
  129.  
  130.       printf ("(1) Mary had a little lamb\n(2) Jack and Jill\n(3) Little Bo Peep\n(4) Quit\n\n");
  131.  
  132.       printf ("Enter a number that corresponds to your choice > ");
  133.       scanf ("%d", &nSelected);
  134.       printf("\n");
  135.  
  136.       if (( nSelected < 1 ) || ( nSelected > 4))
  137.       {
  138.          printf("You have entered an invalid choice. Please try again\n\n\n");
  139.       }
  140.    }
  141.    while (( nSelected < 1) || ( nSelected > 4));
  142.  
  143.    return nSelected;
  144. }
  145.  
  146. /* ************************************************** ************************** */
  147. int ShowFairyMenu ()
  148. {
  149.    int 
  150.       nChoice = 0;
  151.  
  152.    do
  153.    {
  154.       nChoice = ShowFairySubMenu ();
  155.       switch (nChoice)
  156.       {
  157.          case 1:
  158.          { 
  159.             PrintZub ();
  160.          }
  161.          break;
  162.  
  163.          case 2:
  164.          { 
  165.             PrintBigFatGit ();
  166.          }
  167.          break;
  168.  
  169.          case 3:          
  170.          {             
  171.             PrintAliens ();
  172.          }
  173.          break;
  174.  
  175.          case 4:
  176.          { 
  177.             printf ("Bye Bye\n\n");
  178.          }
  179.          break;
  180.       }
  181.    }
  182.    while (nChoice != 4);
  183.  
  184.    return nChoice;
  185. }
  186.  
  187. /* ************************************************** ******************* */
  188. int ShowFairySubMenu ()
  189. {
  190.    int 
  191.       nSelected = 0;
  192.  
  193.    do
  194.    {
  195.  
  196.       printf ("(1) The zub zubs\n(2) The BFG\n(3) PrintAliens\n(4) Quit\n\n");
  197.  
  198.       printf ("Enter a number that corresponds to your choice > ");
  199.       scanf ("%d", &nSelected);
  200.       printf("\n");
  201.  
  202.       if (( nSelected < 1 ) || ( nSelected > 4))
  203.       {
  204.          printf("You have entered an invalid choice. Please try again\n\n\n");
  205.       }
  206.    }
  207.    while (( nSelected < 1) || ( nSelected > 4));
  208.  
  209.    return nSelected;
  210. }
  211.  
  212. /* ************************************************** ************************** */
  213. void PrintMary ()
  214. {
  215.    printf("Mary had a little lamb\n fleese white as snow\n\n\n");
  216. }
  217.  
  218. /* ************************************************** ************************** */
  219. void PrintJack ()
  220. {
  221.    printf("Jack and jill went up the hill to fetch water\nThen they fell down\n\n\n");
  222. }
  223.  
  224. /* ************************************************** ************************** */
  225. void PrintLittleBoPeep()
  226. {
  227.    printf("Little bo peep had sheep\nthen they came home\n\n\n");
  228. }
  229.  
  230. /* ************************************************** ************************** */
  231. void PrintZub ()
  232. {
  233.    printf("zub zub zub zub zub zub zub zub\n\n\n");
  234. }
  235.  
  236. /* ************************************************** ************************** */
  237. void PrintBigFatGit ()
  238. {
  239.    printf("im big and fat and a git\n\n\n");
  240. }
  241. /* ************************************************** ************************** */
  242.  
  243. void PrintAliens ()
  244. {
  245.    printf("take us to your leader\n\n\n");
  246. }
  247.  
  248.  
Regards,
M.Sivadhas.
Nov 22 '06 #2

Post your reply

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

Similar topics

2 posts views Thread by DaWoE | last post: by
1 post views Thread by Joel Whitehouse | last post: by
5 posts views Thread by SalamElias | last post: by
2 posts views Thread by FFrozTT | last post: by
reply views Thread by NPC403 | last post: by
reply views Thread by kermitthefrogpy | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.