-2
#include <stdio.h>

void aeins(){

    int x;
    unsigned int y;
    double z;

    printf("Geben sie einen ganze Zahl ein: ");
    scanf("%d", &x);
    printf("Geben sie eine natürliche Zahl ein: ");
    scanf("%u", &y);
    printf("Geben sie eine reelle Zahl ein: ");
    scanf("%lf", &z);

    printf("Die dritte Potenz von %d ist %d", x, x*x*x);
    printf("Die dritte Potenz von %u ist %u", y, y*y*y);
    printf("Die dritte Potenz von %lf ist %lf", z, z*z*z);

}

void azwei(){

    printf("Geben sie einen Character ein: ");
    char c = getchar();
    printf("Das nachfolgende Zeichen lautet: %c und der ASCII-Wert ist: ", c+1, c+1);

}

int main (void){

    int a;
    int b = 1;

    while(b){
        printf("Welche Aufgabe soll gezeigt werden? ");
        printf("\n(1) Aufgabe 1 \n(2) Aufgabe 2\n");
        scanf("%d", &a);

        switch(a){
            case 1: aeins();
                    b = 0; break;
            case 2: azwei();
                    b = 0; break;
            default: printf("Falsche Eingabe!\n"); break;
        }
    }
}

This is my Program and this is my output:

Welche Aufgabe soll gezeigt werden?
(1) Aufgabe 1
(2) Aufgabe 2
2
Geben sie einen Character ein: Das nachfolgende Zeichen lautet:  und der ASCII-Wert ist:
Process returned 0 (0x0)   execution time : 2.172 s
Press any key to continue.

As you can see my program is ignoring the getchar command. I have tried it with a scanf command, but it won't work too. In the function aeins works everything. I would say I am a beginner to intermediate c programmer if this helps you.

halfer
  • 19,824
  • 17
  • 99
  • 186
  • 1
    Fantastic job authoring this question. It's so refreshing to see all this supporting data from a new contributor! Cheers. Small thing: you have an extra `c+1` or a missing substitution parameter in the line ` printf("Das nachfolgende Zeichen lautet: %c und der ASCII-Wert ist: ", c+1, c+1);` – erik258 Nov 03 '19 at 17:18
  • 2
    Note that `char c = getchar();` will read the newline left in the input buffer after `scanf("%d", &a);` This is a Frequently Asked Question. – Weather Vane Nov 03 '19 at 17:24
  • The returned type from function: `getchar()` is an `int`, not a `char`, so the EOF condition can be recognized in the code – user3629249 Nov 03 '19 at 20:12

2 Answers2

2

scanf has the interesting property that it leaves the newline character which terminated input in the input buffer, so you need to consume that before proceeding to any other input.

Just add a getchar() call after your scanf("%d", &a); and you should be good.

2

You can fix issue by using scanf(" %c",&c); or you need to write 1 more getchar() to consume newline character left from scanf("%d", &a);

Maqcel
  • 493
  • 5
  • 16