繁体   English   中英

C-char scanf问题

[英]C - char scanf problems

我不知道为什么代码

scanf("%c",&eingabe);

每次重叠。

我也尝试过使用getchar,但又遇到了同样的问题。

我使用linux,但使用xterm执行代码。

有人可以帮助我吗?

#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;
}

就像@codaddict 在这里说的那样,

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

一个简单的解决方案是像这样排除换行符:

#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;
}

intscanf替换为以下内容: scanf("%d%*c",&n);

这条路:

#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;
}

就是我的朋友!

  1. 正如@UrbiJr正确提到的,解决方案是排除换行符。 为了做到这一点,您还可以使用getchar() 我使用getchar()运行了您的代码,并且可以正常工作,我也有一台Linux机器,使用GCC进行了编译,并在GNOME Terminal中运行了二进制文件。
  2. 另外,假设您要在键入字符“ p”时退出程序,是否确定case 'p': system("exit"); 在您的机器上工作? 它对我不起作用,所以我使用了case 'p': exit(EXIT_SUCCESS); 相反,它奏效了。

     #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; } 

这会导致您的程序将获得“ \\ n”(来自先前输入的“ Enter”或“ newline”)作为输入。 因此,输入“ getchar();” 在“ 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