簡體   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