【问题标题】:C - Nested for loops not printing multiple elementsC - 嵌套for循环不打印多个元素
【发布时间】:2014-03-01 05:25:18
【问题描述】:

我的嵌套循环只打印一个字符“c”,这是要打印的正确第一个字符,但我不知道为什么我的循环不会继续循环遍历字母表。在确定我的循环错误方面的任何帮助都会很棒。

#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);
}

【问题讨论】:

    标签: c arrays nested-loops


    【解决方案1】:
    int message_equals_cipher = 0;
    int message_equals_cipher2 = 0;
    
    for(message_equals_cipher; ...
        for(message_equals_cipher2; ...
    

    您在循环之外将这些设置为 0 .. for 语句的初始化表达式不会做任何事情——如果您将警告级别设置得足够高,您的编译器应该会告诉您这一点。因为您没有重置message_equals_cipher2,所以您的内部循环总共只会运行一次。你想要的

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

    如果你正在编译 C99 或更高版本,你可以这样做

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

    并摆脱以前对这些变量的定义。

    【讨论】:

      【解决方案2】:

      是的,嵌套的for 循环中的问题是,您没有在第二个for 循环中将message_equals_cipher2 变量初始化为0。 嵌套代码应该是这样的:

        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
            }
        }
      

      我同意 jim 的观点,而不是在嵌套的 for 循环之前初始化变量 message_equals_ciphermessage_equals_cipher2。您可以按照 jim 指定的方式进行操作。

      【讨论】:

        猜你喜欢
        • 2019-02-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-10-15
        • 1970-01-01
        • 2018-04-04
        • 2012-09-14
        相关资源
        最近更新 更多