Returning a Char from a Double

Discussion in 'C Programming' started by Gregc., Apr 14, 2006.

  1. Gregc.

    Gregc. Guest

    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
     
    Gregc., Apr 14, 2006
    #1
    1. Advertising

  2. Gregc.

    Guest

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

    What is your purpose? return (mark) is changed into return('E') ??
     
    , Apr 14, 2006
    #2
    1. Advertising

  3. 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)
     
    Richard Heathfield, Apr 14, 2006
    #3
  4. Gregc.

    Gregc. Guest

    wrote:
    > The warning comes from the last statement "return(mark)".
    >
    > What is your purpose?

    If the grade is 85% then A is printed
     
    Gregc., Apr 14, 2006
    #4
  5. Gregc.

    Al Balmer Guest

    On 13 Apr 2006 16:40:57 -0700, "Gregc." <>
    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
     
    Al Balmer, Apr 14, 2006
    #5
  6. "Gregc." <> 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) <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.
     
    Keith Thompson, Apr 14, 2006
    #6
  7. Gregc.

    Gregc. Guest


    >
    > 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'
     
    Gregc., Apr 14, 2006
    #7
  8. Gregc.

    Guest

    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').
     
    , Apr 14, 2006
    #8
  9. Gregc.

    Gregc. Guest

    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?
     
    Gregc., Apr 14, 2006
    #9
  10. Gregc.

    Jaspreet Guest

    Gregc. wrote:
    > 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.
     
    Jaspreet, Apr 14, 2006
    #10
  11. On 13 Apr 2006 19:55:58 -0700, in comp.lang.c , "Gregc."
    <> wrote:

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


    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
    --
    "Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are,
    by definition, not smart enough to debug it."
    --Brian Kernighan
     
    Mark McIntyre, Apr 14, 2006
    #11
  12. Gregc.

    Gregc. Guest


    > 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
     
    Gregc., Apr 15, 2006
    #12
  13. "Gregc." <> writes:
    >> 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.


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

    Keith Thompson <> writes:

    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?

    --
    Keith Thompson (The_Other_Keith) <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.
     
    Keith Thompson, Apr 15, 2006
    #13
  14. Gregc.

    Gregc. Guest

    Keith Thompson wrote:
    > "Gregc." <> writes:
    > >> 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.

    >
    > I wrote the above, but you snipped the attribution line, the one that
    > looks something like
    >
    > Keith Thompson <> writes:
    >
    > Please don't do that. Discussions are easier to follow if we can tell
    > who wrote what.

    Sorry about that.

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

    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


    > --
    > Keith Thompson (The_Other_Keith) <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., Apr 15, 2006
    #14
  15. "Gregc." <> writes:
    > 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?

    --
    Keith Thompson (The_Other_Keith) <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.
     
    Keith Thompson, Apr 15, 2006
    #15
  16. Gregc.

    Gregc. Guest

    Keith Thompson wrote:
    > "Gregc." <> writes:
    > > 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

    > --
    > Keith Thompson (The_Other_Keith) <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., Apr 15, 2006
    #16
  17. Gregc. opined:

    >
    > Keith Thompson wrote:
    >> "Gregc." <> writes:
    >> > 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>
     
    Vladimir S. Oka, Apr 15, 2006
    #17
  18. Gregc.

    Gregc. Guest

    Thanks for your help gentlemen.
     
    Gregc., Apr 15, 2006
    #18
  19. Gregc.

    CBFalconer Guest

    "Gregc." wrote:
    > Keith Thompson wrote:
    >> "Gregc." <> writes:
    >>> 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/>
     
    CBFalconer, Apr 15, 2006
    #19
  20. On 14 Apr 2006 23:12:32 -0700
    "Gregc." <> wrote:

    <snip>

    > 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
     
    M. =?ISO-8859-1?Q?=C5hman?=, Apr 15, 2006
    #20
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. wwj
    Replies:
    7
    Views:
    558
  2. Sydex
    Replies:
    12
    Views:
    6,505
    Victor Bazarov
    Feb 17, 2005
  3. wwj
    Replies:
    24
    Views:
    2,520
    Mike Wahler
    Nov 7, 2003
  4. Ben Pfaff
    Replies:
    5
    Views:
    480
    Tristan Miller
    Jan 17, 2004
  5. lovecreatesbeauty
    Replies:
    1
    Views:
    1,061
    Ian Collins
    May 9, 2006
Loading...

Share This Page