【问题标题】:Typecasting in C not working (Attempting to convert number to ASCII represented letter)C 中的类型转换不起作用(尝试将数字转换为 ASCII 表示的字母)
【发布时间】:2015-01-19 03:49:55
【问题描述】:

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

一如既往,这里有一个sn-p:

         for (i = 0; i < 26; ++i)
         {
             if (x[i] == 0)
             {
                 printf("%c\n 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();
             }

         }

【问题讨论】:

    标签: c arrays for-loop printing char


    【解决方案1】:

    你可能想要

    for (i = 0; i < 26; ++i)
    {
          if (x[i] == 0)
          {
              char base = 'A'; // or 'a'
              printf("%c\n 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 猜测),因此您应该在打印之前添加碱基。

    【讨论】:

    • 似乎什么都没有出现:l(为了澄清基本的'a'打印,但没有别的。
    • @user2084266 删除getchar();后试试
    • 干杯它现在可以工作了!唯一的问题是,我将如何在一行中打印丢失的字母,而不是一个接一个地循环?
    • 将内部 printf 更改为 printf("%c ", (char)(i + base)); 并在循环后添加另一个 printf printf("letters weren't typed!\n"); 如果要放入 getchar(),请在此 printf 之后添加。
    • 你是很棒的伙伴!非常感谢!
    猜你喜欢
    • 2013-02-08
    • 1970-01-01
    • 2011-10-03
    • 2020-08-29
    • 1970-01-01
    • 2013-05-31
    • 1970-01-01
    • 2022-01-04
    • 1970-01-01
    相关资源
    最近更新 更多