C中输入的[数字,数字]形式的输入条件不正确(Bad input conditions for [number,number] form of input in C)

编程入门 行业动态 更新时间:2024-10-17 07:36:27
C中输入的[数字,数字]形式的输入条件不正确(Bad input conditions for [number,number] form of input in C)

我有一个函数应该以[数字,数字]或[数字,数字]的形式读取输入,我需要为不良输入做条件。 输入应以EOF结束这是我现在作为条件输入的while循环。 问题是,它写了坏输入,例如[1,2] [3,4]] 5,4,但不是[1,2] [3,4] [5]。 当第三个坐标是“未完成”时,它不会写入错误的输入,它将适用于前2个坐标。 当输入少于2个坐标时,还应该有写入错误输入的条件。

int a,b; char par1, comma, par2; printf("Your input here:\n"); while(scanf("%c %lf %c %lf %c", &par1, &a, &comma, &b, &par2)!=EOF) { if((par1!='[') || (comma!=',') || (par2!=']')) {printf("Bad input.\n"); */ here I insert a and b in arrays */ i++; } if(!feof(stdin)) {free(arrayX); free(arrayY); printf("Bad input.\n);} if(i<2) {printf("Bad input\n");}

我会非常感谢任何帮助。 我是编程新手,请原谅我提出愚蠢的问题。

I have a function that should read input in form [number,number] or [number, number] and I need to do conditions for bad input. The input should end with EOF This is what i got now as a while cycle for input with conditions. The problem is, that it writes the bad input, for example for [1,2] [3,4] ]5,4 , but not for [1,2] [3,4] [5 . When the third coordinate is "unfinished" it won't write the bad input, it will work for the first 2 coordinates. Also there should be condition to write bad input, when less then 2 coordinates are input.

int a,b; char par1, comma, par2; printf("Your input here:\n"); while(scanf("%c %lf %c %lf %c", &par1, &a, &comma, &b, &par2)!=EOF) { if((par1!='[') || (comma!=',') || (par2!=']')) {printf("Bad input.\n"); */ here I insert a and b in arrays */ i++; } if(!feof(stdin)) {free(arrayX); free(arrayY); printf("Bad input.\n);} if(i<2) {printf("Bad input\n");}

I would be very thankful for any help. I am quite new in programming, so forgive me for stupid question.

最满意答案

在这种情况下,我会使用正则表达式。

我建议看看PCRE

使用scanf尝试这样做:

while((num_read = scanf("%c %lf %c %lf %c", &par1, &a, &comma, &b, &par2)) != EOF) { if((par1!='[') || (comma!=',') || (par2!=']' || num_read != 5)) { printf("Bad input.\n"); ....

此外,还有一些错误:缩进,或者在if之后打开。

In this case I would use regular expressions.

I'd recommend having a look at PCRE

For using scanftry doing it like this:

while((num_read = scanf("%c %lf %c %lf %c", &par1, &a, &comma, &b, &par2)) != EOF) { if((par1!='[') || (comma!=',') || (par2!=']' || num_read != 5)) { printf("Bad input.\n"); ....

Also, there is some mistake with either: the indentation, or the { that opens after the if.

更多推荐

本文发布于:2023-08-07 11:37:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1464499.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数字   不正确   形式   条件   Bad

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!