注册 登录
编程论坛 C语言论坛

为何这段代码会不断要你输入?即使按了回车后也不会停止?

b1592187525 发布于 2019-08-14 17:04, 2891 次点击
为何这段代码会不断要你输入?即使按了回车后也不会停止?
程序代码:
#include<stdio.h>
#define a 256
int main(void){
    char ch[a];
    int index;
    index=0;
    scanf("%c",&ch[index]);  
    while(ch[index]!='\n'){
        scanf("%c",&ch[index]);   
        index++;   
    }
    return 0;
7 回复
#2
rjsp2019-08-14 18:28
将 index++; 移到上一行之前试试,
#3
b15921875252019-08-14 21:37
回复 2楼 rjsp
将index++;移到上一行就可以了,
但不明白如果不移到上一行的话,为什么就要你不停的输入?
#4
自学的数学2019-08-14 21:49
程序代码:
#include<stdio.h>
#define a 256
int main(void){
    char ch[a];
    int index;
    index=0;
    scanf("%c",&ch[index]);  
    while(ch[index]!='\n'){
        scanf("%c",&ch[index]);   
       }
    return 0;
     
}
#5
b15921875252019-08-14 22:40
回复 4楼 自学的数学
如果加上index++;这句。为何按了回车后也不结束循环?
程序代码:
#include<stdio.h>
#define a 256
int main(void){
    char ch[a];
    int index;
    index=0;
    scanf("%c",&ch[index]);  
    while(ch[index]!='\n'){
        scanf("%c",&ch[index]);   
        index++;   
    }
    return 0;
#6
forever742019-08-14 23:35
你自己得想啊,输入放哪儿了,接着又是判断的谁?
#7
吹水佬2019-08-15 05:03
    int index=0;
    while(scanf("%c",&ch[index]) && ch[index]!='\n')
    {
        index++;
    }
#8
Iml梁2019-08-15 15:33
你这个程序滴二个scanf第一次是执行不了的。
1