Доступ к полю структуры, входящей в его объединение - PullRequest
0 голосов
/ 30 апреля 2018

Эта программа должна обрабатывать данные, которые представляют продукты, используемые на заводе. Желательно, чтобы продукт представлял следующие характеристики: код, описание, единицу измерения и цену; В свою очередь, если продукт импортирован, вы хотите знать происхождение продукта, в то время как если он приобретен в квадрате, вы хотите знать имя телефона поставщика.

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

void clrscr();

typedef enum {IMPORTED, LOCAL} type;

typedef struct
{
    int code;
    char description [20];
    char MeasureUnit [5];
    float price;
    type discriminant;
    union
    {
        char origin [20];
        char destination [20];
        int telephone;
    } impoExpo;
} Product;

//this procedure fails
void loadProduct (Product *p)
{
    printf("\nEnter the code:");
    scanf("%d",&Product.code); //<-error: expected expression before 'Product'
    fflush(stdin);
    printf("\nEnter the description:");
    scanf("%s",Product.description);
    printf("Indicate the unit of measure:");
    scanf("%s",Product.MeasureUnit);
    printf("Enter the price:");
    scanf("%f",&Product.price);
    int i;
    printf("\nInsert 1 if imported");
    scanf("%d", &i);
    if(i == 1)
    {
        p->discriminant = IMPORTED;
    }
    else
    {
        p->discriminant = LOCAL;
    }
    if(p->discriminant == IMPORTED)
    {
        printf("\nEnter source: ");
        gets(p->impoExpo.origin);
    }
    else
    {
        printf("\nEnter the phone");
        scanf("%d", &p->impoExpo.telephone);
    }
}

//it is also
void showProduct (Product p)
{
    printf("\nCode: %d", p.code); //<----- error: request for member 'code' in something not a structure or union
    printf("\nDescription");
    printf("%s", p.description);
    printf("\nMeasurement unit:");
    printf("%s", p.MeasureUnit);
    printf("\nPrice:% .2f", p.price);
    printf("\nType:");
    if (p.discriminant == IMPORTED)
    {
        printf("Imported:");
        printf("\nOrigin: %s", p.impoExpo.origin);
        printf("%s", p.impoExpo.origin);
    }
    else
    {
        printf("Local:");
        printf("\nTelephone: %d", p.impoExpo.telephone);
    }
}

//this one also
bool areequal (Product p1, Product p2)
{
    bool equal = false;
    if ((p1.code == p2.code) && (p1.description == p2.description))
    {
        if ((p1.MeasureUnit == p2.MeasureUnit) && (p1.price == p2.price))
        {
            if (p1.discriminant == p2.discriminant)
            {
                if (p1.discriminant == IMPORTED)
                {
                    if (p1.impoExpo.origin == p2.impoExpo.origin)
                    {
                        equal = true;
                    }
                }
                else
                {
                    if (p1.impoExpo.telephone == p2.impoExpo.telephone)
                    {
                        equal = true;
                    }
                }
            }
        }
    }
    return equal;
}

//this función ok
void copy (Product * const destination, const Product * const origin)
{
    destination->code = origin->code;
    (*destination->description) = (*origin->description);
    (*destination->MeasureUnit) = (*origin->MeasureUnit);
    destination->price = origin->price;
    destination->discriminant = origin->discriminant;

    if(destination->discriminant == IMPORTED)
        (*destination->impoExpo.origin) = (*origin->impoExpo.origin);
    else
        destination->impoExpo.telephone = origin->impoExpo.telephone;
}

//and the latter also
int main ()
{
    int option;
    do
    {
        clrscr();
        printf("Welcome to the program\n");
        printf("Enter an option\n");
        printf("1. Load a product\n");
        printf("2. Show product\n");
        printf("3. Check if two products are the same\n");
        printf("0. Exit");
        printf("Enter the option, and press ENTER");
        scanf("%d",&option);

        switch (option)
        {
        case 1:
            loadProduct(&p);
            getch();
            break;
        case 2:
            showProduct(p);
            getch();
            break;
        case 3:
            printf("Enter the name of the product 1");
            scanf("%d",&p1);
            printf("Enter the name of the product 2");
            scanf("%d",&p2);
            printf("% d",areequal (p1, p2));
            getch();
            break;
        }
    } while (option != 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);
}

1 Ответ

0 голосов
/ 30 апреля 2018

Product - указатель, поэтому вы должны получить доступ к его элементам, используя обозначение стрелки:

    scanf("%d",&p->code); //<-error: expected expression before 'Product'
...