C While loop,当它使用;“字符串”;数据,如何用特定的单词或字母中止它?

C While loop,当它使用;“字符串”;数据,如何用特定的单词或字母中止它?,c,arrays,string,while-loop,C,Arrays,String,While Loop,我只是个初学者,所以请不要太难判断。 我试图理解,当它使用字符串时,如何停止while循环?当它只是数字时,很容易将它与任何非数字符号或任何特定数字(如0)联系起来;但当它是一根绳子时,它就不同了。请帮我理解。 下面是一个简单的代码。首先,我尝试使用一些字符作为评估,例如 while(name1!='q'){ } 但它不起作用。 然后,我用一个特定的字符串编写了额外的数组,并进行了比较: char abort_name[4]={"stop"}; short abort=strcmp(name

我只是个初学者,所以请不要太难判断。 我试图理解,当它使用字符串时,如何停止while循环?当它只是数字时,很容易将它与任何非数字符号或任何特定数字(如0)联系起来;但当它是一根绳子时,它就不同了。请帮我理解。 下面是一个简单的代码。首先,我尝试使用一些字符作为评估,例如

while(name1!='q'){
}
但它不起作用。 然后,我用一个特定的字符串编写了额外的数组,并进行了比较:

char abort_name[4]={"stop"};
 short abort=strcmp(name1,abort_name);
    while (abort!=0) {
看看我的代码。我知道它可能不起作用,因为任何字符串末尾都有一个未打印的\0符号,而且我正在比较两个数组,一个是10个符号,另一个是4个符号,但是我如何绕过它呢

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main(void) {
    char name1[10];
    char abort_name[4]={"stop"}; //I'm trying to use a cancelling word, but it doesn't work

    printf("enter you name here /stop - to cancel/:   ");

    int check1=scanf("%s", name1);
    short abort=strcmp(name1,abort_name);
    while (abort!=0) {
        printf("value is: %d\r\n", check1);
        printf("\r\nname is: %s", name1);
        printf("\r\n\r\nenter you name here:   ");
        short check1=scanf("%s", name1);
        short abort=strcmp(name1,abort_name);

    }

    return 0;
}
#包括
#包括
#包括
内部主(空){
字符名称1[10];
char abort_name[4]={“stop”};//我试图使用一个取消字,但它不起作用
printf(“在此处输入您的姓名/停止-取消/:”;
int check1=scanf(“%s”,name1);
短中止=strcmp(名称1,中止名称);
while(中止!=0){
printf(“值为:%d\r\n”,选中1);
printf(“\r\n名称为:%s”,名称1);
printf(“\r\n\r\n在此处输入您的姓名:”;
short check1=scanf(“%s”,name1);
短中止=strcmp(名称1,中止名称);
}
返回0;
}
upd: 现在我发现了错误,谢谢大家的解释

 while (abort!=0)
 {
   ...
   short abort=strcmp(name1,abort_name);
 }
您正在对
while
语句上方的
main
例程中定义的
abort
变量进行跟踪:这是两个独立的变量,因此您的条件永远不会改变

改为:

abort=strcmp(name1,abort_name);
分配主
abort
变量

(注意,
检查1也有同样的问题)

还请注意:

char abort_name[4]={"stop"};
定义由4个字符串组成的数组。不是你想要的,你需要:

const char abort_name[]="stop";


(顺便说一句,让编译器为您计算大小,
4
由于nul终止符不够)

考虑使用fgets进行输入,并使用do/while循环进行迭代,直到输入
退出

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main(void) {
    char name1[100];
    int match = 0;

    do {
        printf("enter you name here ( or quit):   ");
        fflush ( stdout);//printf has no \n
        if ( fgets ( name1, sizeof name1, stdin)) {//get a line
            if ( 0 != ( match = strcmp ( name1, "quit\n"))) {//compare to quit\n
                printf("\r\nname is: %s", name1);
            }
        }
        else {
            fprintf ( stderr, "fgets problem\n");
            return 0;
        }
    } while ( match);

    return 0;
}
#包括
#包括
#包括
内部主(空){
字符名称1[100];
int匹配=0;
做{
printf(“在此处输入您的姓名(或退出):”;
fflush(stdout);//printf没有\n
if(fgets(name1,sizeof name1,stdin)){//获取一行
如果(0!=(match=strcmp(name1,“quit\n”)){//compare to quit\n
printf(“\r\n名称为:%s”,名称1);
}
}
否则{
fprintf(标准,“fgets问题”);
返回0;
}
}while(匹配);
返回0;
}
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main(void) {
    char name1[100];
    int match = 0;

    do {
        printf("enter you name here ( or quit):   ");
        fflush ( stdout);//printf has no \n
        if ( fgets ( name1, sizeof name1, stdin)) {//get a line
            if ( 0 != ( match = strcmp ( name1, "quit\n"))) {//compare to quit\n
                printf("\r\nname is: %s", name1);
            }
        }
        else {
            fprintf ( stderr, "fgets problem\n");
            return 0;
        }
    } while ( match);

    return 0;
}