小编Dan*_*iel的帖子

将字符串与C++中的字符进行比较

嘿伙计们,我正在尝试编写一个计算学生GPA的程序.出于某种原因,编译器在比较两个字符串时给出了一个错误,但我似乎无法找到它的原因.下面你会发现给我错误的代码片段.如果有人能帮助我搞清楚,我真的很感激.

错误:无法将参数'1'的'std :: string'转换为'const char*'为'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(), …
Run Code Online (Sandbox Code Playgroud)

c++

2
推荐指数
1
解决办法
285
查看次数

标签 统计

c++ ×1