I had to make a list in which i added the name, job and work experience of some added people, but after i register 3 people, if i click "Afisare" (Show), it only shows the last input. Where's the issue? I thought it had to do with my add(adaugare) function, but im not sure. Hi! I had to make a list in which i added the name, job and work experience of some added people, but after i register 3 people, if i click "Afisare" (Show), it only shows the last input. Where's the issue? I thought it had to do with my add(adaugare) function, but im not sure.(sorry for the repetition, the character limit is high =] )
typedef struct corporatisti
{
char nume[20];
char* functie;
int vechime;
struct corporatisti* urm;
}C;
///numele sa aibe minim 4 vocale
int validare(char*s)
{
int i,k=0,ok=0;
for (i = 0; i < strlen(s); i )
{
if (*(s i) =='a' || *(s i) == 'e' || *(s i) == 'i' || *(s i) == 'o' || *(s i) == 'u')
{
k ;
}
if (k == 4)
{
ok = 1;
break;
}
}
return ok;
}
C* adaugare(C*prim)
{
char aux_nume[20], functie_aux[20];
C*p;
do
{
printf("nume:");
scanf(" %s", aux_nume);
} while (!validare(aux_nume));
printf("functie:");
scanf(" %s", functie_aux);
p = (C*)malloc(sizeof(C));
p->functie = (char*)malloc(sizeof(strlen(functie_aux) 1));
strcpy(p->nume, aux_nume);
strcpy(p->functie, functie_aux);
p->vechime = strlen(p->functie);
p->urm = NULL;
if (prim = NULL)
return p;
else
{ p->urm = prim;
return p;
}
}
void afisare(C*prim)
{
C*p;
for (p = prim; p != NULL; p = p->urm)
{
printf("%s ", p->nume);
printf("%s ", p->functie);
printf("%d", p->vechime);
printf("\n");
}
}
int main()
{
C*prim,*q;
prim = NULL;
int opt;
while (1)
{
printf("Alegeti:\n");//means choose option
printf("0.Iesire\n");
printf("1.Adauga\n");
printf("2.Scrie\n");
printf("Opt este:");
scanf("%d", &opt);
switch (opt)
{
case 0:exit(0);
case 1:prim=adaugare(prim);//means add
break;
case 2:afisare(prim);//means show
break;
default:printf("Opt inexistenta!");
break;
}
}
CodePudding user response:
At least these problems,
Not having all compiler warnings enabled
A good well enabled compiler would whine about if (prim = NULL)
. Save time. Enable all compiler warnings.
Undersized allocation
sizeof(strlen(functie_aux) 1)
is the size of a size_t
, perhaps 4 or 8 bytes. @pm100
Cast not needed.
// p->functie = (char*)malloc(sizeof(strlen(functie_aux) 1));
p->functie = malloc(strlen(functie_aux) 1);
Good practice to check for success.
if (p->functie == NULL) {
// TBD code to handle out of memory.
fprintf(stderr, "Out of memory.\n");
exit (EXIT_FAILURE);
}
strcpy(p->functie, functie_aux);
Incorrect compare
=
assigns. ==
compares. @Vlad from Moscow
// if (prim = NULL)
if (prim == NULL)
Overflow risks
scanf(" %s", aux_nume);
risks attempting to save too much.
Use a width 1 less than the size.
Check return value.
Space not needed.
// scanf(" %s", aux_nume);
if (scanf("s", aux_nume) != 1) {
// TBD code to handle bad input.
fprintf(stderr, "No numeric input.\n");
exit (EXIT_FAILURE);
}
strlen()
not needed
No need to run down the string to find its length and then walk down again and maybe quit the loop early. Instead look for the null character.
// for (i = 0; i < strlen(s); i )
for (i = 0; s[i]; i )
CodePudding user response:
here is your bug
if (prim = NULL)
you mean
if (prim == NULL)
CodePudding user response:
/* .... */
p->urm = NULL;
if (prim = NULL) // wrong...
return p;
else
{ p->urm = prim;
return p;
}
The "assignment instead of equality" problem would not even have arisen if you'd thought a bit more about the logic...
All you need is:
/* ... */
p->urm = prim; // assigns NULL in the first instance.
return p;