2014-01-13 91 views
-1

我一直在Windows上使用Visual Studio編程和我的代碼工作正常,但在Ubuntu上運行時,我得到一個「分段錯誤(核心轉儲)」的問題,我不不知道它是什麼造成的。我不知道代碼中的問題在哪裏,所以我粘貼了所有的代碼。先謝謝你!分割故障(核心轉儲)問題windows到linux

#include "Person.h" 
#include "Grade.h" 

using std::ifstream; 
using std::ofstream; 
using std::cout; 
using std::cin; 
using std::endl; 
using std::vector; 
using std::stringstream; 
using std::fixed; 
using std::setprecision; 
using std::setfill; 

unsigned z = 0; 
unsigned y = 0; 

void firstInput(vector<Person>& peoples, char *argv[]) 
{ 
int x = 0; 
string idy, namey, addressy, phoney, s; 

ifstream one; 
one.open(argv[1]); 

while (getline(one, s)) 
{ 
    if (x == 0) 
    { 
     idy = s; 
    } 

    if (x == 1) 
    { 
     namey = s; 
    } 

    if (x == 2) 
    { 
     addressy = s; 
    } 

    if (x == 3) 
    { 
     phoney = s; 
     Person *f1 = new Person(idy, namey, addressy, phoney); 
     peoples.push_back(*f1); 
     x = -1; 
    } 

    x++; 

} 

one.close(); 
} 

void secondInput(vector<Grade>& gradies, char *argv[]) 
{ 
int b; 
string z, idy2, gradey, classy; 

ifstream two; 
two.open(argv[2]); 

b = 0; 

while (getline(two, z)) 
{ 
    if (b == 0) 
    { 
     classy = z; 
    } 

    if (b == 1) 
    { 
     idy2 = z; 
    } 

    if (b == 2) 
    { 
     gradey = z; 
     Grade *g1 = new Grade(classy, idy2, gradey); 
     gradies.push_back(*g1); 
     b = -1; 
    } 
    b++; 
} 

two.close(); 
} 

double scoreConverter(string lettergrade) 
{ 
double converted = 0.0; 

if (lettergrade.substr(0,1) == "A") 
{ 
    converted += 4; 
} 

if (lettergrade.substr(0,1) == "B") 
{ 
    converted += 3; 
} 

if (lettergrade.substr(0,1) == "C") 
{ 
    converted += 2; 
} 

if (lettergrade.substr(0,1) == "D") 
{ 
    converted += 1;; 
} 

if (lettergrade.size() > 1) 
{ 
    if (lettergrade.substr(1,2) == "-") 
    { 
     converted -= 0.3; 
    } 
    if (lettergrade.substr(1,2) == "+") 
    { 
     converted += 0.4; 
    } 
} 
return converted; 
} 

void computeGPA(vector<Grade>& s, vector<Person>& p, string IDnum) 
{ 
int count = 0; 
y = 0; 
double gpa = 0; 

    for (string x = IDnum; y < s.size(); y++) 
    { 
     if (x == s.at(y).getIDs()) 
     { 
     gpa += scoreConverter(s.at(y).getScore()); 
     count++; 
     } 
    } 
     if (gpa > 0) 
     { 
     gpa = gpa/count; 
     } 
     cout << IDnum << " "; 
     cout << fixed << setprecision(2) << setfill('0') << gpa << " "; 

    for (unsigned x = 0; x < p.size(); x++) 
    { 
     if (IDnum == p.at(x).getID()) 
     { 
      cout << p.at(x).getName() << endl; 
      x = 1000; 
     } 
    } 

    z = y; 
} 

void thirdInput(vector<Grade>& gradies, vector<Person>& persons, char *argv[]) 
{ 
string querying; 

ifstream three; 
three.open(argv[3]); 


while(getline(three, querying)) 
{ 
for (unsigned x = 0; x < persons.size(); x++) 
{ 
    if (querying == persons.at(x).getID()) 
    { 
     computeGPA(gradies, persons, persons.at(x).getID()); 
     x = 1000; 
    } 
} 
} 
} 

int main(int argc, char *argv[]) { 

ofstream outputit; 
outputit.open(argv[4]); 

std::ofstream out(argv[4]); 
std::cout.rdbuf(out.rdbuf()); 

vector<Person> people; 
vector<Grade> grades; 

firstInput(people, argv); 

std::sort(people.begin(), people.end()); 

    for (unsigned x = 0; x < people.size(); x++) 
    { 
     cout << people.at(x).getName() << endl; 
     cout << people.at(x).getID() << endl; 
     cout << people.at(x).getPhone() << endl; 
     cout << people.at(x).getAddress() << endl; 
    } 

    cout << endl; 

secondInput(grades, argv); 

std::sort(grades.begin(), grades.end()); 

for (unsigned x = 0; x < grades.size(); x++) 
{ 
    cout << grades.at(x).getIDs() << " "; 
    cout << grades.at(x).getScore() << " "; 
    cout << grades.at(x).getClass() << endl; 
} 

cout << endl; 

thirdInput(grades, people, argv); 

outputit.close(); 
} 
+0

如何定義'Person'和'Grade'? – Joe

+1

您可以根據它在崩潰之前打印的內容來確定崩潰的位置。如果它在崩潰之前沒有打印任何內容,請添加輸出語句,直到獲得一些輸出。 – Gabe

+0

除了'它在哪裏崩潰',在知道崩潰之前提供哪些輸入將會很有幫助。 –

回答

1

爲了找出發生分段故障的位置,您可以使用gdb

使用-ggdb編譯程序,假設您使用的是GCC。

使用下面的命令運行該程序:

gdb myprog myinputs.txt 

一旦進入gdb提示符鍵入run。一旦過程分段發生故障,您應該返回到gdb提示。從那裏,你可以輸入bt來獲得回溯。

這應該會給你足夠的信息來調查你的bug。