Question

My nested loops only print one char, 'c', which is the correct first char to print, but I cannot figure out why my loop won't keep looping through the alphabet. Any assistance in determining my loop error would be great.

#include <stdio.h>
#include <stdlib.h>

void problem_1_function();

int main(){

    problem_1_function();
    return (0);

}

void problem_1_function(){

    FILE *the_cipher_file;
    the_cipher_file = fopen("cipher.txt", "r");
    FILE *the_message_file;
    the_message_file = fopen("message.txt", "r");
    FILE * the_decode_file;
    the_decode_file = fopen("decode.txt", "w");
    int the_letter_counter = 0;
    int the_alphabet_array[100];
    int size_of_alphabet = 0;
    int size_of_message = 0;
    int the_message_counter = 0;
    int the_message_array[100];
    char the_decode_array [15];
    char the_letter_char[26] = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','w','x','y','z'};

    if(the_decode_file == NULL){
            printf("Error opening file!\n");
}
    if(!the_cipher_file){
            printf("Error: Filename \"cipher.txt\" not found!\n");
    }

     while(fscanf(the_cipher_file, " %d%*[,] ", &size_of_alphabet) > 0 && the_letter_counter < 100){
            the_alphabet_array[the_letter_counter] = size_of_alphabet;
            //printf("%d ", size_of_alphabet);
            the_letter_counter++;
            }

    if(!the_message_file){
            printf("Error: Filename \"cipher.txt\" not found!\n");
    }
    while(fscanf(the_message_file, " %d%*[,] ", &size_of_message) > 0 && the_message_counter < 100){
            the_message_array[the_message_counter] = size_of_message;
            //printf("%d ", size_of_message);
            the_message_counter++;
}
    int message_equals_cipher = 0;
    int message_equals_cipher2 = 0;

    for(message_equals_cipher; message_equals_cipher < sizeof(the_message_array); message_equals_cipher++){      //these nested loops go through the alphabet to print letters corresponding to arrays...
            for(message_equals_cipher2; message_equals_cipher2 < 26; message_equals_cipher2++){
                    if(the_message_array[message_equals_cipher] == the_alphabet_array[message_equals_cipher2]){
                            the_decode_array[message_equals_cipher] = the_letter_char[message_equals_cipher2];
                            fprintf(the_decode_file, "%c", the_decode_array[message_equals_cipher]);
                    }
            }
    }

    fclose(the_cipher_file);
    fclose(the_message_file);
    fclose(the_decode_file);
}
Was it helpful?

Solution

int message_equals_cipher = 0;
int message_equals_cipher2 = 0;

for(message_equals_cipher; ...
    for(message_equals_cipher2; ...

You're setting these to 0 outside the loops .. the initialization expressions of your for statements don't do anything -- if you set your warning level high enough, your compiler should tell you that. Because you don't reset message_equals_cipher2, your inner loop will only run once total. You want

for(message_equals_cipher = 0; ...
    for(message_equals_cipher2 = 0; ...

If you are compiling C99 or higher, you can do

for(int message_equals_cipher = 0; ...
    for(int message_equals_cipher2 = 0; ...

and get rid of the previous definitions of those variables.

OTHER TIPS

Yes, the problem in your nested for loop is that you are not initializing your message_equals_cipher2 variable to 0 in your second for loop. The nested code should be like :

  for(message_equals_cipher; message_equals_cipher < sizeof(the_message_array); message_equals_cipher++)
  {
      for(message_equals_cipher2=0; message_equals_cipher2 < 26; message_equals_cipher2++)
      {
                     // Your stuff
      }
  }

I will agree with jim, instead of initializing your variables message_equals_cipher and message_equals_cipher2 before nested for loops. You can do it as jim specified.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top