So i need help figurin out where my segmentation fault is comin from. Im not even close to finishing this program but i was just testing my progress so far to make sure that it was taking values correctly, but sometimes the program doesnt stop when its supposed to and when it does i get a segmentation fault at the end, even if it works the way i want it to.

heres the code:

#include<iostream>
using namespace std;

class Heapsort
{
  private:
     int mylist[];
     int mysize;
  public:
     void get_size();
     bool empty();
     void sort();
     void fill_array();
     void print(int);
};


void Heapsort::get_size()
{
   cout <<endl<<"Please enter the size of the array you want to sort: ";
   cin >> mysize;
   int mylist[mysize];
}

void Heapsort::fill_array()
{
    int i;
    int num;

    for (i=0; i < mysize; i++){
       cout << "Enter a number 1-100: ";
       cin >> num;
       if ((num < 1)||(num > 100)){
           cout << "\n\nEnter a correct number please.\n\n";
           i = i - 1;
       } else {
           mylist[i] = num;
       }
    }
}


int main()
{
  Heapsort heap;

  heap.get_size();

  cout << endl << endl;

  heap.fill_array();

  cout << endl << endl;
    
  return (0);
}

Recommended Answers

All 3 Replies

i just realized i messed those tags up... my bad.

I'm not sure if this is related to your problem, but I'm pretty sure you aren't allowed to just declare arrays of a given size on demand:

cout <<endl<<"Please enter the size of the array you want to sort: ";   
cin >> mysize;   
//no bueno
int mylist[mysize];

I think you have to either be up front and declare an array of an arbitrary size, or create a dynamic array of whatever size you want.. but trying to declare an array of variable size is not allowed.

Try this instead:

cout <<endl<<"Please enter the size of the array you want to sort: ";   
cin >> mysize;   
//simple dynamic array 
int *mylist = new int[mysize];

alright thanks, i completely forgot about dynamic arrays lol. ill try that and see if it fixes the seg. fault.

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.