错误:程序输出显示ascii字符

时间:2013-12-15 00:09:36

标签: 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;
                    }
                    }
 }

1 个答案:

答案 0 :(得分:0)

您正在为结构的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;