I've a big problem. I created a variable array with struct. For that I've used malloc
and realloc
.
But if I'm using scanf
,the console isn't waiting for a second input and is moving forward. How can I solve this problem.
#include <string.h>
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
typedef struct {
float feuchtewert;
float temperaturwert;
char zeitstempel[8];
} T_DHT22;
int main()
{
int length = 1;
T_DHT22 *messreihe = malloc(length * sizeof(*messreihe));
bool weiterErstellen = true;
while(weiterErstellen) {
char c = 'n';
printf("Weiteren Messwert erstellen? <y|n>");
scanf("%c", &c);
if(c == 'y') {
length++;
messreihe = realloc(messreihe, length * sizeof(*messreihe));
printf("\nFeuchtewert: ");
scanf("%.2f", messreihe[length].feuchtewert);
printf("\nTemperaturwert: ");
scanf("%.2f", messreihe[length].temperaturwert);
//gets(messreihe[length].zeitstempel);
}else {
weiterErstellen = false;
}
}
free(messreihe);
//sort_and_calcAvg(messreihe, length);
}