Returning a Char from a Double

M

Mark McIntyre

If the student doesn't meet the criteria, then a F would be returned.
So instead of a return(mark), there should be a return (F);

okay, thats what you need.
and another return to convert the double into a char.

What ?

You have a statement
return 'F';

If your code reaches this point, the function returns *at once*. No
more code is executed.

I don't understand why you think you need to convert the double into
anything. Your function returns a char, A, B... F. Why do you want to
convert 'mark' into anything?
Mark McIntyre
 
K

Keith Thompson

Gregc. said:
Keith said:
Gregc. said:
Keith Thompson wrote: [...]
Let's look at your function again:

| char getGrade(double mark) {
|
|
| if (mark>= 85)
| return ('A');
| else if (mark >= 75)
| return ('B');
| else if (mark >= 65)
| return ('C');
| else if (mark >= 50)
| return ('D');
|
|
| return (mark);
| }

The last return statement is doing something that's legal (because the
value of mark, which is of type double, will be implicitly converted
to type char), but that doesn't make any sense.

The easiest thing for me to do would be to give you the answer. I
know exactly how to fix your getGrade function so it will do what (I
presume) you want it to do. Instead, I'm trying to help you solve the
problem yourself.

Think about this: Under what circumstances will the "return (mark);"
statement be executed? In those circumstances, what is the right
thing to do? What value do you want to return from your function?

I appreciate you not giving me the answer. What I want to return is a
char ie A, B etc, only if the student has met the criteria. Someting
like return getGrade (char).

And what do you want to return if the student *hasn't* met the
criteria?

The function always has to return something, and what it returns
always has to be of type char.

More specifically, what char value should getGrade return when
mark < 50?

If the student doesn't meet the criteria, then a F would be returned.
So instead of a return(mark), there should be a return (F);

Getting very close.
and another return to convert the double into a char.

I don't know what that's supposed to mean. Why do you think a
conversion is necessary?

It's much simpler than you think it is.
 
G

Gregc.

Another question, and I guess this is way off my original topic, but if
replaced the char ie A with a const char, then in theory there should
be no difference. Because when the mark is within a particular range,
it look up the const char and output its value. Is my thinking
correct?

Again, thanks for your help.

Greg
 
K

Keith Thompson

Gregc. said:
Another question, and I guess this is way off my original topic, but if
replaced the char ie A with a const char, then in theory there should
be no difference. Because when the mark is within a particular range,
it look up the const char and output its value. Is my thinking
correct?

I have no idea. Without context, I can't tell what you're asking; no
difference between what and what?

If you mean something like:

return 'A';

vs.

const char top_grade = 'A';
...
return top_grade;

there's no real difference.

Read <http://cfaj.freeshell.org/google/>. *Always* provide context
when you post a followup.
 
G

Gregc.

Keith said:
I have no idea. Without context, I can't tell what you're asking; no
difference between what and what?

If you mean something like:

return 'A';

vs.

const char top_grade = 'A';
...
return top_grade;

there's no real difference.
That's what I thought
 
K

Keith Thompson

Gregc. said:
Keith Thompson wrote: [...]
That's what I thought
Read <http://cfaj.freeshell.org/google/>. *Always* provide context
when you post a followup.

And *again*, please take the time to trim quoted material from
followups. Quote enough so your followup makes sense on its own, but
delete anything that isn't relevant. In particular, don't quote
signatures unless you're commenting on them. (I think you've been
told this before.)
 
G

Gregc.

And *again*, please take the time to trim quoted material from
followups. Quote enough so your followup makes sense on its own, but
delete anything that isn't relevant. In particular, don't quote
signatures unless you're commenting on them. (I think you've been
told this before.)

I will. Thankyou for your help.

Greg
 
C

CBFalconer

Gregc. said:
I will. Thankyou for your help.

Then why did you fail to trim the Keith's sig from your reply?

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
More details at: <http://cfaj.freeshell.org/google/>
Also see <http://www.safalra.com/special/googlegroupsreply/>
 
K

Keith Thompson

Gregc. said:
I will. Thankyou for your help.

The signature is everything following the "-- " line; you've again
quoted part of it. I'm guessing you did that to acknowledge that I
had written the material you quoted. The correct way to do that is to
leave the attribution line in place. For example, the line above:


is an attribution line.

There should have been an attribution line with my name *above* the
quoted text.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,043
Latest member
CannalabsCBDReview

Latest Threads

Top