0

So I trying to do a grade system in C++. For example, an A+ will gives 4.0, A gives 4.0 and A- gives 3.7. For each + - there's 0.3 difference. Here is my code :

        char grade_letter[3];
float value; 
bool input = true;

do{
    cout << "Enter letter grade : ";
    cin>>grade_letter;

    switch(grade_letter[0])
    {
    case 'A' : value = 4;
        break;
    case 'B' : value = 3;
        break;
    case 'C' : value = 2;
        break;
    case 'D' : value = 1;
        break;
    case 'E' : value = 0;
        break;
    }

    if(grade_letter[1]=='+'){
        value += 0.3;
    }
    else if (grade_letter[1]=='-'){
        value -= 0.3;
    }

    if (input == true){
        if(value > 4.0){
            cout << "The numeric value is " << floor(value + 0.5) << endl;
        }else if (value < 0.0){
            cout << "Grade out of range. " << endl;
        }else{
            cout<< "The numeric value is " << value <<  endl;
        }
    }else {
        cout << "Invalid input." << endl;
    }
}while(input == false);

It works when I tried with A+ A and A-. But however, I tried with A=, A/, A*, the result still shows the default value for each grade. There's something wrong with the validation.

If I set the boolean input default value to false, it only works with A+ A- and so on, as long as there's + and - in the input. I thinking of using string and substring to read the input but if so, I've to recode everything.

So, can somebody please help with my validation? I trying to check the length of char grade_letter using charAt but there's an error.

Thanks in advance.

4

4 回答 4

2

检查后'+''-'您需要添加一个检查以确保没有其他字符:

if(grade_letter[1]=='+'){
    value += 0.3;
}
else if (grade_letter[1]=='-'){
    value -= 0.3;
} else if (grade_letter[1]!='\0'){
    /* Error */
}
于 2013-04-26T12:04:53.847 回答
1

您需要进行以下更改:(1)为您的开关添加一个默认值以将输入设置为 false,(2)在检查 + 或 - 的 if 语句系列中添加一个 else,以便如果有其​​他字符比字符串结尾,输入将设置为假,并且 (3) 将 + 或 - 号上的检查移动到检查输入是否为真之后。

char grade_letter[3];
float value; 
bool input = true;

do{
    cout << "Enter letter grade : ";
    cin>>grade_letter;

    switch(grade_letter[0])
    {
    case 'A' : value = 4;
        break;
    case 'B' : value = 3;
        break;
    case 'C' : value = 2;
        break;
    case 'D' : value = 1;
        break;
    case 'E' : value = 0;
        break;
    default:
        input = false;
        break;
    }

    if (input == true){
        if(grade_letter[1]=='+'){
            value += 0.3;
        }
        else if (grade_letter[1]=='-'){
            value -= 0.3;
        } else if (grade_letter[1]){
            // something other than end of string so bad input
            input = false;
        }
        if (input) {
            if(value > 4.0){
                cout << "The numeric value is " << floor(value + 0.5) << endl;
            }else if (value < 0.0){
                cout << "Grade out of range. " << endl;
            }else{
                cout<< "The numeric value is " << value <<  endl;
            }
        }
    }
    if (! input) {
        // at some point we have bad input so let user know.
        cout << "Invalid input." << endl;
    }
}while(input == false);
于 2013-04-26T12:15:54.247 回答
1

您从未设置inputfalse. else也许您在检查第二个字符是一个之后-想要另一个input = false

if(grade_letter[1]=='+'){
    value += 0.3;
}
else if (grade_letter[1]=='-'){
    value -= 0.3;
}
else {
    input = false;
}
于 2013-04-26T12:02:47.847 回答
0

因此,您在验证中错过了两个地方。

一、等级字母:

switch(grade_letter[0])
{
case 'A' : value = 4;
    break;
case 'B' : value = 3;
    break;
case 'C' : value = 2;
    break;
case 'D' : value = 1;
    break;
case 'E' : value = 0;
    break;
default: 
    valid = false;  // What we don't recognize is invalid
    break;
}

然后是第二部分验证:

if (valid)  // continue verification only if valid
{
    switch(grade_letter[1])
    {
    case '+': value += 0.3; break;
    case '-': value -= 0.3; break;
    case ' ': 
    case '\0': break; // we assume space and EOL is OK
    default: valid = false; break;
    }
}
于 2013-04-26T12:07:11.197 回答