简体   繁体   English

为什么下一行不运行?

[英]Why doesnt this next line run?

The point of this program is to ensure valid input of two integers between 0 to 2 with a space in between (and strictly nothing else), and then to assign the result of these two integers to variables.这个程序的要点是确保有效输入介于 0 到 2 之间的两个整数,中间有一个空格(严格来说没有其他任何东西),然后将这两个整数的结果分配给变量。 For some reason this isn't allowed.由于某种原因,这是不允许的。

Very basic typecasting has me confused.非常基本的类型转换让我感到困惑。 The print statement %i and %i never runs, I can't see why.打印语句%i and %i从不运行,我不明白为什么。 even though I've put braces around the part that has "success" labelled.即使我在标有“成功”的部分周围放了大括号。 I've tried quite a few things.我已经尝试了很多东西。

#include <stdio.h>
#include <string.h>

int main(void)
{
    int row, column;
    char buffer[100];

    }

    printf("enter 2 values of row | column format \n");
    
    fgets(buffer,100,stdin);
    sscanf(buffer,"%c %c",&row, &column);
    printf("%i %i", row,column);

// only accepts integers between 0 and 2 inclusive
 
    if ((((int)row >=48) && ((int)row <= 50 )) && ((int)column >= 48) && ((int)column <= 50))
        {
        
        printf("success\n");
        printf("%i and %i", atoi(row), atoi(column));

        }
        
    else
        printf("fail\n");


    return 0;
}

Good that OP it trying to validate input.好的 OP 它试图验证输入。


To accept 2 int :接受 2 int

char dummy;  // Used to detect text after the 2 digits
if (sscanf(buffer,"%d %d %c", &row, &column, &dummy) != 2) {
  puts("Input is not 2 ints");
}

Then test the range然后测试范围

if (row < 0 || row > 2 || column < 0 || column > 2) {
  puts("Outside 0 to 2 range");
}

Additional code needed to detect out-of-range text for an int .需要额外的代码来检测int的超出范围的文本。 Something like the below which limits input to 1 digit each for the int and records the scan offset between the two int to insure some white-space.类似于下面的内容,将int的每个输入限制为 1 个数字,并记录两个int之间的扫描偏移量以确保一些空白。

int n1, n2;
if (sscanf(buffer,"%1d%n %n%1d %c", &row, &n1, &n2, &column, &dummy) != 2 || n1 == n2) {
  puts("Input is not 2 ints");
}

Save time省时间

Enable all compiler warnings.启用所有编译器警告。 A good compiler will complain about sscanf(buffer,"%c %c",&row, &column);一个好的编译器会抱怨sscanf(buffer,"%c %c",&row, &column); - "%c" expects an char * , not int * . - "%c"需要一个char * ,而不是int *

Move away from the scanf() family of functions.远离scanf()函数系列。 They are the cause of so many SO questions...它们是许多 SO 问题的原因......

The following should be simple to understand.下面应该简单易懂。 When you wake up I'd be happy to elaborate on anything you might find perplexing.当您醒来时,我很乐意详细说明您可能会感到困惑的任何事情。

#include <stdio.h>
#include <string.h>

int main( void ) {
    printf("enter 2 values of row | column format \n");
    
    char buf[ 100 ];
    fgets( buf, 100, stdin );

    if( buf[3] == '\n' // fgets leaves LF in the buffer
    &&  ('0' <= buf[0] && buf[0] <= '2' )   // limit ASCII digit
    &&  buf[1] == ' '                       // single space between
    &&  ('0' <= buf[2] && buf[2] <= '2' )   // limit ASCII digit
    ) {
        int row = buf[0] - '0', col = buf[2] - '0';
        printf( "success\n%i and %i", row, col );
    } else
        puts( "fail" );

    return 0;
}

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

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