mardi 3 mars 2020

This code keeps outputting segmentation fault and I'm not sure why. Any suggestions?

#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>

using namespace std;

int main() 
{

  int n;

  cin >> n; //currently 7

  while(n > 0)
  {

   for(int i = 0; i < n; i++)
   {

    if(i != 0)
    {
     //cout << i << "/" << n << endl;
     float test = 1.0 * i / n;
     vector<int> total(test);
     sort(total.begin(), total.end());
     cout << total[i] << endl;


    }
   }
   n--;
  }
}

Edit: Pasted my whole code. I am just a bit confused about why it is a segmentation error. I checked the internet and I don't think this is any of "Accessing a freed address", "Improper use of scanf", or an uninitialized pointer.

Aucun commentaire:

Enregistrer un commentaire