2013-12-15 44 views
0

我似乎有一個與我的程序的輸出有關的問題,當我選擇選項1它工作正常在問我什麼數據我想添加與數據包,我輸入數字數據,因爲我應該但除了數據變量輸出它輸出奇怪的ASCII字符,而不是我最初輸入的數字,所以任何幫助,將不勝感激謝謝。錯誤:程序輸出顯示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; 
        } 
        } 
} 
+0

爲什麼前四個字段中有4個int的數組? – ericbn

回答

0

要添加輸入到結構的int數組作爲雖然他們單個int進行打印,好像他們是字符串。製作int陣列單個int s並使用%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;