C 中的类型转换不起作用(尝试将数字转换为 ASCII 表示的字母)



我目前正在设置一个Pangram检查程序,我正在尝试打印出用户未输入的字符(即缺少的字母以使其成为完整的Pangram)。但是,我正在尝试遍历数组的 26 个位置(字母表中的 26 个字母)并尝试键入转换丢失的位置,但是当我编译并运行程序并且当它进入这种不是 pangram 的情况时,它继续打印"字母未键入",但它实际上并没有写出丢失的字母!他们根本不出现!任何帮助将不胜感激!

与往常一样,这里有一个片段:

         for (i = 0; i < 26; ++i)
         {
             if (x[i] == 0)
             {
                 printf("%cn letter wasn't typed!n", (char)i);
 //Prints out "letter wasn't typed" without printing the actual letters that weren't typed by the user
                 getchar();
             }
         }

你可能想要

for (i = 0; i < 26; ++i)
{
      if (x[i] == 0)
      {
          char base = 'A'; // or 'a'
          printf("%cn letter wasn't typed!n", (char)(i + base));
          //Prints out "letter wasn't typed" without printing the actual letters that weren't typed by the user
          getchar();
      }
}

您正在打印 i 是代码的字母的较低范围。(ASCII,EBCDIC等)

这些信件可能是可打印的,也可能是不可打印的。您可能想打印罗马字母(从26猜测),因此您应该在打印前添加底座。

最新更新