Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
231 views
in Technique[技术] by (71.8m points)

c - Float Data Type in Switch Statement

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.

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;
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Process each grade 1 at a time. Change your function prototype from:

float GradePoints(char grades[])

to

float GradePoints(char grade)

and change your switch statement to switch(grade). Then call your GradePoints function in a loop for each of your 5 grades.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...