切换语句中的浮点数据类型 [英] Float Data Type in Switch Statement
问题描述
我正在为一个较大的程序创建一个函数,该函数将输入字母等级并将其存储在程序的其他位置的数组中,然后为该字母等级指定一个等级点(即A = 4.0,B = 3.0,等等),稍后将在另一个函数中使用该函数,根据存储在另一个数组中的课程时间来计算总GPA.
I'm working on creating a function for a larger program that will take a letter grade that is entered and stored in an array elsewhere in the program and then assign a grade point to that letter grade (ie. A = 4.0, B = 3.0, etc.), which will later be used in another function to calculate total GPA based on course hours that have been stored in another array.
我之所以选择使用switch语句,是因为我在之前的编程中对它们很熟悉,但是我愿意采用一种更好的方法来完成此任务.我当前遇到的错误是"switch语句不完整".
I've chosen to use a switch statement because I'm familiar with them from previous programming I've done, but I'm open to a better method of accomplishing this task if possible. The current error I'm getting is "switch statement not integral".
float GradePoints(char grades[])
{
float points = 0.0;
switch (grades[])
{
case 'A':
points = 4.0;
break;
case 'B':
points = 3.0;
break;
case 'C':
points = 2.0;
break;
case 'D':
points = 1.0;
break;
case 'F':
points = 0.0;
break;
}
return points;
}
推荐答案
一次处理每个1年级.从以下位置更改函数原型:
Process each grade 1 at a time. Change your function prototype from:
float GradePoints(char grades[])
到
float GradePoints(char grade)
并将您的switch语句更改为 switch(grade)
.然后针对您的5个年级中的每一个,循环调用您的 GradePoints
函数.
and change your switch statement to switch(grade)
. Then call your GradePoints
function in a loop for each of your 5 grades.
这篇关于切换语句中的浮点数据类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!