NO

Author Topic: help!! expected char const and lvalue errors... bah!  (Read 3831 times)

sugartwitch

  • Guest
help!! expected char const and lvalue errors... bah!
« on: November 23, 2010, 08:06:53 AM »
Is anyone able to point out where I am wrong or make a suggestion? This is just one chunk of my biggest program yet! (I don't/can't write very big programs...  :P)

I'm doing this Luhn's algorithm thing. This is the chunk:/color]

int run_Luhn (const char* digit_num) ;
{
    int isOdd = 1;            // will check to see if the position is odd
    int sum_odd = 0;
    int sum_even = 0;
    int i, digit, modulus;

    for (i = strlen(digit_num) - 1; i >= 0; --1)                 
      {
  digit = digit_num - '0';
        if (isOdd)                         
        {
            sum_odd = sum_odd + digit;
        }
        else
        {
            sum_even = sum_even + (digit / 5) + ((2 * digit) % 10);       
        }
        isOdd = !isOdd;           
    modulus = (sum_odd + sum_even) % 10;     
    return modulus == 0;   
    }
}

And voici les erreurs:

Building a4 hiyah.obj.
C:\Users\Casey_2\Documents\a4 hiyah.c(95): error #2140: Type error in argument 1 to 'strlen'; expected 'const char *' but found 'int *'.
C:\Users\Casey_2\Documents\a4 hiyah.c(95): error #2088: Lvalue required.
*** Error code: 1 ***
Done.


Ps. line 95 is at the "for" statement

Thank you!!!

JohnF

  • Guest
Re: help!! expected char const and lvalue errors... bah!
« Reply #1 on: November 23, 2010, 09:26:01 AM »
One problem still remains, this line

digit = digit_num - '0';

You are mixing ints and pointer to chars.

The following compiles except for the line above.

Code: [Select]
int run_Luhn(const char * digit_num)
{
int isOdd = 1; // will check to see if the position is odd
int sum_odd = 0;
int sum_even = 0;
int i, digit, modulus = 0;

for (i = strlen(digit_num) - 1; i >= 0; --i)
{
digit = digit_num - '0';
if (isOdd)
{
sum_odd = sum_odd + digit;
}
else
{
sum_even = sum_even + (digit / 5) + ((2 * digit) % 10);
}
isOdd = !isOdd;
modulus = (sum_odd + sum_even) % 10;
}
return modulus == 0;
}

John

mtx500

  • Guest
Re: help!! expected char const and lvalue errors... bah!
« Reply #2 on: November 23, 2010, 09:35:38 AM »
There are several issues with your code:

1. int run_Luhn (const char* digit_num) ;
There is a superfluous semicolon at the end of this line, which is the probably cause of the error messages you get.

2. for (i = strlen(digit_num) - 1; i >= 0; --1)
In the third part, replace "--1" by "--i"

3. digit = digit_num - '0';
You have to de-reference the const char *digit_num to get at a char. You probably want digit_num[ i ].

4. return modulus == 0;
This return is inside the for-loop, but there is no return outside of the for-loop. What should be returned when the for-loop is left?

5. int i, digit, modulus;
Better use size_t instead of int for the variable i, for portability.

I don't know if the code does what you intended after these modifications, but at least it compiles without error and warning with my C compiler.