2011-10-09 1 views
-1
내가 우분투 (리눅스) codeblocks를 사용하고 프로그램을 실행하는 동안 콘솔에서 세그먼트 오류를 ​​얻고있다

C++ 등급 계산기 콘솔 오류

가 0 오류 및 0 경고 페이스트 빈 에서

코드

로 컴파일 여기

http://pastebin.com/wgSHPQjc 코드

#include <iostream> 
#include <string> 
using namespace std; 

class Year 
{ 
    public: 

Year(string sone, string stwo, string sthree, string sfour, string sfive, string ssix, string sseven, string seight) // constructor 
{ 
     subjectName[0] = sone; 
     subjectName[1] = stwo; 
     subjectName[2] = sthree; 
     subjectName[3] = sfour; 
     subjectName[4] = sfive; 
     subjectName[5] = ssix; 
     subjectName[6] = sseven; 
     subjectName[7] = seight; 

     sum = 0; 
     percentage = 0; 
} 
void nameOfSem(string semName) // semster name 
{ 
    name = semName; 
    cout << "Enter your " << name << " marks"<< endl; 
} 
//no of subjects in semster and store marks in an array 
void readMarks(int noOfSubjects) 
{ 
    subjects = noOfSubjects; 
    for(int i=0; i<subjects; i++) 
    { 
     cout << subjectName[i]; // print out subject name stored in the array 
     cin >> yearName[i]; // input from keyboard of marks 
     // while loop so that user enters marks from 0 to 100 
     while (yearName[i]<0 || yearName[i] > 100) 
     { 
      cout << "incorrect input please enter again: "; 
      cin >> yearName[i]; 
     } 
    } 
} 

// function for calculating avarage 
void avarage() 
{ 
    for(int j=0; j<subjects; j++) // addtion of marks (addtion of array) 
    { 
     sum += yearName[j]; 
    } 
    cout << "the total is : " << sum << endl; 
    percentage = float(sum)/float(subjects); 
    cout << "The percentage is : " << percentage << endl; 
} 

      int sum; // for storing sum of marks 

      string name; // for storing name of the semister 
      int subjects; // for storing number of subjects in semister 
      float percentage; // calculating percentage in the sem 
      int yearName[]; // array for string marks 
      string subjectName[]; // array for storin g subject names 

}; 


// main function 
int main() 
{ 
    cout << "Welcome to xxx uni " << endl; 

    // constructor for storing subjects name in the array 
    Year first("Appiled Physics ", "Electronic Devices circuits ", "Basic electrical Engineering ", "C & Data Structures", "English ", "Mathematical Methods ", "mathematics 1 ", "Engineering Drawing "); 

    // name of the sem 
    first.nameOfSem("First Year"); 

    //no of subjects and storing marks in the array 
    first.readMarks(8); 

    //calculating avarage 
    first.avarage(); 




/* 
    Year two(" " , " ", " ") 
    second year object 

    */ 
    return 0; 
} 
+2

디버거에서 실행 해 보셨습니까? (예 : gdb) SEGFAULT는 어떤 행을 표시합니까? – Flexo

+0

@awoodland its line 11 – SRN

답변

4
int yearName[]; // array for string marks 
string subjectName[]; // array for storin g subject names 
에게 있습니다

컴파일러가 표준을 준수하면이 두 줄을 컴파일해서는 안됩니다.

사용 std::vector :

std::vector<int> yearName; 
std::vector<string> subjectName; 

한 다음에 요소를 추가 할 push_back 기능을 사용하십시오.

Year(string sone, string stwo, string sthree, /*...*/) 
{ 
     subjectName.push_back(sone); 
     subjectName.push_back(stwo); 
     subjectName.push_back(sthree); 
     //so on  
}