我需要帮助才能找到问题计算器的问题。该程序在用户插入其字母等级时起作用,例如" A或"输出是4.0。但我遇到问题的问题是当用户插入" A +或A - " " +或 - 添加一个0.3或-0.3到他们的字母等级"它不会增加PLus或减号的值。
这就是我所拥有的:
const double Plus = 0.3;
const double Minus = 0.3;
/**
* Letter grades are A, B, C, D, and F, possibly followed by + or –.
* Their numeric values are 4, 3, 2, 1, and 0. In our scheme, there is no F+ or F–,
* or A+. Adding a + to the grade increases the numeric value by 0.3, a – decreases it
* by 0.3.
*
* Input: The letter of Grade with + or -.
* Output: The value of inserted grade.
*
* @param cin the standard input stream.
* @param cout the standard output stream.
* @return 0 for success.
*/
int run(istream& cin, ostream& cout)
{
// Enter your letter grade: C+
//Grade value is[2.3]
char list [26] = { "A, B, C, D, F, a, b, c, f" };
char grade;
double value;
cout << "Enter your letter grade: ";
cin >> grade;
cout << fixed << setprecision(1);
if ((grade == 'A' || grade == 'a')){
value = 4.0;
}
else if ((grade == 'A+') || (grade == 'a+')){
value = 4.0 + 0.3;
}
else if (grade == 'A-' || grade == 'a-'){
value = (4.0 - Minus);
}
else if (grade == 'B' || grade == 'b'){
value = 3.0;
if (grade == 'B+' || grade == 'b+'){
value = (3.0 + Plus);
}
else if (grade == 'B-' || grade == 'b-'){
value = (3.0 - Minus);
}
}
else if (grade == 'C' || grade == 'c'){
value = 2.0;
if (grade == 'C+' || grade == 'c+'){
value = (2.0 + Plus);
}
else if (grade == 'C-' || grade == 'c-'){
value = (2.0 - Minus);
}
}
else if (grade == 'D' || grade == 'd'){
value = 1.0;
if (grade == 'D+' || grade == 'd+'){
value = (1.0 + Plus);
}
else if (grade == 'D-' || grade == 'd-'){
value = (1.0 - Minus);
}
}
else if (grade == 'F' || grade == 'f'){
value = 0.0;
if (grade == 'F+' || grade == 'f+'){
value = (0.0 + Plus);
}
else if (grade == 'F-' || grade == 'f-'){
value = (0.0 - Minus);
}
}
else if (!(grade == list[26])){
cout << "INVALID LETTER GRADE" << endl;
value = 0;
}
cout << "Grade value is [" << value << "]" << endl;
return 0;
}
答案 0 :(得分:0)
简化:
您的问题是成绩是char
,无法存储'A+'
(2个字符:&#39; A&#39;,&#39; +&#39;)。
将char grade
更改为std::string grade
(请记住包含<string>
),它应该可以正常工作。
答案 1 :(得分:0)
问题是A+
或A-
是两个字符,因此无法存储在单个char
变量中。您可以使用两个变量(尽管如果您没有输入+
或-
,这可能会有问题,因为没有任何内容可以存储在第二个变量中)或者使用{{ 1}}存储输入的内容,然后检查其中是否有std::string
或+
。
此外,作为所有-
语句的简化,您可以使用switch-case块,或使用此技巧直接从字母等级获得成绩点。
对于大写字母:if
对于小写字母:gradePoint = 4.0 - (letter - 'A')
请注意,由于gradePoint = 4.0 - (letter - 'a')
被跳过评分,您必须单独检查和F
。