2

i am having problems in this code, it intends to read an array of structs, with size given by the user, structs that consist of 2 strings, 1 enum and 2 ints. after that read other struct, that consist of 2 ints and one enum, and compare the ints and the enums of this struct with each one of the array. if its equal it prints the two strings. in case is not, it prints "nenhum evento encontrado!"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

enum mes {janeiro=1,fevereiro,marco,abril,maio,junho,julho,agosto,setembro,outubro,novembro,dezembro};

struct data{
    int dias;
    enum mes meis;
    int ano;
};
struct evento{
    char nome [100];
    char local [100];
    int dia;
    enum mes mos;
    int anos;
};
void cadastrar_eventos(struct evento agenda[], int n){
    int i;
    struct evento *p1;
    p1=agenda;
    for (i=0;i<n;i++){
        fgets (p1->nome,100,stdin);
        fgets (p1->local,100,stdin);
        scanf ("%d",&(p1->dia));
        scanf ("%u",&(p1->mos));
        scanf ("%d",&(p1->anos));
        p1++;
    }
    
}
void imprimir_eventos(struct evento agenda[], struct data d, int n){
    int i;
    int count;
    count=0;
    struct evento *p1;
    p1=agenda;
    for (i=0;i<n;i++){
      p1=&agenda[i];
        if (d.dias==agenda[i].dia &&
         d.meis==agenda[i].mos && d.ano==agenda[i].anos){
            printf ("%s %s\n",p1->nome,p1->local);
            count++;
        }
    }
    if (count==0){
        printf("Nenhum evento encontrado!");
    }
    
}

int main (){
    int n;
    struct data hor, *p3;
    p3=&hor;
    scanf ("%d",&n);
    struct evento agenda[n];
    cadastrar_eventos(agenda,n);
    scanf ("%d",&hor.dias);
     scanf ("%u",&hor.meis);
   scanf ("%d",&hor.ano);
    imprimir_eventos (agenda,hor,n);
    return 0;
}

this was the code, but after i tried to use it, when i put the values it will not work. i put the first number (size of the array) and the two first strings and it instantly printf something ramdom. for example if i put '1' 'name' and 'event', it gives

nome [Inferior 1 (process 670) exited normally]

  • 2
    You should read [scanf() leaves the newline character in the buffer](https://stackoverflow.com/questions/5240789/scanf-leaves-the-newline-character-in-the-buffer) since you're mixing `fgets` and `scanf`. You might also consider a [mcve] and showing the input you're using along with the expected result. – Retired Ninja May 24 '23 at 03:44

1 Answers1

2

You have not shown the function (may be the main()) which takes size of array input from user. Looking at your problem description, it seems, you must be using scanf() to take size of array input from user, something like this:

    scanf("%d", &n);    // n is size of array

When you are giving size of array input, you must be pressing ENTER key which is resulting in a newline character leftover in the input stream buffer.

After this, your code must be calling cadastrar_eventos() function and this fgets() statement

    fgets (p1->nome,100,stdin);

ends up reading the leftover newline character from input stream buffer.

The first string that you enter - name, read by this fgets() statement -

    fgets (p1->local,100,stdin);

Hence, when you print local and nome member of struct evento type variable, which holds the input from user, it gives output newline character read into the nome member followed by the first input string which is read into local.

To fix this problem, discard the leftover newline character from input buffer after taking array size input. You can do:

    scanf("%d", &n);   // n is size of array
    int c;
    while((c = getchar()) != '\n' && c != EOF)
        /* discard the character */;

Also, fgets() consider newline character as a valid character and include it in the string copied to buffer passed. Make sure to remove trailing newline character from name buffer. After fgets(), may you can do

    nome[strcspn(nome, "\n")] = 0;
H.S.
  • 11,654
  • 2
  • 15
  • 32