Velocity Reviews > Returning a Char from a Double

# Returning a Char from a Double

Gregc.
Guest
Posts: n/a

 04-13-2006
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

danielhe99@gmail.com
Guest
Posts: n/a

 04-13-2006
The warning comes from the last statement "return(mark)".

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

Richard Heathfield
Guest
Posts: n/a

 04-13-2006
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... */

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)

Gregc.
Guest
Posts: n/a

 04-14-2006

http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> The warning comes from the last statement "return(mark)".
>
> What is your purpose?

If the grade is 85% then A is printed

Al Balmer
Guest
Posts: n/a

 04-14-2006
On 13 Apr 2006 16:40:57 -0700, "Gregc." <(E-Mail Removed)>
wrote:

>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.
>
>When I try to complie it, I get warning: return to char from double.
>
>Thanks
>
>Greg

--
Al Balmer
Sun City, AZ

Keith Thompson
Guest
Posts: n/a

 04-14-2006
"Gregc." <(E-Mail Removed)> writes:
> 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.

--
Keith Thompson (The_Other_Keith) (E-Mail Removed) <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.

Gregc.
Guest
Posts: n/a

 04-14-2006

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

If it falls through, then it will return a 'Fail Grade'

slebetman@yahoo.com
Guest
Posts: n/a

 04-14-2006
Gregc. wrote:
> >
> > Think about what value you want getGrade to return if it falls through
> > all the if statements.

>
> If it falls through, then it will return a 'Fail Grade'

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

Gregc.
Guest
Posts: n/a

 04-14-2006

(E-Mail Removed) wrote:
> Gregc. wrote:
> > >
> > > Think about what value you want getGrade to return if it falls through
> > > all the if statements.

> >
> > If it falls through, then it will return a 'Fail Grade'

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

Jaspreet
Guest
Posts: n/a

 04-14-2006

Gregc. wrote:
> (E-Mail Removed) wrote:
> > Gregc. wrote:
> > > >
> > > > Think about what value you want getGrade to return if it falls through
> > > > all the if statements.
> > >
> > > If it falls through, then it will return a 'Fail Grade'

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

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

 Thread Tools

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts BB code is On Smilies are On [IMG] code is On HTML code is OffTrackbacks are On Pingbacks are On Refbacks are Off Forum Rules

 Similar Threads Thread Thread Starter Forum Replies Last Post lovecreatesbeauty C Programming 1 05-09-2006 08:01 AM silverburgh.meryl@gmail.com C++ 3 03-09-2006 12:14 AM Sydex C++ 12 02-17-2005 06:30 PM wwj C Programming 24 11-07-2003 05:27 PM wwj C++ 7 11-05-2003 12:59 AM

Advertisments