Hi so I have a project and I'm trying to upload it online onto a website which grades my project automatically. The only problem is that for some reason my file isn't compiling when I submit it to the website. I tried compiling on my computer and no errors pop up and even when I run the executable it works perfectly. The only problem that I could possibly see is that when I try double clicking the executable and inputting the same information I end up with a
'Segmentation fault: 11'
Someone please help me understand where this error originates from.
(Just to clarify, this segmentation fault only occurs when I double click the executable file but when I run it from the console it compiles and runs without error)
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <math.h> //sqrt function
using namespace std;
struct Planet{
int row;
int col;
string name;
string symbol;
int id;
};
vector< vector<string> > makeGrid(int rows, int cols);
void fillVector(vector< vector<string> > &grid);
void printVectorGrid(vector< vector<string> > grid);
void printVector(vector<Planet> p);
void printPlanet(Planet p);
double distance(Planet &a, Planet &b){
int colDif = a.col-b.col;
int rowDif = a.row-b.row;
return sqrt(rowDif*rowDif*1.0 + colDif*colDif);
}
vector<Planet> getShortestPath(int srow, int scol, int erow, int ecol ,vector<Planet> v){
Planet temp;
temp.row = srow;
temp.col = scol;
vector<Planet> thing;
while(v.size()>0){
double minDist = 100000000;
int index = 0;
for(int i=v.size()-1; i>=0; i--){
if(minDist >= distance(temp,v.at(i))){
index = i;
minDist = distance(temp, v.at(i));
}
}
temp = v.at(index);
thing.push_back(temp);
v.erase(v.begin()+index);
}
return thing;
}
int main(){
int rows, cols, startRow, startCol, endRow, endCol;
string nameFile, locationFile;
cout << "Enter Locations Filename: ";
cin >> locationFile;
cout << "Enter Names Filename: ";
cin >> nameFile;
ifstream nameIn(nameFile.c_str());
ifstream locationIn(locationFile.c_str());
locationIn >> rows >> cols >> startRow >> startCol >> endRow >> endCol;
int tempRow, tempCol, tempId;
string tempSymbol;
vector<Planet> planets;
while(locationIn >> tempRow >> tempCol >> tempSymbol >> tempId){
if(tempRow >= 1 && tempRow <= rows && tempCol >= 1 && tempCol <= cols){
Planet temp;
temp.row = tempRow;
temp.col = tempCol;
temp.symbol = tempSymbol;
temp.id = tempId;
planets.push_back(temp);
}
else{
cout << tempId << " out of range - ignoring" << endl;
}
}
string tempName;
while(nameIn >> tempId >> tempName){
for(int i=0; i<planets.size(); i++){
if(tempId == planets.at(i).id){
while(tempName.find("XX")!= string::npos){
tempName.erase(tempName.find("XX"), 2);
}
while(tempName.find("_")!= string::npos){
tempName.replace(tempName.find("_"), 1, " ");
}
planets.at(i).name = tempName;
}
}
}
nameIn.close();
locationIn.close();
//now "supposedly" have completed struct of planets
ofstream fout("journey.txt");
vector< vector<string> > grid = makeGrid(rows,cols);
fillVector(grid);
grid[startRow-1][startCol-1] = "S";
grid[endRow-1][endCol-1] = "E";
for(int i=0; i<planets.size(); i++){
grid[planets[i].row-1][planets[i].col-1] = planets[i].symbol;
}
vector<Planet> path = getShortestPath(startRow, startCol, endRow, endCol, planets);
for(int i=0; i<grid.size(); i++){
for(int j=0; j<grid.at(i).size(); j++){
fout << grid[i][j];
}
fout << endl;
}
fout << "Start at " << startRow << " " << startCol << endl;
for(int i=0; i<path.size(); i++){
fout << "Go to " << path.at(i).name << " at " << path.at(i).row << " " << path.at(i).col << endl;
}
fout << "End at " << endRow << " " << endCol;
fout.close();
return 0;
}
void printPlanet(Planet p){
cout << "Planet name: " << p.name << endl;
cout << "Planet row: " << p.row << endl;
cout << "Planet col: " << p.col << endl;
cout << "Planet symbol: " << p.symbol << endl;
cout << "Planet id: " << p.id << endl;
}
void printVector(vector<Planet> p){
for(int i=0; i<p.size(); i++){
cout << p.at(i).name << " ";
}
cout << endl;
}
vector< vector<string> > makeGrid(int rows, int cols){
vector< vector<string> > map;
for(int i=0; i<rows; i++){
vector<string> temp(cols);
map.push_back(temp);
}
return map;
}
void fillVector(vector< vector<string> > &grid){
for(int i=0; i<grid.size(); i++){
for(int j=0; j<grid.at(i).size(); j++){
grid[i][j] = ".";
}
}
}
void printVectorGrid(vector< vector<string> > grid){
for(int i=0; i<grid.size(); i++){
for(int j=0; j<grid.at(i).size(); j++){
cout << grid[i][j] << " ";
}
cout << endl;
}
}