【发布时间】:2018-03-20 13:30:58
【问题描述】:
我只是一个初学者,所以请不要过分评判。 我试图理解,当它使用字符串时,我如何停止一个while循环?当它只是数字时,很容易将它与任何非数字符号或任何特定数字(如 0)绑定;但是如果它是一个字符串,它就不同了。请帮助我理解。 所以下面是一个简单的代码。起初我尝试使用一些字符作为评估,例如
while(name1!='q'){
}
但它不起作用。 然后我用一个特定的字符串写了一个额外的数组,并进行了比较:
char abort_name[4]={"stop"};
short abort=strcmp(name1,abort_name);
while (abort!=0) {
看看我的代码。我知道它可能不起作用,因为在任何字符串的末尾都有这个未打印的 \0 符号,并且因为我正在比较 2 个数组,一个有 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;
}
更新: 现在我发现了错误,谢谢大家的解释!
【问题讨论】:
标签: c arrays string while-loop