外来错误?他们在哪里&他们是什么? C编程
Extraneous Errors? Where & What Are They? C Programming
我的代码已经反复校对了,我仍然无法弄清楚为什么其中一些行会给出错误信息。只有一个 '}' 的行被称为错误。这怎么可能?
#include <stdio.h>
#include <getword.h>
#define STORAGE 255
int getword(char *w) {
char *word = w; /*points to first char in array */
char iochar; /*holds current var being processed */
int counter = 0; /* counts number of characters in a word */
char *first = w; /* points to first character in array */
int bslashflag = 0; /* 0 if no backslash seen, 1 otherwise */
int negateornah = 0; /* if set to 1, return value is negated */
while( ( iochar = getchar() ) != EOF ) { /*runs until EOF is encountered */
if( iochar =='\') /* if a slash char is found in the word*/
{ bslashflag = 1; /*acknowledge that*/
/*backslash was seen*/
iochar = getchar(); /* skip it & go to the next char*/
if(iochar == '\' ){ /*and if it ends up being another*/
/*slash, slash designated to be added in */
*word = iochar; /* so go ahead and add it in */
*word++; /* and go to the next empty slot */
counter++; /*and increment your counter */
iochar = getchar(); /* *ALERT << may or may not beneccessary */
} else if( iochar == '<' || iochar == '>' || iochar == '|' || iochar == '$' || iochar == '&' )
{ /* if a metacharacter is found after a bslash, */
/*just treat it like any other character*/
*word = iochar;
*word++;
counter+=1;
}
}
else if (iochar == ' ')
{
if(counter == 0) //*f the first char is a space */
{
while( (iochar = getchar() ) == ' ') { } /*skips leading blanks*/
} else if(bslashflag == 1) { /*if there was bslash before space*/
*word++ = iochar; /*add the space as part of string*/
counter++; /*increments counter*/
bslashflag--; /*resets backslash flag for next char check*/
}
else
{ /* until alphanum char is found*/
*word = '[=10=]'; /*if not leading blank, its the end of word*/
if(negateornah == 1)
{
negateornah= 0; /*reset flag for next run*/
counter *= -1; /*negate the value in counter*/
return counter;
}
return counter; /* return word size */
}
}
else if( iochar == '\n' || iochar == '<' || iochar == '>' || iochar == '|' || iochar == '$' || iochar == '&' )
{ /*if a metacharacter is encountered*/
if(iochar == '\n' && counter == 0) /* if the first char in the word is \n */
{
*word = '[=10=]'; /* and null terminate the array; TRY *first INCASE OF ERROR*/
if(negateornah == 1) {
negateornah= 0; /*reset flag for next run*/
counter *= -1; /*negate the value in counter*/
return counter;
}
return counter; /* null terminate the array and return count so scan can continue*/
}
else if(iochar == '\n' && counter != 0)
{
*word = '[=10=]' ;
ungetc('\n',stdin); /* put it back where you got it*/
if(negateornah == 1) {
negateornah= 0; /*reset flag for next run*/
counter *= -1; /*negate the value in counter*/
return counter;
}
return counter; /*and return a number so the scan can continue*/
}
else if(iochar == '$')
{
if(counter ==0) {
negateornah = 1; } /*means $ was first char in word, set negate count flag*/
if(bslashflag == 1) /* if a backslash was seen before this character*/
{
*word = iochar; /* just add it into the word*/
*word++; /* as part of the string*/
counter++; /* and count it as a letter*/
bslashflag=0; /*reset flag for next check*/
}
*word++ = iochar; /*add it into word*/
counter++; /*and account for addition*/
return counter;
}
else if(iochar == '|') {
if(counter==0){
*word = iochar;
*word++;
counter++
return counter;
}
if(bslashflag == 1) {
*word = iochar;
*word++;
counter++;
int bslashflag = 0;
}
if(negateornah == 1) {
negateornah= 0; //reset flag for next run*/
counter *= -1; //negate the value in counter*/
return counter;
}
return counter;
}
else if(iochar == '&') {
if (counter ==0) {
*word++ = iochar;
*word = '[=10=]';
counter++;
return counter;
}
if(bslashflag == 1) {
*word = iochar;
*word++;
counter++;
bslashflag = 0;
}
if(negateornah == 1) {
negateornah= 0; /*reset flag for next run*/
counter *= -1; /*negate the value in counter*/
return counter;
}
return counter;
}
else if( iochar == '<' || iochar == '>') {
if(counter==0){
*word++ = iochar;
*word = '[=10=]';
return counter;
}
if(bslashflag==1) {
*word = iochar;
*word++;
counter++;
bslashflag = 0;
}
if(negateornah == 1) {
negateornah= 0; /*reset flag for next run*/
counter *= -1; /*negate the value in counter*/
return counter;
}
return counter;
}
else if (iochar == ' ') /* if space is encountered*/
{
if(bslashflag == 1){ /* if a backslash was seen before this*/
*word = iochar;
*word++;
counter++;
bslashflag = 0;
}
else if(counter == 0) /* as the first character */
{
while( (iochar = getchar() ) == ' ') { } /*keep going until*/
}
else { /* iochar is not a space*/
*word = '[=10=]'; /* this block handles the word ending space*/
if(negateornah == 1) {
negateornah= 0; /*reset flag for next run*/
counter *= -1; /*negate the value in counter*/
return counter;
}
return counter; /* by null terminating the word at the end*/
} /* and returning the count and printed word*/
}
*word++ = iochar; /* add iochar into the array if its an alphanum char*/
counter+=1; /* increment counter to reflect added char*/
}
*word = '[=10=]'; /* if EOF is encountered*/
*first = '[=10=]'; /* null terminate entire word, clearing it from the array*/
if(counter != 0){
if(negateornah == 1) {
negateornah= 0; /*reset flag for next run*/
counter *= -1; /*negate the value in counter*/
return counter;
}
return counter; /*ensure word continues in next run*/
}
return -1; /*and return -1 for EOF*/
}
重复次数最多的错误似乎是:
*word = iochar;
*word++;
尽管这样做很好:
*word++ = iochar;
如果要拆分两个步骤(间接设置值;递增指针),正确的方法是:
*word = iochar;
word++;
我看到的下一个重复次数最多的问题,它不会阻止您的代码编译或 运行,是您在 return
之前重置局部变量——没有意义,一次你打电话 return
他们都走了,不会转移到下一个电话。
在那之后,您的缩进风格会妨碍您的工作——选择一种并坚持使用。即使您在另一个程序中使用不同的样式,也没关系,只要在文件中保持一致即可。
最后,一路编译测试,不要写那么多代码。您永远不应该有这么多错误需要追踪。当您添加新的代码块时,逐步编译和测试。
应用上述修复和建议,以及其他人建议的修复,并将您的代码转换为 C 语言;-) 这是一个可以编译的返工。这并不意味着它是正确的,只是编译器充分接受它,你可以开始真正的调试:
#include <stdio.h>
#include <stdbool.h>
// #include <getword.h>
int getword(char *w) {
char *word = w; /* points to first char in array */
char iochar; /* holds current var being processed */
int counter = 0; /* counts number of characters in a word */
char *first = w; /* points to first character in array */
bool bslashflag = false; /* false if no backslash seen, true otherwise */
bool negateornah = false; /* if true, return value is negated */
while ((iochar = getchar()) != EOF) { /* runs until EOF is encountered */
if (iochar == '\') { /* if a slash char is found in the word */
bslashflag = true; /* acknowledge that backslash was seen */
iochar = getchar(); /* skip it & go to the next char */
if (iochar == '\' ) { /* and if it ends up being another */
/* slash, slash designated to be added in */
*word++ = iochar; /* add it in and go to the next empty slot */
counter++; /* and increment your counter */
iochar = getchar(); /* *ALERT << may or may not be neccessary */
} else if (iochar == '<' || iochar == '>' || iochar == '|' || iochar == '$' || iochar == '&' ) {
/* if a metacharacter is found after a backslash, */
/* just treat it like any other character */
*word++ = iochar;
counter++;
}
} else if (iochar == ' ') {
if (counter == 0) { /* if the first char is a space */
while ((iochar = getchar()) == ' ') {
/* skip leading blanks */
}
} else if (bslashflag) { /* if there was bslash before space */
*word++ = iochar; /* add the space as part of string */
counter++; /* increment counter */
bslashflag = false; /* reset backslash flag for next char check */
} else { /* until alphanum char is found */
*word = '[=13=]'; /* if not leading blank, its the end of word */
if (negateornah) {
counter *= -1; /* negate the value in counter */
}
return counter; /* return word size */
}
} else if (iochar == '\n' || iochar == '<' || iochar == '>' || iochar == '|' || iochar == '$' || iochar == '&' ) {
/* if a metacharacter is encountered */
if (iochar == '\n' && counter == 0) { /* if the first char in the word is \n */
*word = '[=13=]'; /* and null terminate the array; TRY *first INCASE OF ERROR */
if (negateornah) {
counter *= -1; /* negate the value in counter */
}
return counter; /* null terminate the array and return count so scan can continue */
} else if (iochar == '\n' && counter != 0) {
*word = '[=13=]' ;
ungetc('\n', stdin); /* put it back where you got it */
if (negateornah) {
counter *= -1; /* negate the value in counter */
}
return counter; /* and return a number so the scan can continue */
} else if (iochar == '$') {
if (counter == 0) {
negateornah = true; /* means $ was first char in word, set negate count flag */
}
if (bslashflag) { /* if a backslash was seen before this character */
*word++ = iochar; /* just add it into the word as part of the string */
counter++; /* and count it as a letter */
}
*word++ = iochar; /* add it into word */
return counter + 1; /* and account for addition */
} else if (iochar == '|') {
if (counter == 0) {
*word++ = iochar;
return counter + 1;
}
if (bslashflag) {
*word++ = iochar;
counter++;
}
if (negateornah) {
counter *= -1; /* negate the value in counter */
}
return counter;
} else if (iochar == '&') {
if (counter == 0) {
*word++ = iochar;
*word = '[=13=]';
return counter + 1;
}
if (bslashflag) {
*word++ = iochar;
counter++;
}
if (negateornah) {
counter *= -1; /* negate the value in counter */
}
return counter;
} else if (iochar == '<' || iochar == '>') {
if (counter == 0) {
*word++ = iochar;
*word = '[=13=]';
return counter;
}
if (bslashflag) {
*word++ = iochar;
counter++;
}
if (negateornah) {
counter *= -1; /* negate the value in counter */
}
return counter;
} else if (iochar == ' ') { /* if space is encountered */
if (bslashflag) { /* if a backslash was seen before this */
*word++ = iochar;
counter++;
bslashflag = false;
} else if (counter == 0) { /* as the first character */
while ((iochar = getchar() ) == ' ') {
/* keep going until iochar is not a space */
}
} else {
*word = '[=13=]'; /* this block handles the word ending space */
if (negateornah) {
counter *= -1; /* negate the value in counter */
}
return counter; /* by null terminating the word at the end */
} /* and return the count and printed word */
}
*word++ = iochar; /* add iochar into the array if its an alphanum char */
counter++; /* increment counter to reflect added char */
}
*word = '[=13=]'; /* if EOF is encountered */
*first = '[=13=]'; /* null terminate entire word, clearing it from the array */
if (counter != 0) {
if (negateornah) {
counter *= -1; /* negate the value in counter */
}
return counter;
}
}
return -1; /* and return -1 for EOF */
}
我的代码已经反复校对了,我仍然无法弄清楚为什么其中一些行会给出错误信息。只有一个 '}' 的行被称为错误。这怎么可能?
#include <stdio.h>
#include <getword.h>
#define STORAGE 255
int getword(char *w) {
char *word = w; /*points to first char in array */
char iochar; /*holds current var being processed */
int counter = 0; /* counts number of characters in a word */
char *first = w; /* points to first character in array */
int bslashflag = 0; /* 0 if no backslash seen, 1 otherwise */
int negateornah = 0; /* if set to 1, return value is negated */
while( ( iochar = getchar() ) != EOF ) { /*runs until EOF is encountered */
if( iochar =='\') /* if a slash char is found in the word*/
{ bslashflag = 1; /*acknowledge that*/
/*backslash was seen*/
iochar = getchar(); /* skip it & go to the next char*/
if(iochar == '\' ){ /*and if it ends up being another*/
/*slash, slash designated to be added in */
*word = iochar; /* so go ahead and add it in */
*word++; /* and go to the next empty slot */
counter++; /*and increment your counter */
iochar = getchar(); /* *ALERT << may or may not beneccessary */
} else if( iochar == '<' || iochar == '>' || iochar == '|' || iochar == '$' || iochar == '&' )
{ /* if a metacharacter is found after a bslash, */
/*just treat it like any other character*/
*word = iochar;
*word++;
counter+=1;
}
}
else if (iochar == ' ')
{
if(counter == 0) //*f the first char is a space */
{
while( (iochar = getchar() ) == ' ') { } /*skips leading blanks*/
} else if(bslashflag == 1) { /*if there was bslash before space*/
*word++ = iochar; /*add the space as part of string*/
counter++; /*increments counter*/
bslashflag--; /*resets backslash flag for next char check*/
}
else
{ /* until alphanum char is found*/
*word = '[=10=]'; /*if not leading blank, its the end of word*/
if(negateornah == 1)
{
negateornah= 0; /*reset flag for next run*/
counter *= -1; /*negate the value in counter*/
return counter;
}
return counter; /* return word size */
}
}
else if( iochar == '\n' || iochar == '<' || iochar == '>' || iochar == '|' || iochar == '$' || iochar == '&' )
{ /*if a metacharacter is encountered*/
if(iochar == '\n' && counter == 0) /* if the first char in the word is \n */
{
*word = '[=10=]'; /* and null terminate the array; TRY *first INCASE OF ERROR*/
if(negateornah == 1) {
negateornah= 0; /*reset flag for next run*/
counter *= -1; /*negate the value in counter*/
return counter;
}
return counter; /* null terminate the array and return count so scan can continue*/
}
else if(iochar == '\n' && counter != 0)
{
*word = '[=10=]' ;
ungetc('\n',stdin); /* put it back where you got it*/
if(negateornah == 1) {
negateornah= 0; /*reset flag for next run*/
counter *= -1; /*negate the value in counter*/
return counter;
}
return counter; /*and return a number so the scan can continue*/
}
else if(iochar == '$')
{
if(counter ==0) {
negateornah = 1; } /*means $ was first char in word, set negate count flag*/
if(bslashflag == 1) /* if a backslash was seen before this character*/
{
*word = iochar; /* just add it into the word*/
*word++; /* as part of the string*/
counter++; /* and count it as a letter*/
bslashflag=0; /*reset flag for next check*/
}
*word++ = iochar; /*add it into word*/
counter++; /*and account for addition*/
return counter;
}
else if(iochar == '|') {
if(counter==0){
*word = iochar;
*word++;
counter++
return counter;
}
if(bslashflag == 1) {
*word = iochar;
*word++;
counter++;
int bslashflag = 0;
}
if(negateornah == 1) {
negateornah= 0; //reset flag for next run*/
counter *= -1; //negate the value in counter*/
return counter;
}
return counter;
}
else if(iochar == '&') {
if (counter ==0) {
*word++ = iochar;
*word = '[=10=]';
counter++;
return counter;
}
if(bslashflag == 1) {
*word = iochar;
*word++;
counter++;
bslashflag = 0;
}
if(negateornah == 1) {
negateornah= 0; /*reset flag for next run*/
counter *= -1; /*negate the value in counter*/
return counter;
}
return counter;
}
else if( iochar == '<' || iochar == '>') {
if(counter==0){
*word++ = iochar;
*word = '[=10=]';
return counter;
}
if(bslashflag==1) {
*word = iochar;
*word++;
counter++;
bslashflag = 0;
}
if(negateornah == 1) {
negateornah= 0; /*reset flag for next run*/
counter *= -1; /*negate the value in counter*/
return counter;
}
return counter;
}
else if (iochar == ' ') /* if space is encountered*/
{
if(bslashflag == 1){ /* if a backslash was seen before this*/
*word = iochar;
*word++;
counter++;
bslashflag = 0;
}
else if(counter == 0) /* as the first character */
{
while( (iochar = getchar() ) == ' ') { } /*keep going until*/
}
else { /* iochar is not a space*/
*word = '[=10=]'; /* this block handles the word ending space*/
if(negateornah == 1) {
negateornah= 0; /*reset flag for next run*/
counter *= -1; /*negate the value in counter*/
return counter;
}
return counter; /* by null terminating the word at the end*/
} /* and returning the count and printed word*/
}
*word++ = iochar; /* add iochar into the array if its an alphanum char*/
counter+=1; /* increment counter to reflect added char*/
}
*word = '[=10=]'; /* if EOF is encountered*/
*first = '[=10=]'; /* null terminate entire word, clearing it from the array*/
if(counter != 0){
if(negateornah == 1) {
negateornah= 0; /*reset flag for next run*/
counter *= -1; /*negate the value in counter*/
return counter;
}
return counter; /*ensure word continues in next run*/
}
return -1; /*and return -1 for EOF*/
}
重复次数最多的错误似乎是:
*word = iochar;
*word++;
尽管这样做很好:
*word++ = iochar;
如果要拆分两个步骤(间接设置值;递增指针),正确的方法是:
*word = iochar;
word++;
我看到的下一个重复次数最多的问题,它不会阻止您的代码编译或 运行,是您在 return
之前重置局部变量——没有意义,一次你打电话 return
他们都走了,不会转移到下一个电话。
在那之后,您的缩进风格会妨碍您的工作——选择一种并坚持使用。即使您在另一个程序中使用不同的样式,也没关系,只要在文件中保持一致即可。
最后,一路编译测试,不要写那么多代码。您永远不应该有这么多错误需要追踪。当您添加新的代码块时,逐步编译和测试。
应用上述修复和建议,以及其他人建议的修复,并将您的代码转换为 C 语言;-) 这是一个可以编译的返工。这并不意味着它是正确的,只是编译器充分接受它,你可以开始真正的调试:
#include <stdio.h>
#include <stdbool.h>
// #include <getword.h>
int getword(char *w) {
char *word = w; /* points to first char in array */
char iochar; /* holds current var being processed */
int counter = 0; /* counts number of characters in a word */
char *first = w; /* points to first character in array */
bool bslashflag = false; /* false if no backslash seen, true otherwise */
bool negateornah = false; /* if true, return value is negated */
while ((iochar = getchar()) != EOF) { /* runs until EOF is encountered */
if (iochar == '\') { /* if a slash char is found in the word */
bslashflag = true; /* acknowledge that backslash was seen */
iochar = getchar(); /* skip it & go to the next char */
if (iochar == '\' ) { /* and if it ends up being another */
/* slash, slash designated to be added in */
*word++ = iochar; /* add it in and go to the next empty slot */
counter++; /* and increment your counter */
iochar = getchar(); /* *ALERT << may or may not be neccessary */
} else if (iochar == '<' || iochar == '>' || iochar == '|' || iochar == '$' || iochar == '&' ) {
/* if a metacharacter is found after a backslash, */
/* just treat it like any other character */
*word++ = iochar;
counter++;
}
} else if (iochar == ' ') {
if (counter == 0) { /* if the first char is a space */
while ((iochar = getchar()) == ' ') {
/* skip leading blanks */
}
} else if (bslashflag) { /* if there was bslash before space */
*word++ = iochar; /* add the space as part of string */
counter++; /* increment counter */
bslashflag = false; /* reset backslash flag for next char check */
} else { /* until alphanum char is found */
*word = '[=13=]'; /* if not leading blank, its the end of word */
if (negateornah) {
counter *= -1; /* negate the value in counter */
}
return counter; /* return word size */
}
} else if (iochar == '\n' || iochar == '<' || iochar == '>' || iochar == '|' || iochar == '$' || iochar == '&' ) {
/* if a metacharacter is encountered */
if (iochar == '\n' && counter == 0) { /* if the first char in the word is \n */
*word = '[=13=]'; /* and null terminate the array; TRY *first INCASE OF ERROR */
if (negateornah) {
counter *= -1; /* negate the value in counter */
}
return counter; /* null terminate the array and return count so scan can continue */
} else if (iochar == '\n' && counter != 0) {
*word = '[=13=]' ;
ungetc('\n', stdin); /* put it back where you got it */
if (negateornah) {
counter *= -1; /* negate the value in counter */
}
return counter; /* and return a number so the scan can continue */
} else if (iochar == '$') {
if (counter == 0) {
negateornah = true; /* means $ was first char in word, set negate count flag */
}
if (bslashflag) { /* if a backslash was seen before this character */
*word++ = iochar; /* just add it into the word as part of the string */
counter++; /* and count it as a letter */
}
*word++ = iochar; /* add it into word */
return counter + 1; /* and account for addition */
} else if (iochar == '|') {
if (counter == 0) {
*word++ = iochar;
return counter + 1;
}
if (bslashflag) {
*word++ = iochar;
counter++;
}
if (negateornah) {
counter *= -1; /* negate the value in counter */
}
return counter;
} else if (iochar == '&') {
if (counter == 0) {
*word++ = iochar;
*word = '[=13=]';
return counter + 1;
}
if (bslashflag) {
*word++ = iochar;
counter++;
}
if (negateornah) {
counter *= -1; /* negate the value in counter */
}
return counter;
} else if (iochar == '<' || iochar == '>') {
if (counter == 0) {
*word++ = iochar;
*word = '[=13=]';
return counter;
}
if (bslashflag) {
*word++ = iochar;
counter++;
}
if (negateornah) {
counter *= -1; /* negate the value in counter */
}
return counter;
} else if (iochar == ' ') { /* if space is encountered */
if (bslashflag) { /* if a backslash was seen before this */
*word++ = iochar;
counter++;
bslashflag = false;
} else if (counter == 0) { /* as the first character */
while ((iochar = getchar() ) == ' ') {
/* keep going until iochar is not a space */
}
} else {
*word = '[=13=]'; /* this block handles the word ending space */
if (negateornah) {
counter *= -1; /* negate the value in counter */
}
return counter; /* by null terminating the word at the end */
} /* and return the count and printed word */
}
*word++ = iochar; /* add iochar into the array if its an alphanum char */
counter++; /* increment counter to reflect added char */
}
*word = '[=13=]'; /* if EOF is encountered */
*first = '[=13=]'; /* null terminate entire word, clearing it from the array */
if (counter != 0) {
if (negateornah) {
counter *= -1; /* negate the value in counter */
}
return counter;
}
}
return -1; /* and return -1 for EOF */
}