我的代码有什么问题(c ++初学者)?

时间:2018-02-04 02:32:43

标签: c++ c++11 debugging logic

我刚接触编码并参加初学者c ++课程,我真的不确定我的代码有什么问题。我已经把事情搞砸了无济于事。该程序旨在向用户询问字母等级以及其是否为+, - 或无符号(例如A +,A-,A)。我必须使用一个开关和if语句,但我不确定我的逻辑在哪里失败。

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

int main()
{
char sign, letterGrade;
string gradeRange;

cout << "Enter a letter grade: ";
cin >> letterGrade;

cout << "Enter a sign (+ or -) if applicable; otherwise, enter 'x': ";
cin >> sign;

switch (letterGrade) {
case 'A': {
        if (sign == '+' || sign == 'x') {
            gradeRange = "greater than 93%";
        }
        else if (sign == '-') {
            gradeRange = "greater than or equal to 90%, but less than 93%.";
        }
        break;
    }
    case 'B': {
        if (sign == '+') {
            gradeRange = "greater than or equal to 87%, but less than 90%.";
        }
        else if (sign == 'x') {
            gradeRange = "greater than or equal to 83%, but less than 87%.";
        }
        else if (sign == '-') {
            gradeRange = "greater than or equal to 80%, but less than 83%.";
        }
        break;
    }
    case 'C': {
        if (sign == '+') {
            gradeRange = "greater than or equal to 77%, but less than 80%.";
        }
        else if (sign == 'x' || sign == '-') {
            gradeRange = "greater than or equal to 70%, but less than 77%.";
        }
        break;
    }
    case 'D': {
        if (sign == '+' || sign == '-' || sign == 'x') {
            gradeRange = "greater than or equal to 60%, but less than 70%.";
        }
        break;
    }
    case 'F': {
        if (sign == '+' || sign == 'x' || sign == '-') {
            gradeRange = "less than 60%.";
        }
        break;
    }
    default: {
        cout << "Oops, an error occured, please try again.";
    }
cout << "The student's letter grade range is " << gradeRange << endl;

}


return 0;

}

2 个答案:

答案 0 :(得分:1)

你有最终的cout-statement 里面的switch-block,这意味着它会被每个break-statement跳过。

答案 1 :(得分:0)

#include <iostream>
#include <string>

using namespace std;

int main()
{
    char sign, letterGrade;
    string gradeRange;

    cout << "Enter a letter grade: ";
    cin >> letterGrade;

    cout << "Enter a sign (+ or -) if applicable; otherwise, enter 'x': ";
    cin >> sign;

    switch (letterGrade) {
    case 'A': 
            if (sign == '+' || sign == 'x') {
                gradeRange = "greater than 93%";
            }
            else if (sign == '-') {
                gradeRange = "greater than or equal to 90%, but less than 93%.";
            }
            break;

        case 'B': 
            if (sign == '+') {
                gradeRange = "greater than or equal to 87%, but less than 90%.";
            }
            else if (sign == 'x') {
                gradeRange = "greater than or equal to 83%, but less than 87%.";
            }
            else if (sign == '-') {
                gradeRange = "greater than or equal to 80%, but less than 83%.";
            }
            break;

        case 'C': 
            if (sign == '+') {
                gradeRange = "greater than or equal to 77%, but less than 80%.";
            }
            else if (sign == 'x' || sign == '-') {
                gradeRange = "greater than or equal to 70%, but less than 77%.";
            }
            break;

        case 'D': 
            if (sign == '+' || sign == '-' || sign == 'x') {
                gradeRange = "greater than or equal to 60%, but less than 70%.";
            }
            break;

        case 'F': 
            if (sign == '+' || sign == 'x' || sign == '-') {
                gradeRange = "less than 60%.";
            }
            break;

        default: 
            cout << "Oops, an error occured, please try again.";


    }// this is for closing switch 
    cout << "The student's letter grade range is " << gradeRange << endl;
    //u kept the cout statement in switch's block so it wouldn't work
    return 0;
} //this is for closing main