Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cocoa/3.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
Arrays 如何打印整数数组_Arrays_Int_Ascii - Fatal编程技术网

Arrays 如何打印整数数组

Arrays 如何打印整数数组,arrays,int,ascii,Arrays,Int,Ascii,我有一个用于模拟数据包的结构,如下所示: typedef struct{ int source; int dest; int type; int port; char data[50]; }test; test packet[50]; printf("Enter a series of data numbers from 1-50: "); scanf("%c", &packet[i].data[i]); while (packet[i].data[i] >

我有一个用于模拟数据包的结构,如下所示:

typedef struct{
int source;
int dest;
int type;
int port;
char data[50];
}test;

test packet[50];
 printf("Enter a series of data numbers from 1-50: ");
    scanf("%c", &packet[i].data[i]);

    while (packet[i].data[i]  > 48 || packet[i].data[i] > 57)
        {
        printf("Data series needs to be between 1-50, try again: ");
        scanf("%c", &packet[i].data[i]);
        }
   printf("%c \n", packet[i].data[i]);
我试图将数据字段中的任何内容打印到屏幕上。我当前的代码如下所示:

typedef struct{
int source;
int dest;
int type;
int port;
char data[50];
}test;

test packet[50];
 printf("Enter a series of data numbers from 1-50: ");
    scanf("%c", &packet[i].data[i]);

    while (packet[i].data[i]  > 48 || packet[i].data[i] > 57)
        {
        printf("Data series needs to be between 1-50, try again: ");
        scanf("%c", &packet[i].data[i]);
        }
   printf("%c \n", packet[i].data[i]);
通过使用它,我已经能够编译它了——有时它会返回402018,有时是X,有时干脆跳过代码

有人能看出我哪里做错了吗?我想打印当前数据包实例中的整个字符数组,每次创建数据包时,数据包[I]都会递增

while语句用于确保输入的字符是一个数字,并且根据ASCII,数字在所述范围内

请对我温柔一点,我对这个很陌生


非常感谢。

对于该特定数据包的数组输入,必须使用不同的循环变量。 现在,您对数据包索引和字符数组索引使用了相同的变量,即i,我建议对单个字符输入使用getche函数,而不是scanf

并将您的while条件更改为:

 while (packet[i].data[x]  >= 48 || packet[i].data[x] <= 57)
其中x是附加循环变量

for(i=0;i<=49;i++)
    printf("Enter a series of data numbers from 1-50: ");
    for(x=0;x<=49;x++){
        packet[i].data[x] = getche();
         printf("%c \n", packet[i].data[x]);
        if(packet[i].data[x]  >= 48 || packet[i].data[x] <= 57)
            {
            printf("Data series needs to be between 1-50, try again: ");
            packet[i].data[x] = getche();

            }
else
{
  break;
}

    }

这在逻辑上是不正确的:

while (packet[i].data[i]  > 48 || packet[i].data[i] > 57)
                          ^^                        ^^

将第一个>更改为a,请显示控制i值的外部循环。此外,您还希望通过文件而不是交互方式输入测试数据-程序员不进行数据输入!:因此,中途测试是不可靠的;他可能指的是数据包[i]。数据[i]<57,但不确定……对不起,是的,这就是我对循环的意思。另外,我不接受单字符输入,一个字符串最多可以有50个字符。