C - char scanf问题

时间:2016-12-28 00:38:25

标签: c

我不知道为什么代码

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

3 个答案:

答案 0 :(得分:0)

正如@codaddict所说here

  

使用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;
}

用以下内容替换int s scanfscanf("%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 :(得分:0)

  1. 正如@UrbiJr正确提到的,解决方案是排除你的换行符。为此,您还可以使用getchar()。 我使用getchar()运行你的代码并且它有效,我也有一台Linux机器,使用GCC编译并在GNOME终端中运行二进制文件。
  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;
       }
    

答案 2 :(得分:0)

这会导致您的计划获得&#39; \ n&#39; (&#34;输入&#34;或&#34;换行符&#34;来自先前的输入)作为输入。所以,把&#34; getchar();&#34;之前&#34; scanf(&#34;%c&#34;,&amp; eingabe);&#34;。

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