这是我为大学的作业撰写的文章。我已经写了从用户那里得到10个整数的所有代码,并要求用户按1从列表中按升序显示奇数整数,或者从列表中按升序显示偶数整数。
好吧,我已经在程序中的main()函数之前声明并定义了Bubble Sorting函数,并在main()中稍后使用该函数按升序对偶数和奇数进行排序。但是,即使我在顶部声明了函数,我仍然会收到函数未在此范围内声明的错误。我尝试了所有可能的事情。请帮助我,我该怎么办?以下是我的代码
#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";
}
}
}
答案 0 :(得分:3)
一个简单的拼写错误:该函数被声明并实现为BuubleSort_Function
。
您尝试使用BubbleSort_Function
调用它。
编译器错误消息非常有用。要学会解释它们。
(最后,标准C ++要求您将main()
标记为返回int
- 如果缺少某个编译器,C ++编译器会将隐式return 0
插入main
。 - 特别是嵌入式系统的那些 - 放弃这个要求,但这是与标准的偏差。)。
答案 1 :(得分:1)
定义 - BuubleSort_Function
来电者 - BubbleSort_Function
答案 2 :(得分:1)
比较第5行和第7行与第64行,你会发现问题所在。只是一个简单的错字。