這是我爲我的大學作業寫的作業。我已經編寫了所有需要用戶輸入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";
}
}
}
你沒有定義你的'main'來返回一個'int'? – AndyG
@AndyG是否需要將'main'定義爲'int'?我認爲這不是必需的,C/C++自動將其視爲'int' –
不,這是不正確的。你需要寫'int main'。編譯器自動執行的操作是在缺少返回0時插入一個隱式返回值。 – Bathsheba