简体   繁体   中英

C - char scanf problems

I don't know why the code

scanf("%c",&eingabe);

everytime overleaps.

i try it with getchar too but same problem again.

I use linux but execute the code with xterm.

Anyone can help me?

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

int main()
{
    int z1,z2,erg=0;
    char eingabe;

    while(1){


    printf("Geben Sie die erste Zahl an: ");
    scanf("%d",&z1); //works
    fflush(stdin); //clear

    printf("\nGeben Sie die zweite Zahl an: ");
    scanf("%d",&z2); //works
    fflush(stdin);//clear

    erg=z1*z2; //works
    printf("\n%d * %d = %d",z1,z2,erg); //works

    printf("\n");
    printf("#######################");
    printf("\n");

    printf("Weiter = W\n");
    printf("Stop = P\n");

    printf("Eingabe: ");
    scanf("%c",&eingabe); //this is the line with the problem
    fflush(stdin); //clear

    switch(eingabe){
        case 'w':
        system("clear");
        break;
        case 'p':
        system("exit");
        break;
        default:
        printf("\nEingabe Unbekannt");
    }

    printf("\n");

    }


    return 0;
}

As @codaddict said here ,

When reading input using scanf, the input is read after the return key is pressed but the newline generated by the return key is not consumed by scanf, which means the next time you read a char from standard input there will be a newline ready to be read.

A simple solution is to exclude the newline character like this:

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

int main()
{
    int z1,z2,erg=0;
    char buf;
    char eingabe;

    while(1){


    printf("Geben Sie die erste Zahl an: ");
    scanf("%d",&z1); //works
    scanf("%c", &buf);

    printf("\nGeben Sie die zweite Zahl an: ");
    scanf("%d",&z2); //works
    scanf("%c", &buf);

    erg=z1*z2; //works
    printf("\n%d * %d = %d",z1,z2,erg); //works

    printf("\n");
    printf("#######################");
    printf("\n");

    printf("Weiter = W\n");
    printf("Stop = P\n");

    printf("Eingabe: ");
    scanf("%c",&eingabe); 

    switch(eingabe){
        case 'w':
            system("clear");
            break;
        case 'p':
            system("close");
            break;
        default:
            printf("\nEingabe Unbekannt");
    }

    printf("\n");

    }  


    return 0;
}

OR replacing the int s scanf s with something like this: scanf("%d%*c",&n); ,

THIS WAY:

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

int main()
{
    int z1,z2,erg=0;
    char buf;
    char eingabe;

    while(1){

        printf("Geben Sie die erste Zahl an: ");
        scanf("%d%*c",&z1);    //%*c removes the newline from the buffer

        printf("\nGeben Sie die zweite Zahl an: ");
        scanf("%d%*c",&z2);    //%*c removes the newline from the buffer

        erg=z1*z2; //works
        printf("\n%d * %d = %d",z1,z2,erg); //works

        printf("\n");
        printf("#######################");
        printf("\n");

        printf("Weiter = W\n");
        printf("Stop = P\n");

        printf("Eingabe: ");
        scanf("%c",&eingabe);    //no problem with this.

        switch(eingabe){
            case 'w':
            system("clear");
            break;
        case 'p':
            system("close");
            break;
        default:
            printf("\nEingabe Unbekannt");
        }

        printf("\n");

    }

    return 0;
}

And that's it my friend!

  1. as @UrbiJr rightly mentioned, solution would be to exclude your newline character. In order to do that, that you could also use getchar() . I ran your code with getchar() and it works, i have a Linux machine too, compiled using GCC and ran the binary in GNOME Terminal.
  2. Also, assuming you want to exit the program when you type the character 'p', are you sure case 'p': system("exit"); works on your machine? It did not work for me, so i used case 'p': exit(EXIT_SUCCESS); instead, and it worked.

     #include <stdio.h> #include <stdlib.h> int main() { int z1,z2,erg=0; char buf; char eingabe; while(1) { printf("Geben Sie die erste Zahl an: "); scanf("%d",&z1); //works getchar(); printf("\\nGeben Sie die zweite Zahl an: "); scanf("%d",&z2); //works getchar(); erg=z1*z2; //works printf("\\n%d * %d = %d",z1,z2,erg); //works printf("\\n"); printf("#######################"); printf("\\n"); printf("Weiter = W\\n"); printf("Stop = P\\n"); printf("Eingabe: "); scanf("%c",&eingabe); //works switch(eingabe){ case 'w': system("clear"); break; case 'p': exit(EXIT_SUCCESS); break; default: printf("\\nEingabe Unbekannt"); } printf("\\n"); } return 0; } 

It causes your program will get '\\n' ("Enter" or "newline" from the previous input) as the input. So, put "getchar();" before "scanf("%c",&eingabe);".

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

int main()
{
    int z1,z2,erg=0;
    char eingabe;

    while(1){


    printf("Geben Sie die erste Zahl an: ");
    scanf("%d",&z1); //works
    fflush(stdin); //clear

    printf("\nGeben Sie die zweite Zahl an: ");
    scanf("%d",&z2); //works
    fflush(stdin);//clear

    erg=z1*z2; //works
    printf("\n%d * %d = %d",z1,z2,erg); //works

    printf("\n");
    printf("#######################");
    printf("\n");

    printf("Weiter = W\n");
    printf("Stop = P\n");

    printf("Eingabe: ");

    getchar(); // this is the solution

    scanf("%c",&eingabe); //this is the line with the problem
    fflush(stdin); //clear

    switch(eingabe){
        case 'w':
        system("clear");
        break;
        case 'p':
        system("exit");
        break;
        default:
        printf("\nEingabe Unbekannt");
    }

    printf("\n");

    }


    return 0;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM