我有一个函数应该以[数字,数字]或[数字,数字]的形式读取输入,我需要为不良输入做条件。 输入应以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.
更多推荐
发布评论