我一直在尝试遍历预定的字符数组并将其与扫描的单个字符进行比较。如果扫描的字符在数组中,我想将它添加到二维数组中,如果它不在数组中,我想进行错误处理。
我的代码目前是
char c;
char legalChar[] = "./\\=@ABCDEFGHIJKLMNOPQRSTUVWXYZ\n";
int rowCount = 0;
int colCOunt = 0;
int i = 0;
FILE * map;
while (c = fgetc(map), c != EOF) {
while (i < (sizeof(legalChar))){
if (c == legalChar[i]){
if (c == '\n'){
/*Add 1 to number of rows and start counting columns again */
rowCount++;
colCount = 0;
}
else {
/*would have code to add char to 2d array here */
colCount++;
}
}
i++;
}
我计划有
if (c != legalChar[i]){
/*Error handling */
}
但这不起作用,因为它只是在每次迭代时跳入这个 if 语句。
目前程序的输出是 colCount 被赋值为 1,rowCount 保持为 0。迭代的所有字符都在 legalChar[] 数组中,所以我不确定我做错了什么。
任何建议将不胜感激。
谢谢