0

Hey my problem is that when i try to input the numbers "Geburtstag:JAHR/TAG/MONAT:" i must do an zero berfore the number i want. when i doesnt do it the number comes not correctly out example:

Bitte geben sie den Geburtstag:TAG: 7

Bitte geben sie den Geburtstag:MONAT: ein: 11

Bitte geben sie den Geburtstag:JAHR: ein: 2007

and the output is:

:: hat am 00.01.07 Geburtstag

i think the problem is in the write_data function

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#define FALSE 0
#define TRUE !FALSE
#define max 40
#include <string.h>

struct person
{
    char name[max];
    int day;
    int month;
    int year;
};

void read_data(void);
void write_data(void);
void Ueberschreiben(void);

char *NL(char *string);

int main()
{
    int done = FALSE;
    int c;
    while(!done)
    {
        printf("N - Neuen Geburtstag eintragen\n");
        printf("A - Geburtstage Anzeigen\n");
        printf("E - Beenden\n");
        printf("U - Ueberschreiben\n");

        fflush(stdin);
        c = (char)toupper(getchar());
        switch(c)
        {
            case 'N': printf("Neuer Geburtstag\n\n"); write_data(); break;
            case 'A': printf("Geburtstage Anzeigen\n\n"); read_data(); break;
            case 'E': printf("Beenden\n\n"); done = TRUE; break;
            case 'U': printf("Daten Uebercshreiben\n\n"); Ueberschreiben(); break;
            default: printf("????\n"); break;
        }
    }
}

void write_data(void)
{
    FILE* file;
    int f;
    char input[max];
    struct person Mensch;

    printf("Bitte geben sie den Namen der Person ein: ");
    getchar();
    fgets(input, max, stdin);
    NL(input);
    strcpy(Mensch.name, input);


    printf("Bitte geben sie den Geburtstag:TAG: ein: ");
    getchar();
    Mensch.day = atoi(fgets(input, max, stdin));


    printf("Bitte geben sie den Geburtstag:MONAT: ein: ");
    getchar();
    Mensch.month = atoi(fgets(input, max, stdin));


    printf("Bitte geben sie den Geburtstag:JAHR: ein: ");
    getchar();
    Mensch.year = atoi(fgets(input, max, stdin));
    getchar();


    file = fopen("Geburtsjahr.dat", "a");
    if(file == NULL)
    {
        printf("Dateifehler\n");
        exit(0);
    }
    fwrite(&Mensch, sizeof(struct person), 1, file);

    fclose(file);

    printf("Mensch hinzugefuegt\n");
    getchar();

}

void read_data(void)
{
    FILE* file;
    struct person Mensch;
    int x;


    file = fopen("Geburtsjahr.dat", "r");
    if(file == NULL)
    {
        printf("Dateifehler\n");
        return;
    }

    while(TRUE)
    {
        x = fread(&Mensch, sizeof(struct person), 1, file);

        if(x == 0)
        {
            break;
        }
        printf("%s hat am %.2i.%.2i.%i Geburtstag\n", Mensch.name, Mensch.day, Mensch.month, Mensch.year);
    }
    fclose(file);
}

void Ueberschreiben(void)
{
    char c;
    FILE* file;
    printf("Wollen sie wirklich Die Daten Ueberschreiben(y/n): ");
     getchar();
    c = (char)toupper(getchar());

    if(c == 'Y')
    {
        file = fopen("Geburtsjahr.dat", "w");
        if(file == NULL)
        {
            printf("Dateifehler");
            return;
        }
        fclose(file);
        printf("Alle Daten erfolgreich geloescht\n");
        getchar();
    }
    else
    {
        printf("Okay Abgebrochen\n");
        return;
    }
}

char *NL(char *string)
{
    char* string2 = string;
    while(*string2)
    {
        if(*string2 == '\n')
            *string2 = '\0';
        string2++;
    }
    return string;
}
Andreas Wenzel
  • 12,860
  • 3
  • 14
  • 29
ONees
  • 1
  • 3
  • Did you step through the code in a debugger line-by-line and examine variables? – OldProgrammer Dec 29 '21 at 19:21
  • sorry im new what are examine variables – ONees Dec 29 '21 at 19:23
  • 1
    Why all the calls to `getchar()`? `fgets()` should consume the newlines. – Johnny Mopp Dec 29 '21 at 19:24
  • Your question is very unclear. what does " do an zero berfore the number" mean? – OldProgrammer Dec 29 '21 at 19:25
  • `getchar()` reads *every* character from the input including newlines but in `fflush(stdin); c = (char)toupper(getchar());` for reading a menu choice the `fflush(stdin)` is *undefined behaviour*. And when you do input a menu option, the *subsequent* newline will be read by `fgets()` as a blank line. – Weather Vane Dec 29 '21 at 19:29
  • Don't mix the input methods. If you need to enter one `char` then use `fgets` and look at the string. – Weather Vane Dec 29 '21 at 19:35
  • When i dont write the getchars the output looks so: "Bitte geben sie den Namen der Person ein: Bitte geben sie den Geburtstag:TAG: ein: – ONees Dec 29 '21 at 19:53
  • @ONees: To "examine variables" in a debugger means to look at the variables while running your program in a debugger, in order to see whether they always have the values that you intend. You may want to read this for further information: [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/q/25385173/12149471) – Andreas Wenzel Dec 29 '21 at 19:54

0 Answers0