本文介绍了查找字符串向量中是否存在字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
下午好, 我怀疑我无法克服,而且我尝试了很多方法。我正在做一个程序,除其他外,用户必须输入有关序列号必须唯一且类型为char的产品的数据。发生的事情是,当我输入第一个产品序列号时,程序返回已经是具有此序列号的产品,尽管它是第一个。 这是代码I有
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
#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.
推荐答案我会回答您提出的具体问题(留下所有其他错误供您查找)。 第一次调用 procurarNumeroserie()时, contadorProdutos == 0.这意味着循环未执行, posicao 为零。调用者将此视为产品已存在的指示。 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.
更多推荐
查找字符串向量中是否存在字符串
发布评论