-2

The code is german but it does not matter in this case:

I have the famous scanf() new line problem (although it is famous, i could not find any solution). I saw this code in a YouTube video, and in the video the code worked. But on my PC the code does not work because my scanf() function does not recognize that i want to enter my string after I press "ENTER". The line i am talking about is commented like "THE PROBLEM IS HERE"

//
//  main.c
//  list
//
//  Created by Programmieren on 28.04.18.
//  Copyright © 2018 Semih. All rights reserved.
//

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

struct todo{
    char aufgabe[100];
    int tag;
    int monat;
    int jahr;
    struct todo *next;
};

void leerePuffer()
{
    char ch;
    while ((ch == getchar()) != '\n' && ch != EOF);
}


//Prototyp

void ausgabe(struct todo *anfang);
struct todo *neues_element(struct todo *anfang);

int main(int argc, const char * argv[]) {
    int wahl;

    struct todo *anfang = NULL;

    do
    {
        printf("Menu\n");
        printf("1 - Ausgeben der ToDo's\n");
        printf("2 - Eingeben eines neuen Elements\n");
        printf("0 - Beenden\n");
        printf("Bitte geben Sie ihre Wahl ein: ");

        scanf("%d", &wahl);

        switch (wahl)
        {
            case 1:
                ausgabe(anfang);
                break;
            case 2:
                anfang = neues_element(anfang);
                break;
            case 0:
                break;
            default:
                printf("Keine gültige Auswahl\n");
                break;
        }
    }
    while (wahl != 0);


    return 0;
}

void ausgabe(struct todo *anfang)
{
    struct todo *aktuell = anfang;
    int counter = 0;

    while (aktuell != NULL)
    {
        counter++;
        printf("%02d: %50s %02d.%02d.%04d\n", counter, aktuell->aufgabe, aktuell->tag, aktuell->monat, aktuell->jahr);

        aktuell = aktuell->next;

    }
}

struct todo *neues_element(struct todo *anfang)
{
    struct todo *neu = NULL;
    neu = (struct todo *)malloc(sizeof(struct todo));
    if (neu == NULL) {
        printf("Kein Speicher mehr da!\n");
        return anfang;
    }
    else{
        printf("Geben Sie die Daten ein:\n");

        printf("Aufgabentext: \n");
        leerePuffer();
        //THE PROBLEM IS HERE: !!!!!!!!!!!!!!!!!!!!##############
        scanf("%99[^\n]", neu->aufgabe);

        printf("Deadline Tag: ");
        scanf("%d", &(neu->tag));
        printf("Deadline Monat: ");
        scanf("%d", &(neu->monat));
        printf("Deadline Jahr: ");
        scanf("%d", &(neu->jahr));

        neu->next = anfang;

        return neu;

    }
}
Sedem
  • 47
  • 1
  • 9
  • 1
    One important note: The [`getchar`](http://en.cppreference.com/w/c/io/getchar) function returns an *`int`*. This is important for the `EOF` check. – Some programmer dude Apr 28 '18 at 13:01
  • 1
    famous `scanf` misuse? Check return from function – Jacek Cz Apr 28 '18 at 13:01
  • 1
    Have a look at these likely candidates: https://stackoverflow.com/questions/35178520/how-to-read-parse-input-in-c-the-faq http://sekrit.de/webdocs/c/beginners-guide-away-from-scanf.html The latter does NOT only tell you not to use it, it also explains a lot. – Yunnosch Apr 28 '18 at 13:01
  • As for your `scanf` issue. If you know it's an issue then you should know that you could add a leading space in the `"%99[^\n]"` format and `scanf` will handle it by skipping any leading space (including newline). – Some programmer dude Apr 28 '18 at 13:03
  • @JacekCz check return from function? how? – Sedem Apr 28 '18 at 13:03
  • Can you show how exactly you are using the code? Sample input, output, desired output, ... That should be part of a good [mcve]. – Yunnosch Apr 28 '18 at 13:03
  • @Someprogrammerdude i made a leading space... didnt work – Sedem Apr 28 '18 at 13:04
  • 3
    Turns out you don't have the famous `scanf` problem. You have the famous `=` is not the same as `==` problem. – Steve Summit Apr 28 '18 at 13:43

2 Answers2

1

In function leerePuffer(), change ch == getchar() to ch = getchar().

Steve Summit
  • 45,437
  • 7
  • 70
  • 103
0

There's a typo in the line

while ((ch == getchar()) != '\n' && ch != EOF);

It should've been

while ((ch = getchar()) != '\n' && ch != EOF);

ch == getchar() would gives either 0 or 1 since it is a comparison.

And as noted in the comments, getchar() returns an int. Not a char. In case getchar() returns EOF, char won't do.

Have a look here.

Also, there's no need to explicitly cast the return value of malloc() in C. See this post.

J...S
  • 5,079
  • 1
  • 20
  • 35