[英]C structures and functions
I'm new to C programming and I am trying to create a function that adds values to a structure but it does not return anything.我是 C 编程的新手,我正在尝试创建一个 function 向结构添加值但它不返回任何内容。 This function is meant to allow the user to add records.
此 function 旨在允许用户添加记录。
#include <stdio.h>
#include <string.h>
struct Produit {
int Num;
char Nom[50];
char Description[100];
float Prix;
} s[10];
void add(struct Produit s[],int n);
void display(struct Produit s[],int p,int n)
int main {
add(s,1);
display(s,1);
++n;
}
void add(struct Produit s[], int n){
again:
printf("\nEntrez le nom du produit à ajouter:");
scanf("%s",s[n].Nom);
if(searchP(s,s[n].Nom,n)!=-1){
printf("Déjà existant\n");goto again;
}
printf("Entrez la description :");
scanf("%s",&s[n].Description);
printf("Entrez le prix :");
scanf("%f",&s[n].Prix);
}
void display(struct Produit s[],int p,int n) {
printf("Nom du produit: ");
puts(s[p-1].Nom);
printf("Description: ");
puts(s[p-1].Description);
printf("Prix: %.1f", s[p-1].Prix);
printf("\n");
}
When I run this it works fine but when I verify if the record that I've entered is is there I don't find anything.当我运行它时它工作正常,但是当我验证我输入的记录是否存在时,我什么也没找到。 I try to display the record but it's empty.
我尝试显示记录,但它是空的。 it returns this:
它返回这个:
Entrez le nom du produit α ajouter:pen
Entrez la description :red
Entrez le prix :1.99
Nom du produit:
Description:
Prix: 0.0
can anyone tell what is wrong.谁能告诉我出了什么问题。 thanks PS: The function SearchP is working fine in other parts of the code so I don't think it is the problem, but nonetheless here is it.
谢谢 PS:function SearchP 在代码的其他部分工作正常,所以我认为这不是问题,但仍然存在。
int searchP(struct Produit s[],char Name[], int n) {
int found =-1,i;
for (i = 0; i < n-1 && found==-1; i++)
{
if (strcmp(s[i].Nom,Name)==0) {
found=i;
}
else
found=-1;
}
return found;
}
Here is a modified version of your code that works better but still needs some work.这是您的代码的修改版本,效果更好,但仍需要一些工作。
As already commented you need a global counter for the s
array (here: nbp
).如前所述,您需要
s
数组的全局计数器(此处为: nbp
)。 There also one-off errors when searching into s
.搜索
s
时也会出现一次性错误。
#include <stdio.h>
#include <string.h>
struct Produit {
int Num;
char Nom[50];
char Description[100];
float Prix;
} s[10];
int nbp = 0;
void display(struct Produit s[],int p) {
printf("Nom du produit: ");
puts(s[p].Nom);
printf("Description: ");
puts(s[p].Description);
printf("Prix: %.1f", s[p].Prix);
printf("\n");
}
void add(struct Produit s[]);
int searchP(struct Produit s[],char Name[]) {
int found =-1,i;
for (i = 0; i < nbp && found==-1; i++)
{
if (strcmp(s[i].Nom,Name)==0) {
found=i;
}
else
found=-1;
}
return found;
}
int main () {
add(s);
display(s, 0);
return 0;
}
void add(struct Produit s[]){
again:
printf("\nEntrez le nom du produit à ajouter:");
scanf("%s",s[nbp].Nom);
if(searchP(s,s[nbp].Nom)!=-1){
printf("Déjà existant\n");goto again;
}
printf("Entrez la description :");
scanf("%s",s[nbp].Description);
printf("Entrez le prix :");
scanf("%f",&s[nbp].Prix);
nbp++;
}
Execution:执行:
./myprog
Entrez le nom du produit à ajouter:Nom
Entrez la description :Desc
Entrez le prix :1
Nom du produit: Nom
Description: Desc
Prix: 1.0
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.