Can anyone tell me why I'm getting build errors on the actual function heading and the line under it (**) saying "type bool is unexpected" and that I'm missing a ; before {? I've looked and can't find anything.

Also, I get this error but there is no line 80 and all my braces match up (I think):
pa2-q1.cpp(80) : fatal error C1075: end of file found before the left brace '{' at '.\pa2-q1.cpp(17)' was matched


Any help is appreciated! :confused:

#include <iostream>
#include <conio.h>

using namespace std;

const int MAX_ITEMS=10;

bool Bsearch (int , int , int , int, int&);

int main ()
{
    int chart[MAX_ITEMS]= {2,6,9,14,23,65,92,96,99,100};
    int find, start, end, tms;
    tms=0;
    bool cont=1;

    cout<<"This programs looks for a number in a preset list."<<endl;

    do{
        cout<<"Enter a number (no decimals), starting location for search, and ending location "<<
        "(start & end between 0 & "<<MAX_ITEMS-1<<" , inclusive)."<<endl;
        cin>>find>>start>>end;

        if ((start<0||start>MAX_ITEMS-1) ||(end<0||end>MAX_ITEMS-1))
        {
            cout<<"Either the starting or ending location are outside of parameters."<<endl;
            break;
        }
        else if (start>=end)
        {
            cout<<"Starting location must be less than ending location."<<endl;
            break;
        }
        else
        {
            //bool found=0
            if (Bsearch(chart[MAX_ITEMS],find,start,end,tms))
                cout<<"Your number was found in the list after "<<tms<<" passes."<<endl;
            else
                cout<<"Your number was not found in the list after "<<tms<<" passes."<<endl;

            cout<<"Search for another number? Enter 1 for Yes or 0 for No:  ";
            cin>>cont;
        } while (cont);

        _getch();
        return 0;
    }


**bool Bsearch (int chart[], int find, int start, int end, int& tms)
**{
    if (start>end)
        return false;
    else
    {
        int mid=(start+end)/2;
        if (chart[mid]==find)
            return true;
        else if (find<chart[mid])
        {
            tms++;
            return Bsearch (chart[MAX_ITEMS], find, start, mid-1, tms);
        }
        else
        {
            tms++;
            return Bsearch (chart[MAX_ITEMS], find, mid+1, end, tms);
        }
    }
}

Can anyone tell me why I'm getting build errors on the actual function heading and the line under it (**) saying "type bool is unexpected" and that I'm missing a ; before {? I've looked and can't find anything.

Also, I get this error but there is no line 80 and all my braces match up (I think):
pa2-q1.cpp(80) : fatal error C1075: end of file found before the left brace '{' at '.\pa2-q1.cpp(17)' was matched


Any help is appreciated! :confused:

bool Bsearch (int , int , int , int, int&);

bool Bsearch (int chart[], int find, int start, int end, int& tms)

remove the & after the last int in the bool Bsearh fuction

else
{
//bool found=0
if (Bsearch(chart[MAX_ITEMS],find,start,end,tms))
cout<<"Your number was found in the list after "<<tms<<" passes."<<endl;
else
cout<<"Your number was not found in the list after "<<tms<<" passes."<<endl;

cout<<"Search for another number? Enter 1 for Yes or 0 for No: ";
cin>>cont;
} while (cont);

_getch();
return 0;
}

You are missing an ending bracket for the first else. The one right after while belongs to the do/while loop.

remove the & after the last int in the bool Bsearh fuction

Aia, I tried that but it didn't help. :sad:

here is the exact text of the bool and brace errors also:

.\pa2-q1.cpp(59) : error C2062: type 'bool' unexpected
.\pa2-q1.cpp(60) : error C2143: syntax error : missing ';' before '{'

>remove the & after the last int in the bool Bsearh fuction
Why?

2 things I noticed about your code:

  • Your function implementation differs from the prototype in the sense that the prototype doesn't accept an int array for the first parameter. Change it to something like this:
    bool Bsearch (int[], int , int , int, int&);
  • This is NOT how you pass an array: chart[MAX_ITEMS] . Just omit the [] and use chart .

Thank you...thank you!!! I don't know why I can't seem to get it straight on how to pass/declare arrays in a function.


.\pa2-q1.cpp(59) : error C2062: type 'bool' unexpected
.\pa2-q1.cpp(60) : error C2143: syntax error : missing ';' before '{'

The first line I think is because in the prototype you have the first
parameter as an int and in the actual function is a int array.

I always copy my function and use it as a prototype, just adding the ; and the end.

The second error I can not see any missing ; any where. Are you sure that your posted copy is the same you are compiling. In my compiler after fixing the little mispells I don't have those errors.
I have other errors, but will get there... :lol:


What are you trying to do passing as a parameter int& tms?

>What are you trying to do passint as a parameter int& tms?
It's a counter. If it wasn't passed as a reference, it wouldn't update because the variable would be a copy of the original.

>What are you trying to do passint as a parameter int& tms?
It's a counter. If it wasn't passed as a reference, it wouldn't update because the variable would be a copy of the original.

Mister Joeprogrammer,
Am I correct, then, in assuming that this way of reference in a function is something different for C++ than for C? I know you can not do it in C, but my C++ is very limited.

>Am I correct, then, in assuming that this way of reference in a function is something different for C++ than for
>C?
Yes. It's more convenient than a pointer, and much safer, because you don't have to worry about accidentally mispointing it.

Yes. It's more convenient than a pointer, and much safer, because you don't have to worry about accidentally mispointing it.

Thank you. Much appreciated.

Oh and btw, just call him Joey, he really likes it. :D

You WISH! (why oh why did I ever post in that thread...)

why oh why did I ever post in that thread...

Don't bit yourself up, you couldn't help it.;)

You WISH! (why oh why did I ever post in that thread...)

Oh come on cheer up, you got yourself a really good nickname...:mrgreen:

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.