【发布时间】:2017-08-31 17:40:01
【问题描述】:
好的,所以我正在尝试制作一个“读取字符”功能,它只接受大写或小写字母,而不是重音符号(我是法国人,所以它对法国键盘很重要)并将其返回为大写字母。 为此,我为 readcharacter 函数制作了另一个源文件:
char readCharacter()
{
char character;
do
{
printf("Please enter a letter without accents and hit 'enter': ");
character = getchar();
while(getchar() != '\n');
printf("\n");
character = toupper(character); //Upper-case if lower case
} while(((character < 65)||(character > 90)));
return character;
}
这是我的主要内容:
#include "main.h"
int main(void)
{
char MyLetter = readCharacter();
char MyLetter2 = readCharacter();
printf("%c\n%c\n", MyLetter, MyLetter2);
}
我的问题是,我得到了这个作为输出:
S
l
Please enter a letter without accents and hit 'enter':
Please enter a letter without accents and hit 'enter':
S
L
为什么我没有得到这个?
Please enter a letter without accents and hit 'enter':S
Please enter a letter without accents and hit 'enter':l
S
L
不知道这是否相关,但我的 IDE 是 eclipse,编译器是 MinGW(gcc?) 抱歉英语不好和/或编码不好...我刚刚开始编码... 谢谢!
【问题讨论】:
-
请注意
getchar实际上返回一个int。 -
至于你的问题,能否详细说明一下?你展示了你得到的输出,但是你期望的输出是什么?您是否尝试过在调试器中单步执行您的代码?也许您应该花点时间阅读 Eric Lippert 的 How to debug small programs。
-
编辑了预期的输出:)
-
是的,我知道,但实际上不是 int 也是 char 吗?感谢 ascii 表?
-
很好地阅读了为什么你应该尊重
getchar()的返回值类型:stackoverflow.com/questions/35356322/…。长话短说,EOF 会造成麻烦,在某些情况下,某些字符可能会被解释为 EOF。成功时getchar()等将字符值返回为 unsigned char,转换为 int。