简体   繁体   中英

scanf making while loop to run 2 times automatically

scanf is automatically runnning the while loop 2 times when i enter the input a3 c4?

Chef develops his own computer program for playing chess. He is at the very beginning. At first he needs to write the module that will receive moves written by the players and analyze it. The module will receive a string and it should report at first whether this string represents the correct pair of cells on the chess board (we call such strings correct) and then report whether it represents the correct move depending on the situation on the chess board. Chef always has troubles with analyzing knight moves. So at first he needs a test program that can say whether a given string is correct and then whether it represents a correct knight move (irregardless of the situation on the chess board). The cell on the chessboard is represented as a string of two characters: first character is a lowercase Latin letter from a to h and the second character is a digit from 1 to 8. The string represents the correct pair of cells on the chess board if it composed of 5 characters where first two characters represent the cell where chess figure was, 3rd character is the dash "-" and the last two characters represent the destination cell. Input The first line contains a single integer T <= 50000, the number of test cases. T test cases follow. The only line of each test case contains a non-empty string composed the characters with ASCII-codes from 32 to 126. The length of the string is not greater than 10. Output For each test case, output a single line containing the word "Error" if the corresponding string does not represent the correct pair of cells on the chess board. Otherwise output "Yes" if this pair of cells represents the correct knight move and "No" otherwise. Example Input: 4 a1-b3 d2-h8 a3 c4 ErrorError

Output: Yes No Error Error

#include<stdio.h>
#include<string.h>
int main()
{
    int test;
    scanf("%d",&test);
    while(test--)
    {
        char str[11];
        scanf("%s[^\n]",str);
        int length=strlen(str);
        if(length>5 || length<5)
        {
            printf("Error\n");
        }
        else if(str[0]<97 || str[0]>104 || str[1]<49 || str[1]>57 || str[2]!=45 || str[3]<97 || str[3]>104 || str[4]<48 || str[4]>57)
        {
            printf("Error\n");
        }
        else
        {

            if(abs(str[0]-str[3])==1 && abs(str[1]-str[4])==2  ||  abs(str[0]-str[3])==2  ||abs(str[1]-str[4])==1)
            {
                printf("Yes\n");
            }
            else
            {
                printf("No\n");
            }
        }
    }
    return 0;
}

what should i do to make it run normally.

char str[11];
scanf("%s[^\n]",str);

No initialization? Should have been

char str[11];
str[0] = 0;
scanf("%s[^\n]",str);

In your test case, you wanted the loop to execute 4 times, and it does so. The a3 c4 input generates two errors, because scanf only reads a3 (as there is no hyphen between the two), and the rest remains in the buffer for the next scanf.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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