Variable declared in subprocess is not passed to main

0

I have this program whose behavior has to be like the one described below:

For the development of a computer system of a company, it is necessary to implement a type of data that represents the products used in the factory. It is desired that the product represents the following characteristics: code, description, unit of measure, and price; In turn, if the product is imported it you want to know the origin of the same, while if it is bought in place you want to know the name of the provider's phone number. a) Define the type of data that meets the need. b) Implement an operation that loads the data of the type created in point a c) Implement an operation that displays on screen the data of the type created in point a d) Implement an operation that receives two parameters of the type created in point a and returns true if all its components are equal and false otherwise. e) Implement an operation that receives two parameters of the type created in point a and copy each of the components of the second in the first one (this operation must be permanent).

The error is that the program asks to define the variables p, p1 and p2. The issue is that these variables are declared locally, but if I declare them in the main, it does not accept these variables that I pass as a parameter to that function.

#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <stdbool.h>
#include <winsock.h>

void clrscr();

typedef enum {IMPORTADO, LOCAL} tipo;

typedef struct
{
    int codigo;
    char descripcion [20];
    char unidadMedida [5];
    float precio;
    tipo discriminante;
    union
    {
        char origen [20];
        char destino [20];
        int telefono;
    } impoExpo;
} Producto;

void cargarProducto (Producto *p)
{
    printf("\nIngrese el c¢digo: ");
    scanf("%d",&p->codigo);
    fflush(stdin);
    printf("\nIngrese la descripci¢n: ");
    scanf("%s",p->descripcion);
    printf("Indique la unidad de medida: ");
    scanf("%s",p->unidadMedida);
    printf("Ingrese el precio: ");
    scanf("%f",&p->precio);
    int i;
    printf("\nInqrese 1 si es importado");
    scanf("%d", &i);
    if(i == 1)
    {
        p->discriminante = IMPORTADO;
    }
    else
    {
        p->discriminante = LOCAL;
    }
    if(p->discriminante == IMPORTADO)
    {
        printf("\nIngrese al origen: ");
        gets(p->impoExpo.origen);
    }
    else
    {
        printf("\nIngrese el telefono");
        scanf("%d", &p->impoExpo.telefono);
    }
}

void mostrarProducto (Producto p)
{
    printf("\nC¢digo: %d", p.codigo);
    printf("\nDescripci¢n ");
    printf("%s", p.descripcion);
    printf("\nUnidad de medida: ");
    printf("%s", p.unidadMedida);
    printf("\nPrecio: %.2f", p.precio);
    printf("\nTipo: ");
    if (p.discriminante == IMPORTADO)
    {
        printf("Importado: ");
        printf("\nOrigen: %s",p.impoExpo.origen);
        printf("%s",p.impoExpo.origen);
    }
    else
    {
        printf("Local: ");
        printf("\nTelefono: %d", p.impoExpo.telefono);
    }
}

bool soniguales (Producto p1, Producto p2)
{
    bool iguales = false;
    if ((p1.codigo == p2.codigo) && (p1.descripcion == p2.descripcion))
    {
        if ((p1.unidadMedida == p2.unidadMedida) && (p1.precio == p2. precio))
        {
            if (p1.discriminante == p2.discriminante)
            {
                if (p1.discriminante == IMPORTADO)
                {
                    if (p1.impoExpo.origen==p2.impoExpo.origen)
                    {
                        iguales = true;
                    }
                }
                else
                {
                    if (p1.impoExpo.telefono==p2.impoExpo.telefono)
                    {
                        iguales = true;
                    }
                }
            }
        }
    }
    return iguales;
}

void copiar (Producto * const destino, const Producto * const origen)
{
    destino->codigo = origen->codigo;
    (*destino->descripcion) = (*origen->descripcion);
    (*destino->unidadMedida) = (*origen->unidadMedida);
    destino->precio = origen->precio;
    destino->discriminante = origen->discriminante;

    if(destino->discriminante == IMPORTADO)
        (*destino->impoExpo.origen) = (*origen->impoExpo.origen);
    else
        destino->impoExpo.telefono = origen->impoExpo.telefono;
}

int main()
{
    int opcion;
    do
    {
        clrscr();
        printf("Bienvenido al programa\n");
        printf("Ingrese una opcion\n");
        printf("1. Cargar un producto\n");
        printf("2. Mostrar producto\n");
        printf("3. Verificar si dos productos son iguales\n");
        printf("0. Salir");
        printf("Ingrese la opci¢n, y presione ENTER");
        scanf("%d",&opcion);

        switch(opcion)
        {
        case 1:
            cargarProducto (&p);
            getch();
            break;
        case 2:
            mostrarProducto (p);
            getch();
            break;
        case 3:
            printf("Ingrese el nombre del producto 1 ");
            scanf("%s",p1);
            printf("Ingrese el nombre del producto 2 ");
            scanf("%s",p2);
            printf("%d",soniguales(p1,p2));
            getch();
            break;
        }
    } while (opcion!=0);
    getch();
    return 0;
}

void clrscr()
{
    HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
    COORD coord = {0, 0};
    DWORD count;
    CONSOLE_SCREEN_BUFFER_INFO csbi;
    GetConsoleScreenBufferInfo(hStdOut, &csbi);
    FillConsoleOutputCharacter(hStdOut, ' ', csbi.dwSize.X * csbi.dwSize.Y, coord, &count);
    SetConsoleCursorPosition(hStdOut, coord);
}
    
asked by Alejandro Caro 20.05.2018 в 21:29
source

0 answers