2016-11-01 41 views
-2

我用C寫了一個簡單的程序,其功能是添加或刪除大小爲3的整數數組中的數字。所有成員的數組被初始化爲-1。在數組索引0處的值保持重置,即使它在它定義的塊的範圍內

用戶必須從菜單中選擇適當的選項,並可以插入,刪除,修改或查看數組的元素。以下是我在編譯程序後遵循的步驟。 。

  1. 選擇的選項1要添加的元素(在此,程序將首先尋找陣列中的負號,然後替換由用戶所提供的號碼的負數爲例如說:2.so這裏陣列的第一個元素應該被2覆蓋,即array[0] = 2

  2. 選擇選項4查看修改後的數組的元素。

令我驚訝的是,數組的第一個元素是0。I徹底檢查整個程序數組中的第1個要素的意外修改,但無法找到anything.I我無法弄清楚什麼是問題。

注意:代碼已經在linux平臺(OpenSUSE)上編譯過了。我在代碼中放了一些debug printf。此外,我發現當我更改數組"int array[3] = {-1,-1,-1};聲明的位置時「在變量的所有聲明的最後,節目擔任intended.But我還是想知道失敗

#include <stdio.h> 
unsigned char ShowMenu(); 

int main() 
{ 
    ShowMenu(); 
    return 0; 
} 

unsigned char ShowMenu() 
{ 
    int array[3] = {-1,-1,-1}; 
    unsigned char Operation = 0; 
    int Number = 0; 
    unsigned char Index = 0; 

do 
{ 
    printf("number atlocation 1 is %d\n",array[0]); 
    printf("Input Operation to be performed\n"); 
    printf("1.Add\n2.Remove\n3.Modify\n4.View all\n5.Exit\n"); 
    scanf("%d",&Operation); 
    printf("number atlocation 1 is %d\n",array[0]); 
     switch(Operation) 
     { 
    case 1: 
     printf("enter a positive number\n"); 
     scanf("%d",&Number); 
     //check if the number is positive 
     if(Number > 0) 
     { 
      for(Index = 0;Index < 3;Index++)//check for neg number positions 
      { 
     if (array[Index] < 0) 
     { 
      array[Index] = Number; 
      printf("Number added at location %d\n",Index); 
      break; 
     } 
     else 
     { 
      if (Index == 2) 
      { 
      printf("No more elements can be added\n"); 
      } 
      else 
      { 
      //do nothing 
      } 
     } 
      } 
     } 
     else 
     { 
     printf("the number is negative\n"); 
     } 
     Index = 0; 
     Number = 0; 
     printf("leaving case 1 :%d\n",array[0]); 

     break; 

    case 2: 

     printf("Input the number to remove\n"); 
     scanf("%d",&Number); 
     for(Index = 0;Index < 3;Index++)//check for neg number positions 
      { 
     if (array[Index] == Number) 
     { 
      array[Index] = -1; 
      printf("Number removed at location %d\n",Index); 
      break; 
     } 
     else 
     { 
      if(Index == 2) 
      { 
      printf("Number couldn be found\n"); 
      } 
      else 
      { 
      //do nothing 
      } 
     } 
      } 
     Index = 0; 
     Number= 0; 
     break; 

    case 3: 
     printf("Input the ponumber to find and replace\n"); 
     scanf("%d",&Number); 
     //this is not complete 
     break; 


    case 4: 
     printf("The numbers in the array are\n",); 
     for(Index = 0;Index < 3;Index++)//check for neg number positions 
      { 
     printf("Index %d : %d\n",Index,array[Index]); 
      } 
     Index = 0; 
     Number= 0; 
     break; 

    case 5: 
     printf("Operation complete\n"); 
     break; 

    default: 
     printf("Invalid input\n"); 
     } 

} 
    while(Operation != 5); 

    return Operation; 

} 
+1

您的代碼不編譯 –

+0

除了編譯錯誤,也有警告(至少對我來說,使用'gcc'時與'-Wall')。你有沒有考慮過這個警告? – Evert

回答

3

你有

unsigned char Operation = 0; 

,並使用

的原因
scanf("%d", &Operation); 

因此你需要

int Operation = 0; 

%d格式說明需要一個指向int,但您提供一個指向unsigned char

使用scanf當參數與格式字符串不匹配時會產生未定義的行爲。 如果您使用-Wall選項進行編譯,您的編譯器應該已經警告過您。

BTW:您發佈的代碼不編譯:

case 4: 
    printf("The numbers in the array are\n",); 
             ^problem here 
+0

感謝Michael的回覆。在發佈代碼時,我刪除了一些調試打印,並可能留下了流浪的逗號。 – stephen

+0

點擊綠色複選標記即可接受答案。 –

相關問題