C++ 程序不会使用此处不允许的预期表达式和函数进行编译

标签 c++

#include <iostream>
using namespace std;


const int lab8 = 10;
int labArray[lab8];
void promptUser(int [], int);
void sortArray(int [], int);
void showArray(const int[], int);
int searchArray(const int [], int, int value);
int x = 0;
int results = 0;

int main()
{

promptUser(labArray, lab8);
sortArray(labArray, lab8);
showArray(labArray, lab8);
cout << "Choose an integer you want to search from the array: " << endl;
cin >> x;
results = searchArray(labArray, lab8, x);
if (results == -1) {
    cout << "That number does not exist in the array. \n";
    else
    {
        cout << "The integer you searched for was for at element " <<  results;
        cout << " in the array. \n";
    }


}

 void promptUser(int numbers[], int size)
{
int index;
for (index = 0; index <= size - 1;index++ )
{
    cout << "Please enter ten numbers to fill the array " << endl
         << (index + 1) << ": ";
    cin >> numbers[index];
}

}
void sortArray(int array[], int size)
{
bool swap;
int temp;

do
{
    swap = false;
    for (int count = 0; count < (size -1); count++)
    {
        if (array[count] > array[count + 1])
        {
            temp = array[count];
            array[count] = array[count + 1];
            array[count + 1] = temp;
            swap = true;
        }
    }
} while (swap);
}
void showArray(const int array[], int size)
{
for (int count = 0; count < size; count++)
{
    cout << "The array you entered when sorted was: ";
    cout << array[count] << " ";
    cout << endl;
}
}

int searchArray(const int array[], int size, int value)
{
int first = 0,
last = size - 1,
middle,
position = - 1;
bool found = false;

while (!found && first <= last)
{
    middle = (first + last) / 2;
    if (array[middle] == value)
    {
        found = true;
        position = middle;
    }
    else if (array[middle] > value)
        last = middle - 1;
    else
        first = middle + 1;
}

return position;
}

我是 C++ 的新手,正在为我的类(class)做作业。我以为我写的程序可以工作,但对于我的生活我无法弄清楚为什么它不会编译。我确信我遗漏了一些东西或者不理解它应该如何完全工作。我不断收到的错误是“else”语句在第 26 行的预期表达式,当我将“if”和“else”语句放入时,我开始收到 function not allowed here 错误。任何帮助将不胜感激。

最佳答案

在 if 语句中,你打开括号 { 但你永远不会关闭它。不确定这是否是问题所在,但它应该会引发一些问题。

    if (results == -1) {
    cout << "That number does not exist in the array. \n";
     **}**
    else
    {
        cout << "The integer you searched for was for at element " <<  results;
        cout << " in the array. \n";
    }

它应该是这样的。试试吧

关于C++ 程序不会使用此处不允许的预期表达式和函数进行编译,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33446430/

相关文章:

c++ 公历和儒略历继承

c++ - 基类方法的接口(interface)

c++ - 拦截传入的短信

c++ - 客户端服务器编程缓冲区内容不正确

c++ - vector 超出特定机器的范围?

c++ - OpenCV 矩阵创建

c++ - 如何从 C++ 中的 tensorflow 变量获取数据

c++ - 遍历 vector 并删除某些元素的正确方法是什么

C++11 如何扩展类模板,如 std::packaged_task

c++ - 如何将提取的栅格信息转换为opencv库可处理的格式