查找字符串向量中是否存在字符串

编程入门 行业动态 更新时间:2024-10-15 20:16:56
本文介绍了查找字符串向量中是否存在字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

下午好, 我怀疑我无法克服,而且我尝试了很多方法。我正在做一个程序,除其他外,用户必须输入有关序列号必须唯一且类型为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.

更多推荐

查找字符串向量中是否存在字符串

本文发布于:2023-11-30 14:01:04,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1650253.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字符串   向量   是否存在

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!