-3

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;
    }
}
Bryce Caro
  • 65
  • 10
  • 2
    You and I have differing views on _works perfectly_. I'd consider a segmentation fault to not work perfectly, but that's just me. There really isn't enough information here to answer your question. I'm not sure what you're asking: why it works locally but not on some website we're not familiar with, or why you get a segmentation fault. You can [edit] your question to clarify any information. – Tas Dec 12 '16 at 02:39
  • 1
    If it doesn't compile, then please post the compiler error; surely the website must display that? Or are you confusing compilation and execution? – Ken Y-N Dec 12 '16 at 02:42
  • I understand the difference and again it compiles fine on my local device however once I upload it to the grading website it says that the file did not compile. I was just wondering if I messed up somewhere – Bryce Caro Dec 12 '16 at 02:45
  • 1
    I'm sorry but you're not asking us to find your syntax error are you? Does the website really not give you the compilation error? You should find out the version and settings of the compiler so you can compile it the same exact way locally. If it's GCC, you should alway use at least `-Wall -Werror`, and probably `-Wextra`. – Jonathon Reinhart Dec 12 '16 at 02:50
  • I don't believe it's a syntax error but I'm not 100%. I'm kind of new to C++ so I don't understand why it works on my computer but not on the website. It literally just tells me it does not compile – Bryce Caro Dec 12 '16 at 02:56
  • 1
    This is a dup of [this question](https://stackoverflow.com/questions/34250914/issue-with-using-namespace-std). – Ken Y-N Dec 12 '16 at 03:44

2 Answers2

1

The only problem is that for some reason my file isn't compiling when I submit it to the website.

Here is the set of errors from compiling your source with g++-4.8.4 on Linux:

$ g++ -Wall -c t.cc
In file included from /usr/include/c++/4.8/bits/stl_algobase.h:65:0,
                 from /usr/include/c++/4.8/bits/char_traits.h:39,
                 from /usr/include/c++/4.8/ios:40,
                 from /usr/include/c++/4.8/ostream:38,
                 from /usr/include/c++/4.8/iostream:39,
                 from t.cc:1:
/usr/include/c++/4.8/bits/stl_iterator_base_types.h: In instantiation of ‘struct std::iterator_traits<Planet>’:
/usr/include/c++/4.8/bits/stl_iterator_base_funcs.h:114:5:   required by substitution of ‘template<class _InputIterator> typename std::iterator_traits<_Iterator>::difference_type std::distance(_InputIterator, _InputIterator) [with _InputIterator = Planet]’
t.cc:37:48:   required from here
/usr/include/c++/4.8/bits/stl_iterator_base_types.h:165:53: error: no type named ‘iterator_category’ in ‘struct Planet’
       typedef typename _Iterator::iterator_category iterator_category;
                                                     ^
/usr/include/c++/4.8/bits/stl_iterator_base_types.h:166:53: error: no type named ‘value_type’ in ‘struct Planet’
       typedef typename _Iterator::value_type        value_type;
                                                     ^
/usr/include/c++/4.8/bits/stl_iterator_base_types.h:167:53: error: no type named ‘difference_type’ in ‘struct Planet’
       typedef typename _Iterator::difference_type   difference_type;
                                                     ^
/usr/include/c++/4.8/bits/stl_iterator_base_types.h:168:53: error: no type named ‘pointer’ in ‘struct Planet’
       typedef typename _Iterator::pointer           pointer;
                                                     ^
/usr/include/c++/4.8/bits/stl_iterator_base_types.h:169:53: error: no type named ‘reference’ in ‘struct Planet’
       typedef typename _Iterator::reference         reference;
                                                     ^
t.cc: In function ‘int main()’:
t.cc:87:37: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         for(int i=0; i<planets.size(); i++){
                                     ^
...

The first problem comes from your definition of distance -- see documentation for std::distance. I suggest that you get rid of the non-sensical "first word lower case; then camel-case" naming convention.

The exact problem is that this call:

 if(minDist >= distance(temp,v.at(i))){

can match both your distance(Planet &a, Planet &b), and std::distance().

The second problem (line 87) is that int is not an appropriate type to compare against (note that this is merely a warning, but your teacher may be compiling with -Werror flag, which promotes warnings to errors). Use size_t instead.

Employed Russian
  • 199,314
  • 34
  • 295
  • 362
-1

I think it is with regards to the variable Locations Filename. Does the website you are uploading into has a input file option, if so try that out. I tried running your code in an online shell, and it compiled just fine, but then it asked for file location, which I believe could help you solve your issue.

  • The website is basically a drag and drop situation where I drag my file over a box and it automatically uploads to the website. I tried uploading it and it keeps telling me it doesn't compile :( – Bryce Caro Dec 12 '16 at 02:41