Enter a menu

Hi there,
I'm an absoulte beginner in C programming and in a week I have a project to hand in, but not much time available as I have a ton of other projects due in less than 2 weeks.
I've only managed to work out how to create a program menu, but I cannot figure out how to enter an option and make it a menu in itself.
For example, I want to enter option 1.LIGHTS and from there I want to enter into the menu LIGHTS that will have another set of options (that I'll add later). Can anybody please help me figure out the codes for entering that chosen menu and also the option to take me out if it, back to the MAIN MENU? Thank you ever so much!

This is what I worked out by now:

#include
#include "stdafx.h"

int _tmain(int argc, _TCHAR* argv[])

{
int iResponse = 0;
printf("
M A I N M E N U
");
printf("
1 LIGHTS
");
printf("2 GAS CONSUMPTION
");
printf("3 BLINDS
");
printf("4 SECURITY SYSTEM
");
printf("5 TEMPERATURE
");

printf("
Please enter your selection: ");
scanf("%d", &iResponse);

if (iResponse == 1)
printf("
L I G H T S
");

if (iResponse == 2)
printf("
G A S C O N S U M P T I O N
");

if (iResponse == 3)
printf("
B L I N D S
");

if (iResponse == 4)
printf("
S E C U R I T Y S Y S T E M
");

if (iResponse == 5)
printf("
T E M P E R A T U R E
");

Comments

  • [color=Blue]Use functions.
    They good and provide readability to the code.
    Notice how [b]Menu[/b] function displays and runs both main menu and lights menu. The only difference is in arrays containing menu strings.[/color]
    [code]
    char* MainMenu [] =
    {
    "MAIN MENU",
    "LIGHTS",
    "GAS CONSUMPTION",
    "BLINDS",
    "SECURITY SYSTEM",
    "TEMPERATURE",
    "QUIT PROGRAM"
    };

    char* LightsMenu [] =
    {
    "LIGHTS MENU",
    "MENU ITEM #1",
    "MENU ITEM #2",
    "MENU ITEM #3",
    "RETURN BACK TO MAIN MENU"
    };

    int Menu (char* items [], int count)
    {
    int i, active, response;
    [color=Green]//
    // Print menu title (1st item in array)
    //[/color]
    printf ("

    %s
    ", items [0]);
    [color=Green]//
    // Print the rest (starting from 2nd item)
    //[/color]
    for (i = 1; i < count; i++)
    {
    printf ("
    %d %s", i, items [i]);
    }
    [color=Green]//
    // Ask for a choice until proper one will be entered
    //[/color]
    active = 1;
    printf ("
    ");
    while (active == 1)
    {
    printf ("
    PLEASE ENTER SELECTION #: ");
    scanf ("%d", &response);
    if ((response >= 1) && (response < count)) break;
    [color=Green]//
    // Improper value entered
    //[/color]
    printf ("
    SELECTION MUST BE IN RANGE: [1..%d].", count-1);
    }
    return response;
    }

    void DoLightsMenu ()
    {
    int cmd, running=1;
    while (running == 1)
    {
    cmd = Menu (LightsMenu, 5);
    switch (cmd)
    {
    case 1: [color=Green]// MENU ITEM #1[/color]
    break;

    case 2: [color=Green]// MENU ITEM #2[/color]
    break;

    case 3: [color=Green]// MENU ITEM #3[/color]
    break;

    case 4: [color=Green]// back to main menu[/color]
    running = 0;
    break;
    }
    }
    }

    int main ()
    {
    int cmd, running=1;
    while (running == 1)
    {
    cmd = Menu (MainMenu, 7);
    switch (cmd)
    {
    case 1: DoLightsMenu ();
    break;

    case 2: [color=Green]// GAS[/color]
    break;

    case 3: [color=Green]// BLINDS[/color]
    break;

    case 4: [color=Green]// SECURITY[/color]
    break;

    case 5: [color=Green]// TEMPERATURE[/color]
    break;

    case 6: [color=Green]// Stop the program[/color]
    running = 0;
    break;
    }
    }
    }
    [/code]
  • Thank you ever so much for your response! It helps a lot.
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories