简体   繁体   English

为什么 scanf 在第一次使用时有效,但在第二次使用时却无效?

[英]Why does scanf work the first time it's used but not the second?

So I'm simply trying to accept input from the user in the form 1 2,or 0 0, 2 2, 0 1, etc. If the user enters this the first time around then everything works as expected.因此,我只是尝试接受用户以 1 2 或 0 0、2 2、0 1 等形式输入的内容。如果用户是第一次输入此内容,那么一切都会按预期进行。 However, if they get the input wrong from the beginning, the program tells them that they have got it wrong and asks for the input again, but then this input is seemingly transposed and the first variable is blank whilst the second assumes the value intended for the first.然而,如果他们从一开始就输入错误,程序会告诉他们他们输入错误并再次要求输入,但随后这个输入似乎被调换了,第一个变量为空,而第二个变量采用了预期的值首先。 What's going on?这是怎么回事?

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

int validation();

int main ( void ) {
    

    int valid = 0;
    
    while ( valid == 0 ){
        valid = validation();
    }

    return 0;
}

int validation(){
    
    char cfirst, csecond;
    int first, second, c;

    printf("Please enter your number:\n");
    scanf("%c %c", &cfirst, &csecond);

    printf("cfirst = %c, csecond = %c\n", cfirst, csecond);

    
    if( cfirst == '0' ){
        first = 0;
    }
    else if( ( cfirst == '1' || cfirst == '2' ) ){
        first = cfirst - '0';      // alternative to atoi for a singular character              
    }    
    else{
        printf("Move rejected. Please try again\n");
        while( ( c = getchar() ) != EOF && c != '\n');
        return 0;
    }
    
    if( csecond == '0'){
        second = 0;
    }
    else if( csecond == '1' || csecond == '2' ){
        second = csecond - '0';    // alternative to atoi for a singular character
    }
    else{
        printf("Move rejected. Please try again\n");
        while( ( c = getchar() ) != EOF && c != '\n');
        return 0;
    }

    //first = cfirst - '0';           
    //second = csecond - '0';

    printf("Your first number = %d\n", first);
    printf("Your second number = %d\n", second);

    return 0;
}

I tried using the line:我尝试使用该行:

while( ( c = getchar() );= EOF && c != '\n'); while( ( c = getchar() );= EOF && c != '\n');

to clear the input buffer (I think) so that scanf would work as it did the first time.清除输入缓冲区(我认为),以便 scanf 像第一次一样工作。 This didn't work.这没有用。 Now I'm at a loss.现在我不知所措了。

When reading a char with scanf you are reading all the characters in the buffer, including the enter '\n'.使用scanf读取char时,您正在读取缓冲区中的所有字符,包括输入 '\n'。 scanf reads the first data avalibale that matches the type declared and then stops. scanf读取第一个与声明的类型匹配的可用数据,然后停止。 If your program reads the data succesfully the first time it stores the values, but in the buffer remains the enter '\n'.如果您的程序在第一次存储值时成功读取数据,但缓冲区中仍保留输入“\n”。 When trying to read a second time it gets the first two elements in the buffer, in this case the enter '\n' and the first value.当尝试第二次读取时,它会获取缓冲区中的前两个元素,在本例中为输入 '\n' 和第一个值。 When reading data with scanf you should always clean the buffer.使用scanf读取数据时,您应该始终清理缓冲区。 You can use while(getchar!='\n') for cleaning the buffer after reading with scanf您可以使用while(getchar!='\n')在使用scanf读取后清理缓冲区

Hope it helps!希望能帮助到你!

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

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