为什么我的代码没有执行?

时间:2023-01-03 20:20:08

Currently learning C - and I have no clue where I'm going wrong in this code:

目前正在学习C - 我不知道我在这段代码中出错了:

#include <stdio.h>

int main()
{
    char alphabet[20];
    int i;

    for (int i = 0; i > 20; i++)
    {
        printf("Enter in a letter:\n");
        scanf("%s", alphabet[i]);
        if (alphabet[i] == alphabet[i+1])
        {
            printf("Duplicate Letters");
        };
    return 0;
    }

}

The program that I am asked to make for class — I'm required to create a 1D array, add validation for alphabetical letters and duplicate letters, as well as creating a function for sorting the letters and specifying the number of times each letter was put in.

我被要求为课程制作的程序 - 我需要创建一维数组,添加字母和重复字母的验证,以及创建一个函数来排序字母并指定每个字母的放置次数在。

As much as I've been able to attempt coding is:

尽管我已经能够尝试编码是:

  • Create a 1D array to read 20 alphabetical letters
  • 创建一维数组以读取20个字母
  • Add validation for duplicate letters and printf 'Duplicate Letters' but every time I try, the program terminates at 'Enter in a letter:' or it won't execute.
  • 添加重复字母和printf'重复字母'的验证,但每次尝试时,程序都会以“输入字母:”结束,否则将无法执行。

Where did I go wrong?

我哪里做错了?

For background: I work mainly on Windows 7 because that's what the school has — using MinGW as my compiler — but for working at home I use MacOS using Terminal as the compiler.

对于背景:我主要在Windows 7上工作,因为这就是学校所使用的 - 使用MinGW作为我的编译器 - 但是在家里工作我使用MacOS使用Terminal作为编译器。

1 个解决方案

#1


4  

for (int i = 0; i > 20; i++)

You're telling the computer here to initialize i to 0, and then, while i is greater than 20, do the loop. However, since i starts at 0, it will never be greater than 20.

你在这里告诉计算机将i初始化为0,然后,当我大于20时,进行循环。但是,因为我从0开始,它永远不会超过20。

for (int i = 0; i < 20; i++)

And, yes, as comments have pointed out, your use of scanf is incorrect. Lacking a better C reference for it, check out http://www.cplusplus.com/reference/cstdio/scanf/ for descriptions of its arguments.

并且,是的,正如评论所指出的,您对scanf的使用是不正确的。缺少更好的C参考,请查看http://www.cplusplus.com/reference/cstdio/scanf/以获取其参数的描述。

#1


4  

for (int i = 0; i > 20; i++)

You're telling the computer here to initialize i to 0, and then, while i is greater than 20, do the loop. However, since i starts at 0, it will never be greater than 20.

你在这里告诉计算机将i初始化为0,然后,当我大于20时,进行循环。但是,因为我从0开始,它永远不会超过20。

for (int i = 0; i < 20; i++)

And, yes, as comments have pointed out, your use of scanf is incorrect. Lacking a better C reference for it, check out http://www.cplusplus.com/reference/cstdio/scanf/ for descriptions of its arguments.

并且,是的,正如评论所指出的,您对scanf的使用是不正确的。缺少更好的C参考,请查看http://www.cplusplus.com/reference/cstdio/scanf/以获取其参数的描述。