C 如何让程序循环直到输入字符?

C 如何让程序循环直到输入字符?,c,loops,while-loop,char,C,Loops,While Loop,Char,我正试图实现的是:你输入你的名字,得到一个像“你的名字是”这样的响应,如果你输入一个数字,你会得到一个像“无效输入”这样的响应,它会将你返回到“输入你的名字”部分 #include <stdio.h> char i[20]; int result; int main() { void findi(); // im trying to loop it back here if a number is entered instead of a character printf(&quo

我正试图实现的是:你输入你的名字,得到一个像“你的名字是”这样的响应,如果你输入一个数字,你会得到一个像“无效输入”这样的响应,它会将你返回到“输入你的名字”部分

#include <stdio.h>
char i[20];
int result;
int main()

{
void findi(); // im trying to loop it back here if a number is entered instead of a character
printf("Enter your name\n");
result = scanf("%s", &i);
while(getchar() != '\n'){ //dont know how to make it work without the '!'
    if(result = '%s'){
        printf("Your name is: %s", &i); 
        return 0;
    }
    else{
        printf("Invalid input"); //doesnt work 
        findi();
    }
   }
}
//program just ends after a character is entered instead of continuing 
#包括
chari[20];
int结果;
int main()
{
void findi();//如果输入的是数字而不是字符,我会尝试在这里循环
printf(“输入您的姓名”);
结果=扫描频率(“%s”、&i);
while(getchar()!='\n'){//不知道没有'!'如何使它工作
如果(结果=“%s”){
printf(“您的名字是:%s”,&i);
返回0;
}
否则{
printf(“无效输入”);//不起作用
findi();
}
}
}
//程序在输入字符后结束,而不是继续
  • &i
    char(*)[20]
    )用于
    %s
    (预期
    char*
    )调用未定义的行为
  • 条件
    result='%s'
    (将实现定义的值分配给
    result
    ,而不检查其值)看起来很奇怪
  • main()
    调用
    findi()
试试这个:

#include <stdio.h>
#include <ctype.h>

int main(void)
{
    char i[20];
    printf("Enter your name\n");
    /* an infinite loop (loop until return or break) */
    for (;;) {
        int number_exists = 0, j;
        /* limit length to read and check the result*/
        if (scanf("%19s", i) != 1) {
            printf("read error\n");
            return 1;
        }
        /* check if a number is entered */
        for(j = 0; i[j] != '\0'; j++) {
            if (isdigit((unsigned char)i[j])) {
                number_exists = 1;
                break;
            }
        }
        /* see the result */
        if (number_exists) {
            /* one or more number is entered */
            printf("Invalid input\n");
        } else {
            /* no number is entered : exit from the loop */
            printf("Your name is: %s\n", i); 
            break;
        }
    }
    return 0;
}
#包括
#包括
内部主(空)
{
chari[20];
printf(“输入您的姓名”);
/*无限循环(循环直到返回或中断)*/
对于(;;){
整数_=0,j;
/*限制读取和检查结果的长度*/
如果(扫描频率(“%19s”,i)!=1){
printf(“读取错误\n”);
返回1;
}
/*检查是否输入了数字*/
对于(j=0;i[j]!='\0';j++){
if(isdigit((无符号字符)i[j])){
数量_=1;
打破
}
}
/*看到结果了吗*/
如果(数字_存在){
/*输入一个或多个数字*/
printf(“无效输入\n”);
}否则{
/*未输入编号:退出循环*/
printf(“您的名字是:%s\n”,i);
打破
}
}
返回0;
}

while(getchar()!='\n')
可以写成
while(getchar()-'\n')
,如果您想避免
出于某种原因。
结果=“%s”
完全错误谢谢你,我从中学到了很多