1

First of all, I want to explain my vocabulary of functions:

  • Universo: Universe
  • Edad: Age
  • Nombre: Name
  • Alias: Nickname
  • Nacionalidad: Country
  • Persona: Person
  • Insertar persona: Add Person to the Struct array
  • Mostrar persona: Print all the struct array elements

So I want to create a struct array and an options table in where I can choose to add persons or print them. The program max person capacity is 1000 and it checks if the person is already in the array or not, adding ir if so.

So I don't understand in first place why it doesnt print all the persons and if the persons actually are stored. Can you help me? It is a class work and I can't complicate it more that, so pointers aren't allowed. This is my code:

#include <stdio.h>
#include <stdlib.h>
#define MAX_CAD 100
#include <string.h>
#include <ctype.h>
struct persona{
    char nombre[MAX_CAD];
    int edad;
    char nacionalidad[MAX_CAD];
    char alias[MAX_CAD];
};
int insertarPersona(struct persona universo[], int capacidad_max, struct persona nueva);
void mostrarMundo(struct persona universo[], int capacidad_max);
int main()
{

    int seleccion, capacidadmax=1000, i=0;
    struct persona universo[1000];
    struct persona nueva;


            strcpy(universo[i].nombre, "-");
            strcpy(universo[i].nacionalidad, "-");
            strcpy(universo[i].alias, "-");
            universo[i].edad = -1;


        do{
            printf("1-Crear persona                     \^\// \n");
            printf("2-Mostrar universo                  |. .| \n");
            printf("3-Thanos-Chascar                  _ \ - / _ \n");
            printf("4-Restaurar universo               \_| |_/ \n");
            printf("5-Salir                              \ \  \n");
            printf("                                   __/_/__\n");
            printf("                                  |       |\n");
            printf("                                   \     /\n");
            printf("                                    \___/\n");
            scanf("%d", &seleccion);

            if(seleccion==1&&i<1000){


                    printf("Introduzca el nombre de la persona a añadir\n");
                    while(getchar()!='\n');
                    gets(nueva.nombre);
                    printf("Introduzca el alias de la persona a añadir\n");
                    gets(nueva.alias);
                    printf("Introduzca la nacionalidad de la persona a añadir\n");
                    gets(nueva.nacionalidad);
                    printf("Introduzca la edad de la persona a añadir\n");
                    scanf("%d", &nueva.edad);
                    if(insertarPersona(universo, capacidadmax, nueva)==1){
                        strcpy(universo[i].nombre, nueva.nombre);
                        strcpy(universo[i].alias, nueva.alias);
                        strcpy(universo[i].nacionalidad, nueva.nacionalidad);
                        universo[i].edad=nueva.edad;

                        printf("Persona añadida!\n");
                      i++;
                    }


                    else{
                        printf("El universo esta lleno o dicha persona ya esta dentro\n");
                    }




            }else if(seleccion==2){
                printf("pers.  Nombre\t\t\t\t\t Alias\t\t\t\t Nacionalidad\t\t\t Edad\n");
                printf("=====================================================================================================================\n");

                mostrarMundo(universo, capacidadmax);





            }
            printf("%d",i);
        }while (seleccion !=5);


    return 0;
}
int insertarPersona(struct persona universo[], int capacidad_max, struct persona nueva){
    capacidad_max=1000;
    int espersona=0;
    for(int i=0; i=='\0'&& i<capacidad_max;i++){
        if ((strcmp(nueva.nombre, universo[i].nombre)&& (nueva.edad!=universo[i].edad)&& strcmp(nueva.nacionalidad, universo[i].nacionalidad)&& strcmp(nueva.alias, universo[i].alias)) !=0 && i<1000){
            espersona=1;
        }else {
            espersona=0;
        }

    }
    return espersona;
}
void mostrarMundo(struct persona universo[], int capacidad_max){
    capacidad_max=1000;
    for(int i=0; i=='\0'&&i<capacidad_max; i++){
        printf("%d\t%-35s%-25s\t%-20s%10d\n", i+1, universo[i].nombre, universo[i].alias, universo[i].nacionalidad, universo[i].edad);

        if(universo==0){
            printf("Universo no habitado\n");
        }
    }

}
VillageTech
  • 1,968
  • 8
  • 18
Wazabi
  • 83
  • 5
  • What is the input, expected output and actual output? Also, have you run the program in a debugger to help you trace through the program execution? – kaylum Dec 07 '19 at 19:57
  • `int i=0; i=='\0'` what is that intended to check for? – kaylum Dec 07 '19 at 20:00
  • It is supposed to check until the array element is null. For example if i have added 3 people, the i is 2. The expected input is a person data, and the expected output is depending on the number of people you have added, that people with each one of its variables – Wazabi Dec 07 '19 at 20:08
  • That's why it's important to learn to debug effectively on your own. Run that in a debugger and you'll see how many times your loops run for. `i` is not an element of the array. It is an array index. And the array elements are `struct persona` anyway so it makes no sense to compare against a char. I really don't know how you think that will work. – kaylum Dec 07 '19 at 20:16
  • One way is to use a flag inside the struct to mark whether the entry is used or not. For example `universo[i].edad == -1` could be used to indicate a free entry. But you need to initialise all the `edad` values to `-1` at the beginning and whenever a student is removed (at the moment you only initialise the first entry). – kaylum Dec 07 '19 at 20:19
  • See [Why gets() is so dangerous it should never be used!](https://stackoverflow.com/questions/1694036/why-is-the-gets-function-dangerous-why-should-it-not-be-used) Use `fgets()` instead. – David C. Rankin Dec 08 '19 at 08:25
  • So the problem is the for or the gets? And the functions are good or is there any error? – Wazabi Dec 08 '19 at 23:07

0 Answers0