Click here to Skip to main content
15,885,435 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
Good afternoon,

I'm here with a doubt that i am not able to overcome and i've tried a number of ways. I'm doing a program that among other things the user has to enter data about a product where the serial number has to be unique and of type char. What is happening is that when i type the 1st product serial number the program returns that is already a product with this serial number despite being the 1st.

Here's the code I have

C++
#include <stdio.h>
#include <stdlib.h>

typedef struct
{
    char numeroserie;
    char descricao;
    float preco;
} stProduto;
/********************** Funcoes dos produtos *************************/
void inserirProduto(stProduto produtos[], int *contadorProdutos);
int procurarNumeroserie(stProduto produtos[], int contadorProdutos, char produtoAProcurar);

int main()
{
    stProduto produtos[500];
    int contadorProdutos=0;
    inserirProduto(produtos,&contadorProdutos);
    return 0;
}
void inserirProduto(stProduto produtos[], int *contadorProdutos)
{
    char string1;
    char posicao;
    do
    {
        printf("Introduza o numero de serie do produto:");
        scanf("%s",&string1);
        posicao =  procurarNumeroserie(produtos,*contadorProdutos, string1);
        if (posicao == 0)
        {
            printf("Ja existe um produto com esse numero de serie!!!\n");
        }
    }
    while(posicao == 0);
    strcpy(produtos[*contadorProdutos].numeroserie,string1);
    (*contadorProdutos)++;
}
int procurarNumeroserie(stProduto produtos[], int contadorProdutos, char produtoAProcurar)
{
   int i;
    char posicao;
    posicao = 0;
    for(i = 0 ; i < contadorProdutos ; i++)
    {
        if (strcmp(produtos[i].numeroserie, produtoAProcurar) == 0)
        {
            posicao=i;
            i=contadorProdutos;
        }
    }
    return posicao;
}


Please help me!!!Here's a error in the code but i can't find it.
Posted
Comments
[no name] 3-Jul-15 23:30pm    
There are so many problems here I don't know where to start. This code is too raw for analysis. Please observe compiler warnings and address them. Start here: A char is one character - you need arrays of chars (there are other ways but this is easy). For example:

typedef struct
{
char numeroserie[20]];
char descricao[20];
float preco;
} stProduto;

You don't have a vector of structures but an array.
Learn how a do while works etc etc.
Frankie-C 4-Jul-15 15:35pm    
Is this an homework?

1 solution

I will answer the specific question that you asked (leaving all other errors for you to find).

When calling procurarNumeroserie() for the first time, contadorProdutos == 0. This means that the loop is not executed, leaving posicao as zero. The caller sees this as an indication that the product already exists.
 
Share this answer
 
Comments
[no name] 6-Jul-15 5:15am    
Which is implicit in my comment.
Daniel Pfeffer 6-Jul-15 5:23am    
Sorry; I missed the last line of your comment. In my defence, I did give the OP precisely the help that he asked for, no more and no less.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900