Complicator does not give error, but the program does not start

Asked

Viewed 45 times

0

I’m learning data structure with the C language, but I realized that I should study a little more struct and pointers, in one of the exercises that I was given is to create struct with day, month and year, and then make a function that you receive a number of days and return on that day, month and year it would be example 01/01/2021, 15 days, it should return 15/01/2021.

After fixing some errors that appeared at the time of compiling in the terminal I was able to do its executable, but the console opens and it gets dark, nothing appears to type, below is my code and the error.


#include <stdio.h>
#include <stdlib.h>
#define TRUE 1
#define FALSE 0

typedef struct dma{
    int dia;
    int mes;
    int ano;
};

typedef struct dma data;

int mesGrande[7] = {1, 3, 5, 7, 8, 10, 11};

int verificadorAnoBissexto(int ano){
    if(ano % 4 != 0){
        if(ano % 400 == 0){
            return TRUE;
        }else{
            return FALSE;
        }
    }else{
        if(ano % 100 == 0){
            return FALSE;
        }else{
            return TRUE;
        }
    }
}

int verificadorDeMesGrande(int ano){
    int verificador;
    for(verificador = 0 ;verificador < 7 ;verificador++){
        if(ano == mesGrande[verificador]) return TRUE;
    }
    return FALSE;
}

void acrecentadorDeDias(data acres){
    if(verificadorAnoBissexto(acres.ano)){
        if(acres.dia == 29 && acres.mes == 2){
            acres.dia = 1;
            acres.mes = 3;
        }
    }
    if(verificadorDeMesGrande(acres.ano) == TRUE && acres.dia == 31){
        if(acres.ano == 12){
            acres.dia = 1;
            acres.mes = 1;
            acres.ano++;
        }
        acres.dia = 1;
        acres.mes++;
    }
    if(verificadorDeMesGrande(acres.ano) != TRUE && acres.dia == 30){
        acres.dia = 1;
        acres.mes++;
    }
}

data fimEvento(data inicio, int duracao){
    int contador;
    for(contador = 0 ;contador <= duracao ;contador++){
        acrecentadorDeDias(inicio);
    }
    return inicio;
}

int main(){
   data a, b;
   scanf ("%d %d %d", &a.dia, &a.mes, &a.ano);
   int dura;
   scanf ("%d", &dura);
   b = fimEvento (a, dura);
   printf ("%d %d %d\n", b.dia, b.mes, b.ano);
   system("pause");
   return 0;
}

Console não apresenta os printf

If possible they could also explain what happened so that I do not make the same mistake in the future, I will be very grateful.

Sorry if I missed something on the topic is my first.

Solved The problem was that I did not put a printf before the scanf so the "Error" happened, it was already waiting for the data. Corrections For the next users to take this code as a reference, there is an error in it ano == mesGrande[verificador] change the code this year by month and also where it is started.

  • Hello... It seems that you have not instructed the program to write anything before reading the data. Note that you have a 'scanf', and only then the 'printf'. To get the desired result, put a writing statement before the first scanf, something like: 'printf("Enter a date");'.

  • Here: if(ano == mesGrande[verificador]) I don’t think it makes much sense to compare it to year. Everything indicates that the program is waiting for you to inform the date and then the duration.

  • Thank you Marcos, the mistake was really that, same head mistake, I forgot to put a printf before, and it would already go straight to Scanf. Anonimo, thanks too, I just put it in the code.

  • In reality this is not a "mistake", simply the program was waiting for entry. The display of a message helps to signal that it is waiting but in a large number of cases this type of message is totally unnecessary. Anyone using the program should be aware of the required entries.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.