当循环时,当它操作“string”数据时,如何用一个特定的单词或字母中止它?

时间:2022-10-18 17:52:53

I'm just a beginner, so please don't judge too hard. I'm trying to understand, how I can stop a while loop, when it's working with strings? When it's just numbers, it's easy to tie it up with any non numerical symbol or any specific number (like 0); But whet it's a string, it different. Help me to understand please. So below is a simple code. At first I've tried to use as evaluation some characters like

我只是个初学者,所以请不要妄下结论。我试着理解,当它在处理字符串时,我如何停止一个while循环?当它只是数字时,很容易将它与任何非数字符号或任何特定数字(如0)联系起来;但是当它是弦时,它就不同了。请帮助我理解。下面是一个简单的代码。一开始我试着用一些像这样的字符作为评价

while(name1!='q'){
}

but it doesn't work. Then I wrote additional array with one specific string, and made a comparison:

但它不工作。然后我用一个特定的字符串写了另外一个数组,并做了比较:

char abort_name[4]={"stop"};
 short abort=strcmp(name1,abort_name);
    while (abort!=0) {

Take a look at my code. I understand that it probably doesn't work because of this non printed \0 symbol at the end of any string, and because I'm comparing 2 arrays one with 10 symbols and another only with 4, but how can I bypass it?

看看我的代码。我知道它可能不能工作,因为这个没有打印的\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;
}

upd: Now I found the mistake, thank you all for your explanation!

upd:现在我发现错误了,谢谢大家的解释!

2 个解决方案

#1


5  

 while (abort!=0)
 {
   ...
   short abort=strcmp(name1,abort_name);
 }

You are shadowing the abort variable defined in the main routine just above the while statement: those are 2 separate variables, so your condition never changes.

您正在将在主例程中定义的abort变量隐藏在while语句之上:它们是两个单独的变量,因此您的条件不变。

Change that to:

变化:

abort=strcmp(name1,abort_name);

to assign the main abort variable.

分配主中止变量。

(note that there's the same problem for check1)

(注意,check1也有相同的问题)

Also note that:

还要注意:

char abort_name[4]={"stop"};

defines an array of 4 strings. Not what you want, you need:

定义一个包含4个字符串的数组。不是你想要的,你需要的:

const char abort_name[]="stop";

or

const char *abort_name="stop";

(let the compiler compute the size for you BTW, 4 isn't enough because of nul-terminator)

(让编译器为您计算大小,顺便说一下,4并不足够,因为是空终止符)

#2


0  

Consider using fgets for input and a do/while loop to iterate until quit is entered.

考虑对输入使用fgets,并使用do/while循环迭代,直到输入quit。

#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


5  

 while (abort!=0)
 {
   ...
   short abort=strcmp(name1,abort_name);
 }

You are shadowing the abort variable defined in the main routine just above the while statement: those are 2 separate variables, so your condition never changes.

您正在将在主例程中定义的abort变量隐藏在while语句之上:它们是两个单独的变量,因此您的条件不变。

Change that to:

变化:

abort=strcmp(name1,abort_name);

to assign the main abort variable.

分配主中止变量。

(note that there's the same problem for check1)

(注意,check1也有相同的问题)

Also note that:

还要注意:

char abort_name[4]={"stop"};

defines an array of 4 strings. Not what you want, you need:

定义一个包含4个字符串的数组。不是你想要的,你需要的:

const char abort_name[]="stop";

or

const char *abort_name="stop";

(let the compiler compute the size for you BTW, 4 isn't enough because of nul-terminator)

(让编译器为您计算大小,顺便说一下,4并不足够,因为是空终止符)

#2


0  

Consider using fgets for input and a do/while loop to iterate until quit is entered.

考虑对输入使用fgets,并使用do/while循环迭代,直到输入quit。

#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;
}