Home > Back-end >  My function doesn't work and i don't understand why ..in C
My function doesn't work and i don't understand why ..in C

Time:08-20

I'm working on a problem that keeps the evidence of a shop stock, there is a this part where it is asked to get the code of the product, which I must get by reversing the name of the product.

For example:

name of the product : snack
codebar: kcans

And everytings works just fine until it gets to the line with strcpy.

The menu is not in english but the first one gets the input the second will show the output.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>


typedef struct Product
{
    char productName[30];
    char productCode[30];
    int price;
    int stock;
    int date[3];
}P;

char* reverse(char *name)
{
    char aux[30];
    int i,j;

    for(i = strlen(name) - 1, j = 0; i >= 0; i--,j  )
    {
        aux[j] = name[i];
    }
    aux[j] = '\0';

    return aux[30];
}


int nameValidation(char *name)
{
    int i;
    for(i = 0; i < strlen(name); i  )
    {
        if(name[i] >= 'a' && name[i] <= 'z');
        else
        {
            return 0;
            break;
        }
    }
    if(i == strlen(name))
    return 1;
}

void input(P *product, int *n)
{
    (*n)  ;
    do
    {
        printf("Product name: ");
        scanf("%s", (product   *n)->productName);

    } while (!nameValidation((product   *n)->productName));


    printf("Price: ");
    scanf("%d", &(product   *n)->price);

    printf("Quantity: ");
    scanf("%d", &(product   *n)->sotck);


    do
    {
        printf("Expiration day: ");
        scanf("%d", &(product   *n)->date[0]);

        if((product   *n)->date[0] >= 1 && (product   *n)->date[0] <= 30)
            break;
    } while (1);
   
    
    do
    {
        printf("Expiration month: ");
        scanf("%d", &(product   *n)->date[1]);

        if((product   *n)->date[1] >= 1 && (product   *n)->date[1] <= 12)
            break;
    } while (1);
    

    (product   *n)->date[2] = 2024;
    printf("Expiration Year: %d\n", (product   *n)->date[2]);

    strcpy((product   *n)->productCode, reverse((product   *n)->productName));
    printf("Product Code: %5s\n", (product   *n)->productCode);

}


void output(P *product, int n)
{
    int i;
    for(i = 0; i <= n; i  )
    {
        printf("Product name: s ", (product   i)->productName);

        printf("Price: ] ", (product   i)->price);

        printf("Stock:]\n", (product   i)->stock);
    }

}


int main()
{
    int option;
    int n;
    P product[100];

    n = -1;
    do
    {
        printf("\n0.Iesire\n");
        printf("1.Citirea unui produs de la tastatura\n");
        printf("2.Afisarea produselor citite\n");
        printf("3.Afisarea produselor cu un pret mai mic dat de utilizator\n");
        printf("4.Sa se afiseze toate produsele care au lungimea numelui egala cu un numar par\n");
        printf("5.Afisarea primului produs care este egal cu un nume dat de utilizator\n");
        printf("6.Sa se realizeze produsul cifrelor fiecarui numar de bucati al produselor, apoi sa se copieze fiecare produs intr-un vector si sa se afiseze vectorul.\n");
        printf("7.Copierea tuturor codurilor produselor intr-un vector de sir de caractere, apoi afisare vectorului\n");
        printf("8.Copierea pretului produselor intr-un vector apoi afisarea vectorului in ordine crescatoare\n");
        printf("9.Sa se transforme prima litera din codul produsului in litera mare daca aceasta este vocala.Nu se vor folosii functii predefinite in schimbarea literei\n");
        printf("10.Sa se afiseze forma binara a numarului de bucati ale produselor care au numarul de bucati mai mic decat 100 .\n");
        printf("11.Sa se afiseze toate produsele care urmeaza sa expire in urmatoarele 10 zile.Utilizator va preciza de la tastatura ziua si luna curenta.\n");
        printf("12.Sa se pun pe ultimul rand al unei matrici de caractere de ordin dat de numarul de produse, primul caracter din fieacre nume de produs,pe restul '.'\n");
        printf("13.Stabilirea unui nou pret pt toate produsele daca utilizatorul doreste.Astfel utilizator va stabili procentajul cu care se vor scumpi produsele.\n");
        printf("14.Sa se stearga un produs precizat de utilizator prin numele acestuia\n");
        printf("Introduceti o optiune: ");
        scanf("%d", &optiune);

        switch (option)
        {
        case 1:
            input(product, &n);
            break;
        case 2:
            output(product, n);
            break;
        case 3:
            
            break;
        case 4:
            /* code */
            break;
        case 5:
            /* code */
            break;
        case 6:
            /* code */
            break;
        case 7:
            /* code */
            break;
        case 8:
            /* code */
            break;
        case 9:
            /* code */
            break;
        case 10:
            /* code */
            break;
        case 11:
            /* code */
            break;
        case 12:
            /* code */
            break;
        case 13:
            /* code */
            break;
        case 14:
            /* code */
            break;
        case 0:
            exit(1);
            break;
        
        default:
            printf("Introduceti o optiune valida");
            break;
        }

    } while (option != 0);
    
    return 0;
}

CodePudding user response:

You shouldn't waste time typing in 15 'prompts' and 15 case labels as if adding more code will get things working. I strongly suggest deleting that code (or commenting out those lines) and focus your attention on the tiny utility pieces that you need to build upon.

Here's a 'fixed' version of reverse() to get you going. PLEASE work incrementally, testing every tiny extra line of code added, every tiny change you make. Mountains don't grow from the top down...

char *reverse( char *str ) {
    for( int i = 0, j = strlen( str ) - 1; i <= j; i  , j-- ) {
        char tmp = str[i];
        str[i] = str[j];
        str[j] = tmp;
    }
    return str;
}

int main() {
    char *str[] = { "the fish", "goats are good" };

    printf( "%s", str[0] );
    printf( " == %s\n", reverse( str[0] ) );

    printf( "%s", str[1] );
    printf( " == %s\n", reverse( str[1] ) );

    return 0;
}

Output:

the fish == hsif eht
goats are good == doog era staog

EDIT:

Because of criticism leveled at this answer, I feel dutybound to add this note.

Because this function reverses the string "in place", you will need to adapt the following line of code:

strcpy( (product   *n)->productCode, reverse((product   *n)->productName) );

to

reverse( strcpy( (product   *n)->productCode, (product   *n)->productName) );
  •  Tags:  
  • c
  • Related