Doubt C input flows

1

The following program works well for me in C ++ with its respective changes (cin and cout), however, when passing it to C, the input provided by the keyboard fails.

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


struct etapa{
    int h, m, s;
}etapas[3], *puntero_etapa = etapas;


void giveData(){

  for (int i = 0; i < 3; i++) {
    printf("Introduzca el numero de horas: \n" );
    scanf("%i\n", &puntero_etapa[i].h);
    //cin>>puntero_etapa[i].h;          
    printf("Introduzca el numero de minutos: \n" );
    scanf("%i\n", &((puntero_etapa + i)-> m));
    //cin>>(puntero_etapa + i)-> m;
    printf("Introduzca el numero de segundos: \n" );
    scanf("%i\n", (&(puntero_etapa + i)-> s));
    //cin>>(puntero_etapa + i)-> s;
  }
}

void timeEtapas(const struct etapa *puntero_etapa1){
  int time = 0, time1 = 0, time2 = 0;
  for (int i = 0; i < 3; i++) {
    time += (puntero_etapa1 + i)-> h;
    time1 += (puntero_etapa1 + i)-> m;
    time2 += (puntero_etapa1 + i)-> s;
    printf("La duracion total es: Horas: %i minutos: %i segundos:  
            %i\n",time, time1, time2 );
  }
}


int main(int argc, char const *argv[]) {
  giveData();
  timeEtapas(puntero_etapa);
  return 0;
}

Causing an exit of this type:

Introduzca el numero de horas: 
888
0
Introduzca el numero de minutos: 
8
Introduzca el numero de segundos: 
8
Introduzca el numero de horas: 
8
Introduzca el numero de minutos: 
8
Introduzca el numero de segundos: 
8
Introduzca el numero de horas: 
8
Introduzca el numero de minutos: 
8
Introduzca el numero de segundos: 
8

As you can see, it only fails in the first iteration, which asks me twice and therefore it is hauled down. How to solve it?

    
asked by Diego 13.09.2017 в 18:27
source

1 answer

1

The line break in the reading is over:

scanf("%i\n", &puntero_etapa[i].h);
//       ^^

You should leave it like this:

scanf("%i", &puntero_etapa[i].h);

And the same for the rest of the readings

    
answered by 13.09.2017 / 18:34
source