switch inside another switch

0

My problem is this: I want to make a submenu within the menu in my program (I had not done it before). I can not find my mistake I would appreciate it to anyone who can notice it. I send them the main of the program. Thank you very much.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "header.h"
int main()
{
    int option,option2,option3;
    T_empleados reg;
    T_estudiantes reg1;
    do
    {
        printf("Ingrese la operacion que desee realizar:");
        printf("\n 1: Cargar los archivos:");
        printf("\n 2: Mostrar los archivos:");
        printf("\n 3: Actualizar(con un 7.8%) los sueldos de aquellos
               estudaintes que superen un promedio de 7:");
        printf("\n 4: SALIR.\n");
        scanf("%d",&option);
        switch(option)
        {
        case 1:
            do
            {
                printf("\n 11: Cargar el archivo 'empleados':");
                printf("\n 12: Cargar el archivo 'estudiantes':");
                printf("\n 13 Volver al menu principal.\n");
                scanf(&option2);
                switch(option2)
                {
                case 11:
                    pfemp=fopen("empleados","wb");
                    if(!pfemp)
                    {
                        printf("No se ha podido abrir el archivo.");
                        exit(1);
                    }
                    while(Carga_archivo_empleado(&reg)==1)
                    {
                        fwrite(&reg,sizeof(T_empleados),1,pfemp);
                    }
                    fclose(pfemp);
                    break;
                case 12:
                    pfest=fopen("estudiantes","wb");
                    if(!pfemp)
                    {
                        printf("No se ha podido abrir el archivo.");
                        exit(1);
                    }
                    while(Carga_archivo_estudiante(&reg1)==1)
                    {
                        fwrite(&reg1,sizeof(T_estudiantes),1,pfest);
                    }
                    fclose(pfest);
                    break;
                }
            }
            while(option2!=13);
            break;
        case 2:
            do
            {
                printf("\n 21: Mostrar el archivo 'empleados':");
                printf("\n 22: Mostrar el archivo 'estudiantes':");
                printf("\n 23 Volver al menu principal.\n");
                scanf(&option3);
                switch(option3)
                {
                case 21:
                    pfemp=fopen("empleados","rb");
                    if(!pfemp)
                    {
                        printf("No se ha podido abrir el archivo.");
                        exit(1);
                    }
                    fread(&reg,sizeof(T_empleados),1,pfemp);
                    printf("Apellido\tNombre\tDNI\tSueldo");
                    while(!feof(pfemp))
                    {
                        Mostrar_archivo_empleado(&reg);
                        fread(&reg,sizeof(T_empleados),1,pfemp);
                    }
                    fclose(pfemp);
                    break;
                case 22:
                    pfest=fopen("estudiantes","rb");
                    if(!pfest)
                    {
                        printf("No se ha podido abrir el archivo.");
                        exit(1);
                    }
                    fread(&reg1,sizeof(T_estudiantes),1,pfest);
                    printf("Apellido\tNombre\tDNI\tPromedio");
                    while(!feof(pfest))
                    {
                        Mostrar_archivo_estudiante(&reg1);
                        fread(&reg1,sizeof(T_estudiantes),1,pfest);
                    }
                    fclose(pfest);
                    break;
                }
            }
            while(option2!=23);
            break;
        case 3:
            Actualizar_archivos(&reg,&reg1);
            break;
        }
    }
    while(option!=4);

    return 0;
}
    
asked by micaela 12.09.2017 в 00:43
source

1 answer

1

As well as many other errors and problems in your code, I focus on the one you mention:

    case 2:
        do
        {
            scanf(&option3);
            switch(option3)
            {
              // ...
             }
        }
        while(option2!=23);
        break;

scanf is updating option3 and, however, the loop expects a specific value in option2 .

Please, do not nest switchs ... do not have functions of more than 20 or 30 lines. Divide the program into functions ... your health and your notes will thank you.

    
answered by 12.09.2017 в 01:52