Thread: Problem with a char variable set as a letter

  1. #1
    C(++)(#)
    Join Date
    Jul 2004
    Posts
    309

    Problem with a char variable set as a letter

    Ok in a program I'm working on I have a variable declared as such:
    Code:
    char newgrade;
    Now in a function I'm trying to set this variable to equal a letter based upon the comparison of variable to a number, such as this:
    Code:
         if(gradevar>=1 || gradevar<=1&&gradevar>=.90)
         {
             newgrade="A";
         }
    Now, I was taught that the "char" variable was used to store a letter. If so why am I getting the error: invalid conversion from `const char*' to `char'? Is there a diffrent variable I should use? Would it be better to use a string as the variable type? Or if possible explain what the error meant?


    Also I have tried using an array and I get the same error.
    Last edited by 7smurfs; 12-10-2004 at 12:00 PM.
    To code is divine

  2. #2
    Magically delicious LuckY's Avatar
    Join Date
    Oct 2001
    Posts
    856
    Using the double quotes (") around something declares it as an array of chars (C-style strings are such things), but you just want a single char which should be enclosed in a single quote (') also known as an apostrophe.

  3. #3
    C(++)(#)
    Join Date
    Jul 2004
    Posts
    309
    Thanks Lucky that worked like a charm
    To code is divine

  4. #4
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    Off posted question, but problem may come back to bite you later anyway, so take a look at the logic of the conditional:

    if(gradevar >=1 || gradevar<=1 && gradevar>=.90)

    In order to be true gradevar will need to be 1 and only 1. That's because 1 is both >= and <= 1 and also >= .9. Any other value for gradevar will be evaluated as false. Now, that may be what you wanted, but I doubt it. I suspect you would be better off like this:

    if(gradevar >= 1 || (gradevar <= 1 && gradevar >= .9))

    but better yet is probably something like this:

    if(gradevar >= 0.9)

    Eventhough boolean logic ends up as a simple true/false result sooner or later, it still trips people (myself included) up as often as anything else.

  5. #5
    C(++)(#)
    Join Date
    Jul 2004
    Posts
    309
    Quote Originally Posted by elad
    Off posted question, but problem may come back to bite you later anyway, so take a look at the logic of the conditional:

    if(gradevar >=1 || gradevar<=1 && gradevar>=.90)

    In order to be true gradevar will need to be 1 and only 1. That's because 1 is both >= and <= 1 and also >= .9. Any other value for gradevar will be evaluated as false. Now, that may be what you wanted, but I doubt it. I suspect you would be better off like this:

    if(gradevar >= 1 || (gradevar <= 1 && gradevar >= .9))

    but better yet is probably something like this:

    if(gradevar >= 0.9)

    Eventhough boolean logic ends up as a simple true/false result sooner or later, it still trips people (myself included) up as often as anything else.
    Wow thanks, you just fixed another bug in my program (god I love this forum )
    To code is divine

  6. #6
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Actually logical AND has a higher priority than logical OR, so it worked fine the first time. Mind you, placing full parenthese is still more clear, and of course the simplification applies.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  7. #7
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    Well, how'd ya like them apples. I thought they were of equal precedence and the conditional was read from left to right. Thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. seg fault at vectornew
    By tytelizgal in forum C Programming
    Replies: 2
    Last Post: 10-25-2008, 01:22 PM
  2. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 09:42 PM
  3. Replies: 9
    Last Post: 11-23-2007, 12:28 PM
  4. problem in char variable
    By hitesh_best in forum C Programming
    Replies: 2
    Last Post: 08-11-2007, 12:01 AM
  5. Need help understanding info in a header file
    By hicpics in forum C Programming
    Replies: 8
    Last Post: 12-02-2005, 12:36 PM