文号
t56257
阅读
4643
回复
9
收藏
4
学c语言遇见问题(小问题)
2013/04/21
IP:福建
将输入的字符中大写改为小写并加密,这个程序中,为什么去掉第6那一行就正常运行,加上后,输入p按回车直接得出(1111111Press any key to continue)
#include<stdio.h>
#include<ctype.h>
main()
{
char c;
while(getchar()!='p'); //为什么这一行去掉就正常
printf("1111111");
c=getchar();
while(c!='\n')
{
if(isalpha(c))
{
c=tolower(c);
c=(c-'a'+1)%26+'a';
}
putchar(c);
c=getchar();

}

}
13年8个月前IP:未同步
518408
1楼
getchar() 好像会吃回车符号的
引用
评论
13年8个月前IP:未同步
518412
2楼
回 1楼(celeron533) 的帖子
例如,语句while(getchar()!='p');printf("aabbcc");这个语句意思是,当s输入p按enter键时,程序才往下继续执行下一语句.所以我那个并没错。
引用
评论
13年8个月前IP:未同步
518415
3楼
回 1楼(celeron533) 的帖子
您是说,输入p按回车,这个回车会被第二个c=getcgar()中的getchar()吃掉?
引用
评论
13年8个月前IP:未同步
518418
4楼
getchar()似乎是从键盘缓冲区里面读取数据的,所以可能会读到重复的数据。
conio.h 下的getch()是逐字从键盘读取的

可能取决于不同的编译器
引用
评论
13年8个月前IP:未同步
518422
5楼
getchar()


__CRT_INLINE int __cdecl __MINGW_NOTHROW getchar (void)
{
  return (--stdin->_cnt >= 0)
    ?  (int) (unsigned char) *stdin->_ptr++
    : _filbuf (stdin);
}

从stdin流读取的。。。取第一位


getch()

* Low level console I/O functions. Pretty please try to use the ANSI
* standard ones if you are writing new code.
_CRTIMP int __cdecl __MINGW_NOTHROW    getch (void);
_CRTIMP int __cdecl __MINGW_NOTHROW    getche (void);
引用
评论
13年8个月前IP:未同步
518435
6楼
回 4楼(celeron533) 的帖子
啊,恍然大悟,当我输入pabcd按回车时,程序将abcd正确加密为bcde,而且做了个实验,果然后来的getchar会吃掉回车;
#include<stdio.h>
void main()
{
    char a,b;
    int e=1;
    a=getchar();
    if(e<2){
    b=getchar();
    putchar(b);
    putchar(b);}
}                                                                     实验步骤:输入z按回车
引用
评论
13年8个月前IP:未同步
520176
7楼
[s:274]
引用
评论
13年6个月前IP:未同步
543290
8楼
用
#include <stdlib.h>
fflush(stdin) ;
清除输入缓冲区就不怕这个问题了
把fflush放在输入语句之后
引用
评论
笔记
{{note.content}}
{{n.user.username}}
{{fromNow(n.toc)}} {{n.status === noteStatus.disabled ? "已屏蔽" : ""}} {{n.status === noteStatus.unknown ? "正在审核" : ""}} {{n.status === noteStatus.deleted ? '已删除' : ''}}
  • 编辑
  • 删除
  • {{n.status === 'disabled' ? "解除屏蔽" : "屏蔽" }}
我也是有底线的