2014-01-13 3 views
-1

Windows 용 Visual Studio를 사용하여 프로그램을 작성하고 코드가 제대로 작동하지만 Ubuntu에서 실행할 때 "분할 오류 (코어 덤프)"문제가 발생하고 그것이 그것을 일으키는 원인을 알지 못한다. 코드에 문제가있는 곳이 어디인지 모르겠으므로 모든 내용을 붙여 넣었습니다. 미리 감사드립니다. 분할 오류는 gdb을 사용할 수 있습니다 발생 위치를 파악하기 위해세그먼트 화 오류 (Core Dumped) 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

'사람'과 '등급'은 어떻게 정의되어 있습니까? – Joe

+1

충돌하기 전에 인쇄 된 내용을 기반으로 충돌 위치를 파악할 수 있습니다. 충돌 이전에 아무것도 인쇄하지 않았다면 출력이 나올 때까지 출력 명령문을 추가하십시오. – Gabe

+0

'어디에서 충돌이 발생합니까?'뿐만 아니라 충돌하기 전에 제공 한 입력을 파악하는 것이 도움이됩니다. –

답변

1

.

GCC를 사용한다고 가정하고 프로그램을 -ggdb으로 컴파일하십시오.

다음 명령을 사용하여 프로그램을 실행

gdb myprog myinputs.txt 

일단 프롬프트 gdb 내부 입력 run. 프로세스 세분화가 실패하면 gdb 프롬프트로 돌아와야합니다. 거기에서 bt을 입력하면 백 트레이스를 얻을 수 있습니다.

이렇게하면 버그를 조사 할 수있는 충분한 정보를 얻을 수 있습니다.