Can anyone help? here is my code: passing an array base list to a function and search for an item.

#include <iostream>
#include <fstream>
const int MAX_LENGTH = 10;
using namespace std;
 
class IntList
{
      public:
             IntList();
             void GetList(ifstream &data);
             void PrintList();
             int LengthIs();
             
             
      private:
              
              int length;
              int values[MAX_LENGTH];
};
bool BinarySearch(IntList [], int , int , int ,bool);
int main()
{
    ifstream inData;
    IntList info;
    int item,fromLoc,toLoc,istep=0;
    bool found = true;
    char answer;
    
    string msg = "Number out of range! ";
    inData.open("int.dat");
    do{
        cout << fixed << showpoint;
        cout << "Enter value to be searched for: "<< endl;
        cin  >> item;
        cout << "Enter starting location in 0-9 range: "<< endl;
        cin  >> fromLoc;
        cout << "Enter end location in 0-9 range: "<< endl;
        cin  >> toLoc;
        try{
             while(istep != MAX_LENGTH)
             {
                 if((fromLoc <= 9) && (fromLoc >=0)&&(toLoc <= 9) && (toLoc >=0) && (fromLoc<toLoc))
                 {   cout << "Correct range! Do the search." <<endl;
                     BinarySearch(info,item,fromLoc,toLoc,found);
                     istep++;
                     cout << "This is the " << istep <<"th iteration, do you want to continue? <Y/N> ";
                     cin >> answer;
                     if(answer == 'n')
                     {   cout << "Search abrted!";
                        break;
                     }
                 }
                 else
                       throw msg;
             
             }
        }
        catch(string message)
        {
           cout << message << "Search aborted!";
           
        }
        break;
         
    }while(found = false);
    
    
    
    cout << endl;
    cout << "*******************************************************" << endl;
  
    info.GetList(inData);
    cout << "The list is : ";
    info.PrintList();
    cout << endl;
    cout << "Length is  " << info.LengthIs() << endl;
    cout << "*******************************************************" << endl;
    system("pause");
    return 0;
    
}
IntList::IntList()
{
  length =0;
}
void IntList::GetList(ifstream &data)
{
     int value;
     
     data >> value;
     
     while(data)
     {
       for(int i=0;i<MAX_LENGTH;i++)
       {
          data >> value;
          values[i] = value;
          length++;
       }
       cout << "List has been populated." << endl;
     }
          
}
void IntList::PrintList()
{
     int index;
     for(index=0;index<MAX_LENGTH;index++)
     {
       cout << values[index]<< " ";
     }
}
       
             
int IntList::LengthIs()
{
     return length;
}
bool BinarySearch(IntList info[], int item, int first, int last,bool found)
{
     int mid;
     found = false;
     int num = 1;
     while(first <= last)
     {
       mid = (first + last) / 2;
       if(item > info[mid])
          first = mid +1;
       else if(item < info[mid])
            last = mid - 1;
       else
       
           return found = true;
          
         
           
     }
     return found = false;
}

My compilation error:
50 D: cannot convert `IntList' to `IntList*' for argument `1' to `bool BinarySearch(IntList*, int, int, int, bool)'
D:\In function `bool BinarySearch(IntList*, int, int, int, bool)':
137 D:\ no match for 'operator>' in 'item > *((+(((unsigned int)mid) * 44u)) + info)'

Recommended Answers

All 3 Replies

>IntList info;
Make this into an array, and the compiler won't complain about converting from IntList to IntList*.

I can't check over the rest of your code right now, but it looks alright at first glance.

#include <iostream>
#include <fstream>
const int MAX_LENGTH = 10;
using namespace std;
 
class IntList
{
      public:
             IntList();
             void GetList(ifstream &data);
             void PrintList();
             int LengthIs();
             
             
      private:
              
              int length;
              int values[MAX_LENGTH];
};
bool BinarySearch(IntList *, int , int , int ,bool);
int main()
{
    ifstream inData;
    IntList values;
    int item,fromLoc,toLoc,istep=0;
    bool found = true;
    char answer;
    
    string msg = "Number out of range! ";
    inData.open("int.dat");
    do{
        cout << fixed << showpoint;
        cout << "Enter value to be searched for: "<< endl;
        cin  >> item;
        cout << "Enter starting location in 0-9 range: "<< endl;
        cin  >> fromLoc;
        cout << "Enter end location in 0-9 range: "<< endl;
        cin  >> toLoc;
        try{
             while(istep != MAX_LENGTH)
             {
                 if((fromLoc <= 9) && (fromLoc >=0)&&(toLoc <= 9) && (toLoc >=0) && (fromLoc<toLoc))
                 {   cout << "Correct range! Do the search." <<endl;
                     BinarySearch(&values,item,fromLoc,toLoc,found);
                     istep++;
                     cout << "This is the " << istep <<"th iteration, do you want to continue? <Y/N> ";
                     cin >> answer;
                     if(answer == 'n')
                     {   cout << "Search abrted!";
                         cout << "There were " << istep << " iterations.";
                        break;
                     }
                 }
                 else
                       throw msg;
             
             }
        }
        catch(string message)
        {
           cout << message << "Search aborted!";
           
        }
        break;
         
    }while(found = false);
    
    
    
    cout << endl;
    cout << "*******************************************************" << endl;
  
    values.GetList(inData);
    cout << "The list is : ";
    values.PrintList();
    cout << endl;
    cout << "Length is  " << values.LengthIs() << endl;
    cout << "*******************************************************" << endl;
    system("pause");
    return 0;
    
}
IntList::IntList()
{
  length =0;
}
void IntList::GetList(ifstream &data)
{
     int value;
     
     data >> value;
     
     while(data)
     {
       for(int i=0;i<MAX_LENGTH;i++)
       {
          data >> value;
          values[i] = value;
          length++;
       }
       cout << "List has been populated." << endl;
     }
          
}
void IntList::PrintList()
{
     int index;
     for(index=0;index<MAX_LENGTH;index++)
     {
       cout << values[index]<< " ";
     }
}
 
          
             
int IntList::LengthIs()
{
     return length;
}
bool BinarySearch(const IntList &values, int item, int first, int last,bool found)
{
    
     found = false;
     int num = 1;
     while(first <= last)
     {
       int mid = (first + last) / 2;
       
       if(item > values[mid])
          first = mid +1;
       else if(item < values[mid])
            last = mid - 1;
       else
       
           return found = true;
          
         
           
     }
    
     return found = false;
}

Error :140 C:\ no match for 'operator[]' in 'values[mid]'

Please, i am going nuts with this stuff.
Someone else mentioned that I have to define the operatot; how and should i do that.

Where was values defined as an array?

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.