Help

I can't seem to figure out how to a) get the program to close when I select 8 and b) it keeps giving me the error message "error: invalid conversion from ‘element (*)() {aka int (*)()}’ to ‘int’ [-fpermissive]" Please help!!

#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;

const int MLS=50;
typedef int element;
const element SENTINEL=(-1);

class AList{
private:
element items[MLS];
element target;
bool found;
int pos;
int size;
public:
void Read();
void RandomNumber();
void BubbleSort();
void InsertionSort();
void SelectionSort();
void LinearSearch(element target, bool &found, int &pos);
void BinarySearch(element target, bool &found, int &pos);
void PrintList();
void Swap(int pos1, int pos2);


};

int main(){
AList MyList;
int pos;
bool found;
element target;
int userval;
int printmenu();
userval=printmenu();
if (userval ==8)
std::exit;
else
;
while (userval != 8){
switch (userval){
case 1:
MyList.Read();
break;
case 2:
MyList.RandomNumber();
break;
case 3:
MyList.BubbleSort();
break;
case 4:
MyList.InsertionSort();
break;
case 5:
MyList.SelectionSort();
break;
case 6:
MyList.LinearSearch(target, found, pos);
break;
case 7:
MyList.BinarySearch(target, found, pos);
break;
default:
cout<<"invalid input"<<endl;
}
MyList.PrintList();
printmenu();
}
}

int printmenu(){
int userval;
cout<<"Please select an option from the menu:"<<endl;
cout<<"1. Reset the current list from the keyboard"<<endl;
cout<<"2. Reset the current list using randomly generated elements";
cout<<endl;
cout<<"3. Perform Bubble Sort on the current list"<<endl;
cout<<"4. Perform Insertion Sort on the current list"<<endl;
cout<<"5. Perform Selection Sort on the current list"<<endl;
cout<<"6. Perform Linear Search on the current list"<<endl;
cout<<"7. Perform Binary Search on the current list"<<endl;
cout<<"8. Quit the program"<<endl;
cout<<endl;
cout<<"enter an nubber between 1 and 8: ";
cin>>userval;
return userval;
}

element read_element(){
element userval;
cin>>userval;
while(!cin.good()){
cin.clear();
cin.ignore(80,'\n');
cout<<"invalid type, Should be a whole number, try again: ";
cin>>userval;
}
return userval;
}

void AList::Swap(int pos1, int pos2){
//Pre: the Native Object is valid and 0<=pos1, 0<=pos2
//Post: the Native object AList is unchanged except
//the elements in position1 and position2 have bee exchanged
temp=items[pos1];
items[pos1]=items[pos2];
items[pos2]=temp;
}

void AList::Read(){
//pre: None
//Post: The Native object is valid
// using data input by the user.
element userval;
size =0;
int count;
count=0;
cout<<"enter elements, "<<SENTINEL<<" to stop:";
userval=read_element();
while((userval!=SENTINEL)&&(size!=MLS)){
count++;
items[size]=userval;
size++;
if(size<MLS)
userval=read_element;
else
cout<<"list is now full."<<endl;
}
cout<<"Actual passes= "<<count<<endl;
PrintList();
}

void AList::BubbleSort(){
//Pre:The Native Object AList is valid
//Post: the Native Object is unchanged
//except its elements are now in ascending order
int count;
count=0;
for(int i=0; i<size-1; i++)
for(int j=0; j<size-1; j++)
if(items[j]>items[j+1]){
count++;
Swap(j, j+1);
}
else
;
cout<<"Actual passes = "<<count<<endl;
}

void AList::InsertionSort(){
//Pre: the Native Object AList is valid.
//Post: the Native Object AList is unchanged except elements
// are now in ascending order.
int j;
int count;
count=0;
bool done;
for (int i=1; i<size; i++){
j=i;
done=false;
while((j>=1)&&(!done))
if (items[j]<items[j-1]){
Swap(j,j-1);
count++;
j--;
}
else
;
}
cout<<"Actual Passes: "<<count<<endl;
}

void AList::SelectionSort(){
//Pre: The Native Object AList is valid
//Post: the Native Object AList is unchanged except
// elements are noe in ascending order.
int maxpos;
int count;
count=0;
for(int i=(size-1); i>0; i--){
maxpos=0;
for(int j=1; j<=i; j++)
if (items[j]> items[maxpos])
maxpos=j;
else
;
Swap(maxpos, i);
count++;
}
cout<<"Actual Passes: "<<count<<endl;
}

void AList::LinearSearch(element target, bool &found, int &pos){
//Pre: Native Object AList is valid and the target is valid
//Post: if the target exists on the Native Object AList, then found
// will be true and the position will be a location of a target
// on the list. Otherwise, found will be false and position
// undefined.
found = false;
pos = 0;
int count;
count=0;
while((!found)&&(pos<size))
if(target==items[pos])
found=true;
else
pos++;
count++;
}
void AList::BinarySearch(element target, bool &found, int &pos){
//Pre: the Native Object AList is valid and the target is valid and
// the onject is in ascending order.
//Post:If the target exists on the Native Object AList, then found
// will be true and the position will be the location of a target
// on the list. Otherwise, found will be false and the position
// undefined.
int low;
int high;
int mid;
int count;
count=0;
found = false;
low=0;
high=size-1;
while((!found)&&(low<=high)){
mid=(low+high)/2;
if(target==items[mid]){
found=true;
pos= mid;
}
else if(target<items[mid])high=mid-1;
else //target is >items[mis]
low = mid+1;
count++;
}
cout<<"Actual Passes: "<<count<<endl;
}

void AList::RandomNumber(){
int max;
int min;
int x;
cout<<"enter Max range: "<<endl;
cin>>max;
cout<<"enter Min range: "<<endl;
cin>>min;
srand(int(time(0)));
x=rand();
(x%(max-min-1))+min;
}
void AList::PrintList(){
//Pre: The Native Object AList is valid
//Post: The Elements from the Native Object have been Displayed.
for(int i=0; i<size;i++)
cout<<items[i]<<endl;
}
The error will have more information than that. You can't just pick out the information you find interesting and ask for help on that. You have to post everything.
the only part of the error message I took out was the line number because the post was too long and the line number wouldn't match up anyways since there is a massive block quote at the beginning of my program. The error is supposedly in my void AList::Read() function with the statement "element=read_element;" inside the if/else statement. That was all the information it gave, sorry.
I ran your code thru a compiler and got this. I suggest you fix them all. We can help if you're stuck.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
y.cc:37:14: warning: empty parentheses interpreted as a function declaration [-Wvexing-parse]
int printmenu();
             ^~
y.cc:37:14: note: replace parentheses with an initializer to declare a variable
int printmenu();
             ^~
              = 0
y.cc:40:1: warning: expression result unused [-Wunused-value]
std::exit;
^~~~~~~~~
y.cc:108:1: error: use of undeclared identifier 'temp'
temp=items[pos1];
^
y.cc:110:13: error: use of undeclared identifier 'temp'
items[pos2]=temp;
            ^
y.cc:128:8: error: assigning to 'element' (aka 'int') from incompatible type 'element ()'
userval=read_element;
       ^~~~~~~~~~~~~
y.cc:252:16: warning: expression result unused [-Wunused-value]
(x%(max-min-1))+min;
~~~~~~~~~~~~~~~^~~~
3 warnings and 3 errors generated.


EDIT: read_element is a function and userval is an int.
Last edited on
Topic archived. No new replies allowed.