I made this program for school having a few problems and it's saying segment fragment fail, I'm pretty lost :(
here's what i have
#include <iostream>
using namespace std;
const int MLS = 50;
typedef int element;
const element SENTINEL = -1;
class AList {
private:
element items [MLS];
int size;
void Swap(int pos1, int pos2);
int Read_int();
element Read_element();
public:
void Menu();
void Read();
void Print();
void BubbleSort();
void InsertionSort();
void SelectionSort();
};
int main() {
cout<< "Sort and Search Demo Program, version 1.0\n"
<< "(c) 2011, Rob Brann\n\n";
const int keyboard = 1,
random = 2,
bubble = 3,
insertion = 4,
selection = 5,
linear = 6,
binary = 7,
quit = 8;
cout<< "1: Reset the current list from the keyboard\n"
<< "2: Reset the current list using randomly generated elements\n"
<< "3: Perform Bubble Sort on the current list\n"
<< "4: Perform Insertion Sort on the current list\n"
<< "5: Perform Selection Sort on the current list\n"
<< "6: Perform Linear Search on the current list\n"
<< "7: Perform Binary Search on the current list\n"
<< "8: Quit the Program\n\n"
<< "Choose an action: ";
choice=Read_int();
while ((choice<1)||(choice>8)) {
cout << "invalid menu choice please try again: ";
choice=Read_int();
if (choice==1)
Read();
else;
cout<<"wrong";
}
}
int AList::Read_int() {
int val;
cin>> val;
while (!cin.good()) {
cout<< "invalid menu choice please try again: ";
cin.clear();
cin.ignore(80,'\n');
cin>>val; }
return val;
}
void AList::Read() {
element userval;
cout<< "Enter elements : ";
while ((userval!=SENTINEL) && (size<MLS)) {
items [size] = userval;
size ++;
}
}
void AList::Print(){
for (int i=0;i<size;i++)
cout<< items[i]<<endl;
}
void AList::BubbleSort() {
for (int i=0; size-1; i++){
for (int j=0; j<size-1-i; j++)
if (items[j]>items[j+1])
Swap (j, j+1);
else
;
}
}
void AList::Swap(int pos1, int pos2) {
element temp;
temp = items[pos2];
items[pos2]=items[pos1];
items[pos1]=temp;
}
element Read_element() {
element userval;
cin>> userval;
while ( !cin.good());
cout<< "invalid choice please enter a whole number between 1 and"
<< " 1000: ";
cin.clear();
cin.ignore(80,'\n');
cin>> userval;
return userval;
}
void AList::InsertionSort() {
int j;
bool done;
for (int i=1; i<size; size++)
j=i;
done = false;
while ((j>=1) && (!done))
if (items[j] < items[j-1]) {
Swap (j,j-1);
j--;
}
else
done=true;
}
void AList::SelectionSort() {
int maxpos;
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);
}
}
I compiled your program here and it ran fine. You only had a warning.
1 2 3 4 5 6 7 8 9 10
void AList::Read()
{
element userval;
cout<< "Enter elements : ";
while ((userval!=SENTINEL) && (size<MLS))
{
items [size] = userval;
size ++;
}
}
The variable userval was being used without being initialized. You should set it to 0 or NULL or something that will get rid of that warning because when I quit the program, I got an error as it tried to leave. The program closed but not fully leaving it hanging without any way to input into it or even close it without using the Task Manager (or in my case, the debugger controlling it).
Your error isn't with the code but rather with your compiler or something else.