'Text is placed directly behind a text
I want to write a program that collects data and stores it. But when I try to use the function that I wrote, the text is displayed directly behind the text and I can't make an input on the first question.
void new_entry(void)
{
struct mensch
{
char name[30];
char adress[30];
int telefon;
char geschlecht[30];
char email[30];
} data;
FILE* file;
char temp[30];
printf("Please enter the name: ");
fgets(data.name, sizeof data.name, stdin);
printf("Please enter the adress: ");
fgets(data.adress, sizeof data.adress, stdin);
printf("Please enter the Telefon-number: ");
fgets(temp, sizeof temp, stdin);
data.telefon = atoi(temp);
printf("Please enter the gender: ");
fgets(data.geschlecht, sizeof data.geschlecht, stdin);
printf("Please enter the email: ");
fgets(data.email, sizeof data.email, stdin);
file = fopen(FILE_PATH, "a+");
fwrite(&data, 1, sizeof data, file);
fclose(file);
}
The output is:
Please enter the name: Please enter the adress:
That's why I can't input anything on the first question.
Solution 1:[1]
The Problem was, that a newline character was in the input buffer i cleared it with that:
while(getc(stdin) != '\n');
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | zlSxrtig |
