Print Multiple Data with a Vector in C ++

0

Good Night I have the following exercise in which I require that in the end I print the data (Name and Age) of people over 60 years.

Until the moment it does not do it correctly and it prints all the cases and of step the age is not the indicated one if not other numbers. How could I solve it?

int main(int argc, char const *argv[])
{
int i = 0, cpersonas_tercera = 0, edad[2], edad_tercera[2];
float tercera_edad = 0, promedio;
char nombre[2][30];

for (int i = 1; i <= 2; i++)
{
    system("cls");
    cout<<"< = = = D A T O S  P E R S O N A L E S = = = >";
    cout<<"\n\nNombre: ";
    cin>>nombre[i];
    cout<<"\nEdad: ";
    cin>>edad[i];

    if (edad[i] >= 60)
    {
        cpersonas_tercera++;
        tercera_edad = tercera_edad + edad[i];
    }
}

promedio = tercera_edad / cpersonas_tercera;

system("cls");
cout<<fixed<<setprecision(2);
cout<<"< = = = D A T O S  R E C O L E C T A D O S = = = >";
cout<<"\n\nCantidad de Personas de Tercera Edad (+60 Años): "<<cpersonas_tercera;
cout<<"\n\nPromedio de la Tercera Edad (+60 Años): "<<promedio;

for (i = 1 ; i <= 2 ; i++)
{
    if (edad[i] >= 60)
    {
        cout<<"\nNombre: "<<nombre[i];
        cout<<"\nEdad: "<<edad[i];
    }
}

return 0;
}
    
asked by Carlos Agustin Guanipa Alvarez 15.04.2017 в 00:38
source

1 answer

0

Your main error is in both loops, they start in position 1 to 2. When declaring the array age [2], if you create 2 spaces, the problem is that you always have to take into account that the arrays begin from position 0.

Ex:

edad[5]--> [0] [1] [2] [3] [4]

In the loop you start from position 1, which will really be 2, so when you print position 2, you will look for information that does not exist.

In summary, this is solved by changing:

for (int i = 0; i < 2; i++)
    
answered by 15.04.2017 / 01:28
source