Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/65.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C For循环不使用空终止符终止_C_Arrays_String - Fatal编程技术网

C For循环不使用空终止符终止

C For循环不使用空终止符终止,c,arrays,string,C,Arrays,String,我正在处理一个机器问题,任务是输入n个数组,然后我必须“合并排序” 但是,当我执行并输入单词时,它不会在第n个单词处停止,并且会要求我输入更多字符,即使我使用了空终止符 任何帮助都将不胜感激 #include <stdio.h> #include <string.h> #include "mp1_lib.h" int main() { int n; printf("\n"); printf("Input n: "); scan

我正在处理一个机器问题,任务是输入n个数组,然后我必须“合并排序”

但是,当我执行并输入单词时,它不会在第n个单词处停止,并且会要求我输入更多字符,即使我使用了空终止符

任何帮助都将不胜感激

#include <stdio.h>
#include <string.h>
#include "mp1_lib.h"

int main()
{
    int n;      
    printf("\n");
    printf("Input n: ");
    scanf("%d", &n);
    n += 1;
    char words[n][16];
    initialize(n, words);
    get_words(n, words);
    print_words(n, words);  
    printf("\n");
}

void initialize(int n, char words[][16])
{  
    for (int x=0; x < n; x++)
    {
        for(int y=0; y < 16; y++)
        {
            words[x][y] = ' ';
        }
    }
}

void get_words(int n, char words[][16])
{
    char c;
    char check = '0';
    for(int x = 0; x < n; x++)
    {
        int y = 0;
        while (check != '\0' && y < 16)
        {
            c = getchar();
            words[x][y] = c;
            check = c;
            y++;
        }
    }
}

void print_words(int n, char words[][16]) 
{
    for(int x=0; x < n; x++)
    {   
        for(int y=0; y < 16; y++)
        {   
            putchar(words[x][y]);
        }
    }
}
#包括
#包括
#包括“mp1_lib.h”
int main()
{
int n;
printf(“\n”);
printf(“输入n:”);
scanf(“%d”和“&n”);
n+=1;
字符字[n][16];
初始化(n,字);
获取单词(n,单词);
打印单词(n,单词);
printf(“\n”);
}
无效初始化(整数n,字符字[][16])
{  
对于(int x=0;x
由于键入空字符对最终用户来说可能很棘手,我建议您更改行以检查其他字符,如转义字符或ESC键,其值为27,更易于键入

while (check != 27 && y < 16)
while(检查!=27和&y<16)

您不能通过键盘输入与值
0
0x0
相同的
'\0'
-字符。因此,循环不会以这种方式终止

通常,当通过键盘获取输入时,您将通过按“回车”来终止单个字符串,回车由新行字符
'\n'
表示。 您可以进一步增强您的程序,以在输入“空”字(即字长为0)后停止扫描字。 请参阅以下程序,该程序利用
fgets
读取字符串,直到下一个新行字符+一个通用代码删除新行字符本身,然后:

void get_words(int n, char words[][16])
{
    for(int x = 0; x < n; x++)
    {
        if (fgets(words[x],16,stdin)){
            words[x][strcspn(words[x],"\n")] = '\0';  // remove new line (if any)
        }
        else{
            words[x][0] = '\0';  // othwise, set it to an empty string
            break;
        }
        if (words[x][0] == '\0')  // empty string entered (or end-of-file reached)
            break;
    }

}

void print_words(int n, char words[][16])
{
    for(int x=0; x < n && words[x][0] != '\0'; x++)  {
        printf("%s\n",words[x]);
    }
}

int main()
{
    int n;

    printf("\n");

    printf("Input n: ");
    scanf("%d\n", &n);
    n += 1;

    char words[n][16];

    get_words(n, words);

    print_words(n, words);

    printf("\n");
}

如何在键盘上输入空字符?您是否尝试过
while(检查!='*'&&y<16)
以查看*char输入是否将退出循环?是!有效:)不是“\0”思想调试器说什么?到底哪里出了问题?调试是编程的一个主要部分,要习惯它。不确定它是否相关,但您需要为函数编写原型,或者将
main
函数放在最后。@Bel:调试器不用于查找语法错误。它们允许你在程序执行时观察程序,一次一行地浏览程序,观察变量的值,等等。可能不可移植,但在Ubuntu上完全可以:使用Ctrl-@或Ctrl空格。但这是一个非常奇怪的输入约定,所以我同意使用其他东西的建议。
void initialize(int n, char words[][16])
{  
    for (int x=0; x < n; x++)
    {
        for(int y=0; y < 16; y++)
        {
            words[x][y] = ' ';
        }
        words[x][16-1] = '\0';
    }
}