Variable returns to 0 when returning to main menu C #

1

Good, my problem is that in the part of the code that appears the first if of Case 1: says if (pos < vectorsize) this I do so that the user can not add more data and does not exceed the limit,

The problem is that when you return to the main menu, the variable pos returns to 0 so it will never get to the else where it shows the error of limit execido, my question is How do I make it so that Do not go back to 0 every time you enter the menu?

static void Main(string[] args)
{
    int oba = 0, obb = 0, obc = 0, pra = 0, prb = 0, prc = 0, proy = 0;
    int opcion = 0;
    int confirmacion = 0;
    int ci = 0;
    int vectorsize = 0;
    int pos = 0;
    if (vectorsize == 0)
    {
        Console.WriteLine("Ingrese la cantidad de alumnos.");
        vectorsize = Convert.ToInt32(Console.ReadLine());
    }

    double[] alumnos = new double[vectorsize];
    while (opcion != 4)
    {
        string opcionsino = "SI";
        Console.Clear();
        //Console.WriteLine(vectorsize);
        Console.WriteLine("Bienvenido");
        Console.WriteLine("");
        Console.WriteLine("1-> Agregar alumno.");
        Console.WriteLine("2-> Asignar notas.");
        Console.WriteLine("3-> Informe.");
        Console.WriteLine("4-> Salir.");
        Console.WriteLine("");
        Console.WriteLine("Elija una opcion.");
        Console.WriteLine(pos);
        opcion = Convert.ToInt32(Console.ReadLine());
        switch (opcion)
        {
            case 1:
            {
                Console.Clear();
                for (int i = 0; opcionsino == "SI" || opcionsino == null; i++)
                {
                    if (pos < vectorsize)
                    {
                        for (int i2 = 0; i2 < alumnos.Length && alumnos[i2] != 0; i2++)
                            pos = i2 + 1;
                        Console.Clear();
                        Console.WriteLine("Ingrese la cedula del alumno sin puntos ni guiones Ej: 12345678");
                        ci = Convert.ToInt32(Console.ReadLine());
                        for (int i3 = 0; i3 < alumnos.Length; i3++)
                        {
                            confirmacion = 0;
                            //int control = 0;
                            if (ci == alumnos[i3])
                            {
                                Console.Clear();
                                Console.WriteLine("Alumno ya ingresado.");
                                Console.ReadLine();
                                confirmacion = 7;
                                break;
                            }
                        //control++;
                        }
                    }
                    else
                    {
                        Console.WriteLine("Limite exidido.");
                        break;
                    }

                    if (confirmacion != 7)
                    {
                        alumnos[pos] = ci;
                        pos = pos + 1;
                    }

                    Console.WriteLine("¿Deseas agregar otro alumno? Si/No.");
                    opcionsino = Console.ReadLine();
                    opcionsino = opcionsino.ToUpper();
                }

                Console.Clear();
                Console.WriteLine("Precione una tecla para volver al menú principal.");
                Console.ReadLine();
                pos = 0;
                break;
            }
        }
    }
}

Thank you very much in advance.

    
asked by Federico Barrios 23.06.2016 в 20:58
source

2 answers

2

The problem is that you set the value of pos to 0 after reading from the keyboard to return to the main menu. Comment that line and it will no longer be reset to 0:

            Console.Clear();
            Console.WriteLine("Precione una tecla para volver al menú principal.");
            Console.ReadLine();
            // pos = 0;  <--- Aquí lo pones a 0, comenta esta línea
            break;
    
answered by 02.07.2016 в 12:39
0

- I propose these 5 corrections (I add comments in each point) ... -

static void Main(string[] args
    int oba = 0, obb = 0, obc = 0, pra = 0, prb = 0, prc = 0, proy = 0;
    int opcion = 0;
    int confirmacion = 0;
    int ci = 0;
    int vectorsize = 0;
    int pos = -1;
    if (vectorsize == 0)
    {
        Console.WriteLine("Ingrese la cantidad de alumnos.");
        vectorsize = Convert.ToInt32(Console.ReadLine());
    }

double[] alumnos = new double[vectorsize];

The double array has to be initialized (as compared to 0) to consider an element empty, they will be initialized in 0) ...

for (int cont = 0; cont < vectorsize; cont++)  
   alumnos[cont] = 0;

while (opcion != 4)
{
    string opcionsino = "SI";
    Console.Clear();
    //Console.WriteLine(vectorsize);
    Console.WriteLine("Bienvenido");
    Console.WriteLine("");
    Console.WriteLine("1-> Agregar alumno.");
    Console.WriteLine("2-> Asignar notas.");
    Console.WriteLine("3-> Informe.");
    Console.WriteLine("4-> Salir.");
    Console.WriteLine("");
    Console.WriteLine("Elija una opcion.");
    Console.WriteLine(pos);
    opcion = Convert.ToInt32(Console.ReadLine());
    switch (opcion)
    {
        case 1:
        {
            Console.Clear();
            for (int i = 0; opcionsino == "SI" || opcionsino == null; i++)
            {

If we manage pos as position (index + 1), we can not use it later as if it were the index. (Error: if (pos < vectorsize) ...

                if (pos <= vectorsize)
                {
                    for (int i2 = 0; i2 < alumnos.Length && alumnos[i2] != 0; i2++);

Only the position of the first element containing 0 will be assigned to pos, only if there is no previous position so that they can be entered values are students sequentially ...

                    if (pos == -1)                            
                       pos = i2 + 1;  

                    Console.Clear();
                    Console.WriteLine("Ingrese la cedula del alumno sin puntos ni guiones Ej: 12345678");
                    ci = Convert.ToInt32(Console.ReadLine());
                    for (int i3 = 0; i3 < alumnos.Length; i3++)
                    {
                        confirmacion = 0;
                        //int control = 0;
                        if (ci == alumnos[i3])
                        {
                            Console.Clear();
                            Console.WriteLine("Alumno ya ingresado.");
                            Console.ReadLine();
                            confirmacion = 7;
                            break;
                        }
                    //control++;
                    }
                }
                else
                {
                    Console.WriteLine("Limite exidido.");
                    break;
                }

                if (confirmacion != 7)
                {

The index must be set for the assignment, which is position-1, so that the value is not assigned to the next student by mistake (Error: alumnos[pos] = ci; ) ...

                    alumnos[pos-1] = ci;   
                    pos++; // << Más directo que : pos = pos + 1;      

                }

                Console.WriteLine("¿Deseas agregar otro alumno? Si/No.");
                opcionsino = Console.ReadLine();
                opcionsino = opcionsino.ToUpper();
            }

            Console.Clear();
            Console.WriteLine("Precione una tecla para volver al menú principal.");
            Console.ReadLine();

The following line will be deleted to be able to enter student values sequentially ...

            // pos = 0;
            break;
            }
        }
    }
}
    
answered by 04.08.2016 в 09:05