c - 错误: Program output shows ascii characters

标签 c structure

我的程序输出似乎有问题,当我选择选项 1 时,它在询问我想在数据包中添加哪些数据方面工作正常,我按预期输入数字,但是除了数据变量输出之外,它输出奇怪的 ASCII 字符而不是我最初输入的数字,因此任何帮助将不胜感激,谢谢。

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


struct packet{
    int source[4];
    int destination[4];
    int type[4];
    int port[4];
    char data[50];
};

void main ()
{

struct packet s[50]; //Array for structure input
int choice;
int customerCount = 0, ii = 0;

 while (customerCount <= 50){
                 printf("What would you like to do?\n");

                 printf("\t1) Add a packet.\n");
                 printf("\t2) s all packets.\n");
                 printf("\t3) Save packets.\n");
                 printf("\t4) Clear all packets.\n");
                 printf("\t5) Quit the programme.\n");
                 scanf("%i", &choice);

    switch (choice)
                    {
                        case 1: printf("\n****Adding a packet*****\n");
                                printf("Where is the packet from?\n");
                                scanf("%i", &s[customerCount].source);
                                printf("Where is the packet going?\n");
                                scanf("%i", &s[customerCount].destination);
                                printf("What type is the packet?\n");
                                scanf("%i", &s[customerCount].type);
                                printf("What is the packet's port?\n");
                                scanf("%i", &s[customerCount].port);
                                printf("Enter up to 50 characters of data.\n");
                                scanf("%s", s[customerCount].data);
                                customerCount++;
                                break;

                        case 2: printf("\nDisplaying Infomation\n");
                                for(ii = 0; ii < customerCount; ii++) {
                                printf("\nSource: %s", s[ii].source);
                                printf("\nDestination: %s", s[ii].source);
                                printf("\nType : %s", s[ii].type);
                                printf("\nPort : %s", s[ii].port);
                                printf("\nData: %s\n---\n", s[ii].data);
                                 }
                        break;


                        case 3: break;

                        case 4: break;

                        case 5: break;

                        default: printf("\nThis is not a valid choice, please choose again\n\n");
                                 break;
                    }
                    }
 }

最佳答案

您将输入添加到结构的 int 数组中,就像它们是单个 int 一样,并将它们打印出来,就像它们是字符串一样。将 int 数组设为单个 int 并使用 %i 作为 printf 格式说明符打印它们。

struct packet
{
    int source;
    int destination;
    int type;
    int port;
    char data[50];
};

case 2:
    printf("\nDisplaying Infomation\n");
    for(ii = 0; ii < customerCount; ii++)
    {
        printf("\nSource: %i", s[ii].source);
        printf("\nDestination: %i", s[ii].source);
        printf("\nType : %i", s[ii].type);
        printf("\nPort : %i", s[ii].port);
        printf("\nData: %s\n---\n", s[ii].data);
    }
    break;

关于c - 错误: Program output shows ascii characters,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20589769/

相关文章:

c++ - 如何为 native Windows(而不是 X Windows)编译 gtk+ 应用程序?

C - 跨多个文件返回结构指针时出错

mysql - 为学校日记构建mysql数据库

c - 不理解空指针/如何转换? (C语言编程)

c - 初始化结构体 C 中的变量矩阵

c - 指向结构的指针作为参数,函数内引用的成员产生奇怪的值

c - c99 的 ansi c 结构初始化

c - 初始化静态结构

c - 如何获取 C 程序启动以来经过的毫秒数?

Tab控件的客户区?