I'm trying to compare elements being fed in from a text file into my 2D array. An example of the textfile is as follows:
ABCDE
FGHIK
LMNOP
I know I can read in the file fine and can print each element as well.
I'm trying to go through and compare each character with the next. In my head this is how it works: If they match, move on to the next char, then compare that char with the next. If they do not match, then print "error" to the terminal and break out of the switch statement. Though when I run this in my program it only prints "Is this being accsessed?" "It sure is". Any help on what I could be doing wrong would be very helpful.
void match_char(char** array, int height, int width){
int a, i, j;
printf("Is this being accessed?");
a = 0;
i = 0;
j = 0;
switch(a){
case 1: if(a <1 ){
for(j = 0; j < width; j++){
if (array[i][j] != array[i][j+1]){
printf("error\n");
break;
} else if(j == width) {
a=1;
break;
}
}
}/* End of case 1*/
case 2: if (a > 0 && a < 2){
for(i = 0; i < height; i++){
if (array[i][j] != array[i+1][j]){
printf("error\n");
break;
}else if(i == height) {
printf("No error");
a=2;
break;
}
}
}/* End of case 2 */
case 3:
if (a > 2) {
a=0;
break;
}
}/*End of switch */
printf("It sure is");
}
switchstatement is wrong: cases 1 and 2 will never be executed.ais intialised with the value 0. As there is nocase 0or adefaultin theswitch,it skips theswitchsince none of the cases are trueawill always be 0 when you enter the switch, and there is no 0 case. What exactly isarepresenting? It looks like there are some other logic problems in the switch.