简体   繁体   English

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

[英]C++ program wont compile with expected expression and function not allowed here

#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;
}

I am new to c++ and just working on an assignment for my class. 我是c ++的新手,只是为我的课程从事作业。 I thought the program I wrote would have worked but for the life of me I can not figure out why it will not compile. 我以为我写的程序可以用,但是对于我一生来说,我不知道为什么它不能编译。 I am sure I am missing something or not understanding how it should work completely. 我确定我丢失了某些东西,或者不了解它应该如何完全工作。 The errors I keep receiving are expected expression on line 26 by the 'else' statement and when I put the 'if' and 'else' statements in I started receiving function not allowed here errors. 我一直收到的错误是'else'语句在第26行的预期表达式,当我将'if'和'else'语句放入我开始接收此处不允许的函数错误时。 Any help would be greatly appreciated. 任何帮助将不胜感激。

In the if statement, you open the bracket { but you never close it. 在if语句中,打开括号{,但不要将其关闭。 Not sure if this is the problem but it should raise some issues. 不知道这是否是问题,但是应该引起一些问题。

    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";
    }

This is how it should look. 这就是它的外观。 Try it 试试吧

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM