Error dimensioning vectors in C ++

4

Greetings.

It's my first time in forums, I'm learning C ++, and I have had a problem; I have to do a program that at first allows me to add student data to an arrangement.

The problem is that it does not matter how many students you indicate to the program that I want to enter; because it allows me to enter only two students and then a window appears that says that the program stopped working.

I have tried to do it in two ways, with switch, and with switch and with functions, and in both cases the same thing happens to me; here I copy the code of the switch intent and functions, from now on thanks to whoever takes the time to help me.

#include<iostream>
#include<stdlib.h>

using namespace std;    
void agregar();

int main(){

    //muestra el menu, y permite ejecutar la opcion 8.(salir del programa)

    int opcion;

    cout << "Que desea hacer?" << endl;
    cout << endl;
    cout << "1.Aregar alumno" << endl;
    cout << "2.Agregar notas de alumno" << endl;
    cout << "3.modificar notas de alumno" << endl;
    cout << "4.Eliminar alumno" << endl;
    cout << "5.Mostrar lista de aprobados" << endl;
    cout << "6.Mostrar list de reprobados" << endl;
    cout << "7.Mostrar toda la lista de alumnos" << endl;
    cout << "8.Salir del programa" << endl;
    cout << endl;
    cin >> opcion;

    switch(opcion){
        case 1:{
            agregar();
            break;
        }
    }

    system ("pause");
    return 0;
}

void agregar(){

    // variables utilizadas en void agregar()
    int m = 0, n = 0, i, r = 0;
    string codigo[m], nombre[m], grado[m];

    cout << "Cuantos alumnos desea agregar? ";
    cin >> m;
    n = n + m;

    cout << "ingrese los datos: " << endl;
    for(i = 0; i < m; i++){
        cout << "nombre del alumno " << i + 1 << ": ";
        cin >> nombre[i];

    }

    cout << "Desea ingresar mas alumnos? ";
    cout << "1.SI";
    cout << "2.No";
    cin >> r;

    if(r = 1){
        m = 0;
        void agregar();
    }else{
        m = 0;
        int main();
    }
    system ("pause");
}
    
asked by Codigo 24.05.2017 в 20:09
source

2 answers

2

Fixes of variable size (VLA in English ).

As you mentioned gbianchi your problem lies in the declaration of the arrangements, therefore, change the order in which the size of the arrays is requested and its definition can solve the problem, as long as your compiler supports Arrays of variable size (AtV).

AtVs do not belong to the standard C ++ but they are an extension of the compiler (read about the extension in the GCC compiler ). Each compiler has its own extensions so that if the code compiles you, your compiler has this extension, but in other compilers this extension could be disabled or not exist and it would show you an error. So: your code would not be portable.

Proposal.

Use std::vector , this template from the standard C ++ library can be used as an array and can be resized at run time:

using vector_string = std::vector<string>;

// variables utilizadas en void agregar()
int m = 0, n = 0, i, r = 0;
vector_string codigo, nombre, grado;

std::cout << "Cuantos alumnos desea agregar? ";
std::cin >> m;
n = n + m;

codigo.resize(n);
nombre.resize(n);
grado.resize(n);

The std::vector::resize method resizes the vector so that it is able to store the number of elements indicated. Afterwards, the insertion of data remains unchanged:

std::cout << "ingrese los datos: " << std::endl;
for(i = 0; i < m; ++i){
    std::cout << "nombre del alumno " << i + 1 << ": ";
    std::cin >> nombre[i];
}

Other things to consider.

  • It is not advised to use using namespace std , read this question to find out why.
  • It is also not recommended to use std::endl since it does flush of stream , which may cause performance problems, read this question for more details.
  • Use the pre-increment instead of the post-increment, this article explains why.
  • This instruction:

    if(r = 1){
    

    It will always be true, because you are assigning the value 1 to the variable r . After assigning, r is evaluated as Boolean data. In C ++ any non-zero value ( 0 ) is considered true ( true ). If you want to make a comparison use the double equal ( == ):

    if(r == 1){
    
answered by 25.05.2017 в 08:50
1

The error is in the initialization of the vectors:

// variables utilizadas en void agregar()
    int m = 0, n = 0, i, r = 0;
    string codigo[m], nombre[m], grado[m];

    cout << "Cuantos alumnos desea agregar? ";
    cin >> m;
    n = n + m;

    cout << "ingrese los datos: " << endl;
    for(i = 0; i < m; i++){
        cout << "nombre del alumno " << i + 1 << ": ";
        cin >> nombre[i];

    }

the vector name where you try to save the names is initialized to 0 (with 0 elements), therefore, when you ask how many students you want to enter, that does not change the size of the vector because it was initialized before.

You should start the vector when you really know how many students you want to enter. Just modify the order of your code.

// variables used in void add ()         int m = 0, n = 0, i, r = 0;

    cout << "Cuantos alumnos desea agregar? ";
    cin >> m;
    n = n + m;
    string codigo[m], nombre[m], grado[m];
    cout << "ingrese los datos: " << endl;
    for(i = 0; i < m; i++){
        cout << "nombre del alumno " << i + 1 << ": ";
        cin >> nombre[i];

    }

In no case, anyway, I'm looking at the rest of the code, where there are several more errors.

look for example here:

if(r = 1){
        m = 0;
        void agregar();
    }else{
        m = 0;
        int main();
    }

You are recursively calling to add, but you are already within that function .. all these calls should be structured in another way. for example within a while that knows when to finish the execution of all the points. you are also doing m = 0 without any apparent sense, because when you call the function again, the m is different, because it is in another scope.

My advice is to read a little more about how the language works.

    
answered by 24.05.2017 в 21:51