简体   繁体   English

程序不产生任何输出

[英]Program doesn't produce any output

I tried writing a code that gets up to 10 words, and if everything is a pangram it prints that it's a pangram.我尝试编写一个最多 10 个单词的代码,如果一切都是 pangram,它会打印出它是 pangram。 The thing is about this code is that even if the user enters 7 words that are a pangram, the for loop needs to stop and print that the sentence formed out of this words is indeed a pangram.这段代码的问题在于,即使用户输入 7 个单词是 pangram,for 循环也需要停止并打印出由这些单词组成的句子确实是 pangram。 I wrote the code and it compiles just fine, the problem is the exe file, I try to execute it and it doesn't work.我写了代码并且它编译得很好,问题是exe文件,我尝试执行它但它不起作用。 Can you guys help me?你们能帮帮我吗? my code:我的代码:

#include <stdio.h>
#include <string.h> 

#define MAX_WORDS 10
#define MAX_WORD_LENGTH 10
#define TEN 10

int main(void)
{
    int i = 0;
    int j = 0;
    int k = 0;
    int p = 0;
    int flag = 0;
    int counter = 0;

    char words[MAX_WORDS][MAX_WORD_LENGTH] = { 0 }; 

    for(i = 0; i < TEN; i++)
    {

        flag = 0;
        fgets(words[i], MAX_WORDS, stdin);
        //inputs the name
        if(words[i][strlen(words[i])-1] == '\n')
        {
            words[i][strlen(words[i])-1] = 0;
        }
        //makes sure there is no problem
        for(p = 0; p <= i; p++)
        {   
            for(k = 97; k < 123; k++)
            {
                for(j = 0; j < strlen(words[i]); j++)
                {
                    if(k == words[i][j])
                    {
                        flag = 1;
                    }

                }

            }       

        }
        if(flag)
        {
            counter++;
        }
        if(counter == 26)
        {
            i = 10;
            printf("It's a pangram?\nYes!");
        }
    }





    return 0;

}

Here is what missing in your code.这是您的代码中缺少的内容。 You forgot to add the break statement after setting the flag.您在设置标志后忘记添加break语句。 And incrementing the counter should be part of words iterating for loop.并且增加计数器应该是迭代循环的单词的一部分。

#include <stdio.h>
#include <string.h> 

#define MAX_WORDS 10
#define MAX_WORD_LENGTH 10
#define TEN 10

int main(void)
{
    int i = 0;
    int j = 0;
    int k = 0;
    int p = 0;
    int flag = 0;
    int counter = 0;

    char words[MAX_WORDS][MAX_WORD_LENGTH] = { 0 }; 

    for(i = 0; i < TEN; i++)
    {
        flag = 0;
        fgets(words[i], MAX_WORDS, stdin);
        //inputs the name
        if(words[i][strlen(words[i])-1] == '\n')
        {
            words[i][strlen(words[i])-1] = 0;
        }
        //makes sure there is no problem
        for(p = 0; p <= i; p++)
        {   
            for(k = 97; k < 123; k++)
            {
                for(j = 0; j < strlen(words[i]); j++)
                {
                    if(k == words[p][j])
                    {
                        flag = 1;
                        break;
                    }

                }
                if(flag)
                {
                    counter++;
                }
            }       

        }
        if(counter == 26)
        {
            i = 10;
            printf("It's a pangram?\nYes!");
        }
    }
    return 0;
}

As pointed out in the comments, you should try to debug the code yourself first.正如评论中所指出的,您应该先尝试自己调试代码。

For starters, I would stick a printf at the beginning of the for(i = 0; i < TEN; i++) loop to ensure the code is actually being executed.对于初学者,我会在for(i = 0; i < TEN; i++)循环的开始处贴上 printf 以确保代码实际正在执行。 Of course you can put it in every single other loop to get some idea where the code is going.当然,您可以将它放在每个其他循环中以了解代码的去向。

One mistake I'm noticing is in here: fgets(words[i], MAX_WORDS, stdin);我注意到的一个错误在这里: fgets(words[i], MAX_WORDS, stdin); . . The second parameter to fgets is expected to be the max word length and not the maximum number of words. fgets 的第二个参数应该是最大字长而不是最大字数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM