-4

So I'm writing a series of functions, and then I try to compile, I get errors talking about parentheses. I've looked over my code, and I can't find any mismatched parenthesis. Can anyone tell me what I'm doing wrong>

int str_to_int(char* str)
{
  int sum = 0;
  int x;

  for(x=strlen(str) ; x>=2 ; x--) {

    if(30 <= str[x] <= 39) {
      sum += raise16((str[x] - 30) x);
    };

    if(41 <= str[x] <= 46) {
      sum += raise16((str[x] - 41) x);
    };

    if(61 <= str[x] <= 66) {
      sum += raise16((str[x] - 61) x);
     };
  }

  return sum;
}
metalhead696
  • 195
  • 2
  • 11
  • 3
    I think this `30 <= str[x] <= 39` doesn't do what you think it does, also the semicolons at the end of the if statements aren't necessary – Rizier123 Jan 26 '15 at 02:43
  • 2
    Don't just tell us you have "errors talking about parentheses". Include the actual error message in your question. Out of curiosity, what is the significance of the numbers `30`, `39`, `41`, `46`, `61`, and `66`? I think you're trying to recognize `'0'` .. `'9'`, `'A'` .. `'F'`, and `'a'` .. `'f`' -- but those are the *hexadecimal* ASCII values of those characters. Don't use [magic numbers](http://en.wikipedia.org/wiki/Magic_number_(programming)#Unnamed_numerical_constants); use the character constants themselves. – Keith Thompson Jan 26 '15 at 02:59
  • 1
    An expression like `10 <= x <= 20` is actually legal, but it doesn't mean what you think it means. It means `(10 <= x) <= 20`, where `(10 <= x)` yields `0` if the condition is false, `1` if it's true. That `0` or `1` value is then compared to `20`. – Keith Thompson Jan 26 '15 at 03:16

2 Answers2

2

This is wrong

raise16((str[x] - 30) x)

perhaps you mean

raise16((str[x] - 30) * x)

and the rest of the equivalent expressions.

And also, this comparison will not work as you expect it to

if(30 <= str[x] <= 39)

what would work is

if ((str[x] >= 30) && (str[x] <= 39))
Iharob Al Asimi
  • 52,653
  • 6
  • 59
  • 97
0

use the && to concatenate 2 conditions, here is the corrected code:

int str_to_int(char* str)
{
    int sum = 0;
    int x;

    for (x = strlen(str)-1; x >= 0; x--) {
        if (30 >= str[x] && str[x] <= 39) {
            sum += raise16((str[x] - 30)*x);
        } else if (41 >= str[x] && str[x] <= 46) {
            sum += raise16((str[x] - 41)*x);
        } else if (61 >= str[x] && str[x] <= 66) {
            sum += raise16((str[x] - 61)*x);
        }
    }
    return sum;
}

Reason for correction:

  1. The condition if(61 >= str[x] && str[x] <= 66) for exmple checks for both the conditions (61 >= str[x]) and (str[x] <= 66).
  2. I have corrected the index to go to all the characters in the string by taking x as one less than length of string and checking for x>=0 as string index begins at 0.
  3. I have removed the ; at the end of the if conditions as they are not necessary.
  4. changed the if conditions to else if conditins as they are exclusive.
  • Maybe you want to explain a bit more to OP why to use `&&` operator and make a note that he missed the operators in the function calls plus the semicolons at the end of the if statements – Rizier123 Jan 26 '15 at 02:49
  • `(30 >= str[x] && str[x] <= 39)` -- the `>=` should be `<=`. And the numbers are all wrong (see my comment on the question). – Keith Thompson Jan 26 '15 at 03:14
  • To notify users,use "@KeithThomson" otherwise they may not see the message. What keith says is right. `if (30 >= str[x] && str[x] <= 39) ` is the same as `if (str[x] <= 30)` in your code which is **not** what the OP wanted. – Spikatrix Jan 26 '15 at 03:44
  • I told you to use "@username" to notify a user. And what you have said is wrong. They are the same. Both of them will be true only when `str[x]<=30` – Spikatrix Jan 26 '15 at 03:55
  • Why have you deleted all your comments without fixing the code? It makes our comments look incomplete too! – Spikatrix Jan 26 '15 at 04:16
  • your comments are in-appropriate. i have compiled this code and verified the values. I dont want to use in-appropriate words as i wish to contribute to this site for a long time. Sorry if you disagree, we will have to continue that way. – Ganesh Kamath - 'Code Frenzy' Jan 26 '15 at 04:18