2016-11-30 193 views
0

這是我爲我的大學作業寫的作業。我已經編寫了所有需要用戶輸入10個整數的代碼,並要求用戶按1以從列表或2升序顯示奇數整數,以便從列表中以升序顯示偶數整數。那麼,我已經在程序中的main()函數之前聲明和定義了Bubble Sorting函數,並且稍後在main()中使用該函數來按升序對偶數和奇數進行排序。但是,即使我已經在頂部聲明瞭該函數,但我仍然得到該函數未在此範圍內聲明的錯誤。我嘗試了所有可能的事情。請幫我,我該怎麼辦?以下是我的代碼C++接收錯誤,該函數未在此範圍內聲明

#include <iostream> 
#include <conio.h> 
using namespace std; 

void BuubleSort_Function(int [], int); 

void BuubleSort_Function(int arr[], int arrSize) 
{ 
    int extraMem; 

    for(int i = 0; i < arrSize; i++) 
    { 
     for(int arrIndex = 0; arrIndex < arrSize - 1; arrIndex++) 
     { 
      if(arr[arrIndex] > arr[arrIndex+1]) 
      { 
       extraMem = arr[arrIndex]; 
       arr[arrIndex] = arr[arrIndex+1]; 
       arr[arrIndex+1] = extraMem; 
      } 
     } 
    } 
} 

main() 
{ 
    int num[10], i, even[10], odd[10], inputOpt, totalEvens = 0, totalOdds = 0; 

    system("cls"); 

    cout << "Please enter 10 integers: " << endl; 

    for(i = 0; i < 10; i++) 
    { 
     cin >> num[i]; 
    } 

    cout << endl << endl << endl << "1. Show odd numbers in ascending order and their total numbers" << endl; 
    cout << "2. Show even numbers in ascending order and their total numbers" << endl; 

    do 
    { 
     cout << endl << "Enter 1 for the first option or 2 for the second option: "; 
     cin >> inputOpt; 

     if(inputOpt != 1 && inputOpt != 2) 
     { 
      cout << "Wrong Input! Please enter the correct input value"; 
     } 
    } 
    while(inputOpt != 1 && inputOpt != 2); 

    if(inputOpt == 1) 
    { 
     for(i = 0; i < 10; i++) 
     { 
      if(num[i] % 2 == 1) 
      { 
       odd[totalOdds] = num[i]; 
       totalOdds++; 
      } 
     } 

     BubbleSort_Function(odd,totalOdds); 
     cout << endl << "The total numbers of Odds Integers are " << totalOdds; 
     cout << endl << "The Integers arranged in Ascending Order:" << endl; 

     for(i = 0; i < totalOdds; i++) 
     { 
      cout << odd[i] << "\t"; 
     } 
    } 

    if(inputOpt == 2) 
    { 
     for(i = 0; i < 10; i++) 
     { 
      if(num[i] % 2 == 0) 
      { 
       even[totalEvens] = num[i]; 
       totalEvens++; 
      } 
     } 

     BubbleSort_Function(even,totalEvens); 
     cout << endl << "The total numbers of Odds Integers are " << totalEvens; 
     cout << endl << "The Integers arranged in Ascending Order:" << endl; 

     for(i = 0; i < totalEvens; i++) 
     { 
      cout << even[i] << "\t"; 
     } 
    } 
} 
+1

你沒有定義你的'main'來返回一個'int'? – AndyG

+0

@AndyG是否需要將'main'定義爲'int'?我認爲這不是必需的,C/C++自動將其視爲'int' –

+0

不,這是不正確的。你需要寫'int main'。編譯器自動執行的操作是在缺少返回0時插入一個隱式返回值。 – Bathsheba

回答

3

簡單的拼寫錯誤:函數聲明和實現爲BuubleSort_Function

您嘗試使用​​調用它。

編譯器錯誤消息非常有用。要學會解釋它們。

(最後,標準C++要求你標記main()爲返回的int - 一個用C++編譯器會插入一個隱含的return 0main如果缺少一些編譯器 - 特別是那些用於嵌入式系統 - 放棄這一要求,但是這是一個偏差來自標準)。

+0

我不明白,我是編程新手,能否請您詳細說明一下?我應該用不同的名稱而不是我用來聲明/定義它的名稱來調用函數嗎? –

+2

你難道沒有看到你拼錯了「Bubble」嗎? – Bathsheba

+0

哦,我現在明白了。非常感謝。大聲笑,我什至沒有注意到,我拼寫錯誤:D –

1

認定中 - BuubleSort_Function

來電 - BubbleSort_Function

1

比較5日線,並與第64行第7行,你會發現什麼is.Just一個簡單的拼寫錯誤的問題。

+0

Upvoted數線! (我假設你準確地做到了這一點)。 – Bathsheba

+0

我只是把代碼放入DevCpp中,並找出線條。 感謝upvoting! – WOET