我写了一个代码在 C 中实现Julias Caesar密码学。
问题是我的 fgets()没有等待输入和跳到fputs()。
#include<stdio.h>
int main(){
int size;
printf("Enter the size of the string: ");
scanf("%d",&size);
int key;
printf("Enter the key: ");
scanf("%d",&key);
char a[size];
printf("Enter the string: ");
fgets(a,size,stdin);
for(int i=0;i<size;i++){
a[i]+=key;
}
fputs(a,stdout);
return 0;
}
我使用CodeBlocks 17.12来编译此C程序。
我的操作系统是Windows 7(32位)
答案 0 :(得分:1)
欢迎来到stackoverflow。
输入答案时,缓冲区的结尾(\ n)处有换行符。当fgets()读取您的输入时,它将读取换行符。您可以删除换行符,或使用正则表达式将其跳过,或在行上删除一次fgets(),以便按照this other answer中的建议再次使用scanf(),这可能会对您有所帮助。
而且,请记住在发表问题之前先在stackoverflow中进行搜索!