简体   繁体   English

C-char scanf问题

[英]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. 我也尝试过使用getchar,但又遇到了同样的问题。

I use linux but execute the code with xterm. 我使用linux,但使用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 , 就像@codaddict 在这里说的那样,

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. 使用scanf读取输入时,按下返回键后将读取输入,但scanf不会使用返回键生成的换行符,这意味着下次您从标准输入中读取字符时,将有一个换行符可供使用读。

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); intscanf替换为以下内容: 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. 正如@UrbiJr正确提到的,解决方案是排除换行符。 In order to do that, that you could also use getchar() . 为了做到这一点,您还可以使用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. 我使用getchar()运行了您的代码,并且可以正常工作,我也有一台Linux机器,使用GCC进行了编译,并在GNOME Terminal中运行了二进制文件。
  2. Also, assuming you want to exit the program when you type the character 'p', are you sure case 'p': system("exit"); 另外,假设您要在键入字符“ p”时退出程序,是否确定case 'p': system("exit"); works on your machine? 在您的机器上工作? It did not work for me, so i used case 'p': exit(EXIT_SUCCESS); 它对我不起作用,所以我使用了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. 这会导致您的程序将获得“ \\ n”(来自先前输入的“ Enter”或“ newline”)作为输入。 So, put "getchar();" 因此,输入“ getchar();” before "scanf("%c",&eingabe);". 在“ 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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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