【发布时间】:2016-02-06 22:55:58
【问题描述】:
基本上正如标题所说..当我的程序从控制台运行时,它会询问您是否要加密或解密..当我输入 e 或 E 时,它会创建一个新的空白行(直到我输入了某种文本),然后同时显示“输入文本”和“输入密钥”行..
所以,在控制台中它看起来像:
您想要 (E)ncrypt 还是 (D)ecrypt? e
asdf jkl;
输入您要加密的文本:输入用于加密的密钥:(用户输入)
然后程序退出..
//message to be encrypted
char text[250];
//word to use as the key
char key[50];
//stores the encrypted word
char encrypted[250];
char answer;
printf("Would you like to (E)ncrypt or (D)ecrypt? ");
scanf(" %c", &answer);
if(answer == 'e' || answer == 'E')
{
printf("Enter the text you want to encrypt : ");
fgets(text, 250, stdin);
printf("Enter a key to use for encryption : ");
fgets(key, 50, stdin);
printf("Encrypted text : ");
//code that encrypts the text here
}
那么,问题在于它完全跳过了 fgets 而不是等待/允许用户输入任何答案.. 为什么?
【问题讨论】:
-
scanf(" %c", &answer);在输入缓冲区中留下了一个newline,由fgets占用:摆脱它。 -
@Weather Vane 这很有意义(抱歉,第一周学习 c)。你如何摆脱输入缓冲区中留下的换行符??