【发布时间】:2018-02-24 18:28:04
【问题描述】:
我正在尝试创建一个 GPA 计算器,方法是使用 GradePoint() 函数进行实际转换,并将 int main() 用于 cin 和 cout。
我已经被困了好几个小时了。我尝试了很多不同的东西,查阅了很多不同的教程,但似乎没有什么与我需要做的事情相似。
GradePoint()函数要求如下。
此函数将产生两种结果之一。如果参数在 0 到 100 之间,函数会将成绩四舍五入到最接近的整数,并返回等效的 GPA 值。否则,如果传入的成绩不在 0 到 100 之间,该函数将抛出 std::out_of_range 异常。
int main() 要求如下。
提示用户输入成绩并确保它是数字。不要验证范围,因为这将在 GradePoint() 函数中完成。
#include <iostream>
#include <iomanip>
using namespace std;
//function prototypes
double GradePoint(double p);
// testing some funtions
int main()
{
double p = 0.0;
cout << "Please a percentage grade to convert to grade points: ";
cin >> p;
while(!(cin >> p))
{
cin.clear();
cin.sync();
cout << "Please enter a numeric value: ";
}
cout << "A percentage grade of " << p;
cout << " Is a grade point average of " << GradePoint;
return 0;
}
double GradePoint(double p)
{
double g;
if (p >=90 && p <=100)
g = 5.0;
else if (p >=85 && p <=89)
g = 4.5;
else if (p >=80 && p <=84)
g = 4.0;
else if (p >=75 && p <=79)
g = 3.5;
else if(p >=70 && p <=74)
g = 3.0;
else if (p >=65 && p <=69)
g = 2.5;
else if (p >=60 && p <=64)
g = 2.0;
else if (p >=55 && p <=59)
g = 1.5;
else if (p >=50 && p <=54)
g = 1.0;
else if (p <50)
g = 0.0;
return g;
}
【问题讨论】:
-
您的问题是什么?你有什么问题?
-
cin.sync();应该是cin.ignore();。另外,GradePoint不调用函数。 -
有些值在
GradePoint()中没有涵盖,例如89 int 而不是double可能是更好的选择。)否则,您必须重新考虑GradePoint()函数的设计。 -
使用
cout << " Is a grade point average of " << GradePoint(p);(在括号中传递参数p)。 -
谢谢你,这正是我所需要的!!不敢相信我看了好几个小时。