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.