嘿,夥計們,我正在編寫一個計算學生GPA的程序。出於某種原因,編譯器在比較兩個字符串時給了我一個錯誤,但我似乎找不到原因。在下面你會發現一段給我錯誤的代碼。如果有人能幫我弄明白,我會很感激。在C++中比較字符串與字符
錯誤:無法轉換 '的std :: string' 到 '爲const char *' 的參數 '1' 到 'INT STRNCMP(爲const char *,爲const char *,爲size_t)'
double StudentInfo::getGPA() {
double temp = 0;
for(int i = 0; i < totalCourses; i++) {
if(strncmp(Courses[i].getGrade(), "A") == 0) //Gets string "grade", and compares it to "A".
temp = temp + 4;
if(strncmp(Courses[i].getGrade(),"A-", 2) == 0)
temp = temp + 3.7;
if(strncmp(Courses[i].getGrade(), "B+", 2) == 0)
temp = temp + 3.3;
if(strncmp(Courses[i].getGrade(), "B") == 0)
temp = temp + 3;
if(strncmp(Courses[i].getGrade(), "B-", 2) == 0)
temp = temp + 2.7;
if(strncmp(Courses[i].getGrade(), "C+", 2) == 0)
temp = temp + 2.3;
if(strncmp(Courses[i].getGrade(), "C") == 0)
temp = temp + 2;
if(strncmp(Courses[i].getGrade(), "C-") == 0)
temp = temp + 1.7;
if(strncmp(Courses[i].getGrade(), "D+") == 0)
temp = temp + 1.3;
if(strncmp(Courses[i].getGrade(), "D") == 0)
temp = temp + 1;
else
temp = temp + 0;
}
GPA = temp/totalCourses;
return GPA;}
不知道這是你的錯誤,但是大部分的STRNCMP線缺失的第三個參數(給長度來比較) – 2010-11-20 20:08:30