Returning a Char from a Double

G

Gregc.

G'day

I was wondering if someone can explain the concept of 'returning a Char
from a Double' . For example, I have the following code:

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);

When I try to complie it, I get warning: return to char from double.

Thanks

Greg
 
D

danielhe99

The warning comes from the last statement "return(mark)".

What is your purpose? return (mark) is changed into return('E') ??
 
R

Richard Heathfield

Gregc. said:
G'day

I was wondering if someone can explain the concept of 'returning a Char
from a Double' . For example, I have the following code:

char getGrade(double mark) {

The function is defined to return char, yes? So far so good.
if (mark>= 85)
return ('A');

If mark is greater than 85, you try to return 'A'. In fact, 'A' has int
type, but it's okay because we know the value of 'A' is representable as a
char, and a conversion will be supplied automatically.
else if (mark >= 75)
return ('B');

Same for 'B'...
else if (mark >= 65)
return ('C');
else if (mark >= 50)
return ('D');

....and 'C' and 'D'. But if mark < 50, none of the above applies, so we drop
down to the default case:
return (mark);

mark is a double - but this function is supposed to return char, not double.
When I try to complie it, I get warning: return to char from double.

What you need to do is assign a grade to marks that are < 50. I suggest
replacing the line with:

return 'E';

or, if you prefer:

return 'F'; /* Failed! Take the exam again in summer school... */
 
A

Al Balmer

G'day

I was wondering if someone can explain the concept of 'returning a Char
from a Double' . For example, I have the following code:

char getGrade(double mark) {

declares getGrade as a function returning a char. the variable mark is
declared to be a double.
if (mark>= 85)
return ('A');

Here you return an int, but it will be within the range of a char.
else if (mark >= 75)
return ('B');
else if (mark >= 65)
return ('C');
else if (mark >= 50)
return ('D');


return (mark);

Here you return a double, returned as a char. That's why the warning.
In general, there's no guarantee that a double will be within the
legal range for a char.

If you write

return (char) mark;

you may eliminate the warning, because you're indicating to the
compiler that you did it on purpose, and presumably know what you're
doing. But it's not guaranteed.
 
K

Keith Thompson

Gregc. said:
I was wondering if someone can explain the concept of 'returning a Char
from a Double' . For example, I have the following code:

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);

When I try to complie it, I get warning: return to char from double.

It's something you don't want to do.

mark is of type double; getGrade returns a result of type char. For
example, getGrade(90.0) returns 'A', but getGrade(45.0) attempts to
return 45.0, which is not a value of type char.

As it happens, the value will be implicitly converted from double to
char; the result is character whose code is 45 ('-' in ASCII). The
compiler is clever enough to let you know that, though this is legal,
it's unlikely to be what you want.

Think about what value you want getGrade to return if it falls through
all the if statements.
 
G

Gregc.

then you should not return(mark), you should return('F').

Given that I am trying to return a char, then in the printf statement I
would use %c, and getGrade because I am trying to return the function.
Am I correct in my logic?
 
J

Jaspreet

Gregc. said:
Given that I am trying to return a char, then in the printf statement I
would use %c, and getGrade because I am trying to return the function.
Am I correct in my logic?

Just use a return ('F') at end and you are right in using the getGrade
return type to display in printf using a %c.
 
M

Mark McIntyre

Given that I am trying to return a char, then in the printf statement I
would use %c, and getGrade because I am trying to return the function.
Am I correct in my logic?

Yes, but it has nothing to do with your problem. You didn't return a
grade if mark was < 50%, you need to fix your algorithm.

Mark McIntyre
 
G

Gregc.

mark is of type double; getGrade returns a result of type char. For
example, getGrade(90.0) returns 'A', but getGrade(45.0) attempts to
return 45.0, which is not a value of type char.

This may sound silly, but when I try to output the grade using a
printf("%c\n", getGrade) it comes up blank. Is this because the
getGrade function is looking at a number and not a character?

Greg
 
K

Keith Thompson

I wrote the above, but you snipped the attribution line, the one that
looks something like


Please don't do that. Discussions are easier to follow if we can tell
who wrote what.
This may sound silly, but when I try to output the grade using a
printf("%c\n", getGrade) it comes up blank. Is this because the
getGrade function is looking at a number and not a character?

The printf call isn't the problem. The problem is a bug in your
getGrade function.

Once again, you've declared getGrade() to return a result of type
char, but you have a return statement, "return (mark);", in which you
attempt to return a value of type double.

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?
 
G

Gregc.

Keith said:
I wrote the above, but you snipped the attribution line, the one that
looks something like


Please don't do that. Discussions are easier to follow if we can tell
who wrote what.
Sorry about that.
The printf call isn't the problem. The problem is a bug in your
getGrade function.

Once again, you've declared getGrade() to return a result of type
char, but you have a return statement, "return (mark);", in which you
attempt to return a value of type double.

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).

G

 
K

Keith Thompson

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?
 
G

Gregc.

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);

and another return to convert the double into a char.
G
 
V

Vladimir S. Oka

Gregc. opined:
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);
Correct.

and another return to convert the double into a char.

But why this, when you've already done the conversion?

If you've already exhausted all possible execution paths, it's
redundant, confusing, and potential source of maintenance bugs.

If you're algorithm is faulty (i.e. there's more execution paths than
you provided for), it will get executed and you end up with exactly
the same problem as earlier.

There is a place for a "this should never happen" type constructs, but
you'd still want to return something sensible (especially as in these
cases input values are probably weird in the first place). In your
case, something sensible is a char, not included in the set of grade
indicators. You can then test for this in the caller, inform user of
the problem, and abort, for example.

PS
Don't quote signatures, unless you're commenting on them (the lot after
"-- ").

--
Sigh. I like to think it's just the Linux people who want to be on
the "leading edge" so bad they walk right off the precipice.
(Craig E. Groeschel)

<http://clc-wiki.net/wiki/Introduction_to_comp.lang.c>
 
C

CBFalconer

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);

and another return to convert the double into a char.

Lets try the power of a little reformatting, which means pushing
around the blanks and line endings, without altering the order of
things. Here is your original function, reformatted:

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');
/* else */ return (mark);
}

I also added a baby comment before the "return (mark)", which may
clear things up. Does the symmettry give you a clue?

--
"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/>
 
M

M. =?ISO-8859-1?Q?=C5hman?=

On 14 Apr 2006 23:12:32 -0700

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).

G

If mark < 50, don't print anything, is that what you want? Maybe
this will help, maybe not:

Don't think of C's return mechanism as a parcel which can have
something in it or not. Computer memory can be compared to a row
of combination padlocks, a memory position always "contain" something.
The return statement change the value of a byte or bytes in memory,
the caller read that place.

You can choose to leave whatever value is in the byte (it can be
anything, even 'A' or 'B') unchanged, by not executing a return.
But you can't return nothing.

Magnus
 

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

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,189
Latest member
CryptoTaxSoftware

Latest Threads

Top