-4

The issue im having with my code is the following:

  1. line 15, not recognising the brackets as related despite that it is
  2. as well as not recognising the first semi colon in (x/10; x<10, ++count);
  3. not recognising the divide symbol and greater than symbol (x/10; x<10, ++count);
  4. line 22, count = 15 || count = 16); apparently both of these numbers are not assignable.

// check credit number if valid
#include <cs50.h>
#include <stdio.h>
#include <math.h>

int main(void)
{
    long x;
    int count;
    count = 0;
    printf("Please enter your credit card number: \n");
    scanf("%ld", &x);
    while (x >= 9)
        {
        (x/10; x>10, ++count);
        }
    printf("length of card number: ld, count");
    if (count != 13 || count != 15 || count != 16)
        {
        printf("is invalid, \n");
        }
    if (count = 13 || count = 15 || count = 16); 
        {
        printf("is valid, \n");
        }
}
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278

2 Answers2

2

(x/10; x>10, ++count); looks like you are attempting to write the expressions of a for loop. You cannot just write these as an isolated statement. A for statement has the form:

for ( clause-1 ; expression-2 ; expression-3 ) statement

clause-1 may be either a declaration or an expression. It is evaluated at the start of the loop.

The body of the loop, in statement, which may be a compound statement ({}), is executed as long as expression-2 evaluates to true just before the statement begins. After each execution of statement, expression-3 is evaluated.

Note that semicolons are used for both separators; there is no comma.

If you intended these conditions to be merged with the while you have before them, you need to restructure the entire thing into a for loop or other proper C structure.

In the first if statement, count != 13 || count != 15 || count != 16 is wrong. count is always either not equal to 13 or not equal to 15, because it can never be equal to both. Think again about what condition you want to test.

In the second if statement, the operator for comparing values is ==. In contrast, = is the operator for assigning a value.

Eric Postpischil
  • 195,579
  • 13
  • 168
  • 312
1
  1. Not sure what you're trying to do here, but those parentheses are wrong; are you maybe confusing them with for statement syntax?

  2. Same as #1.

  3. Same as #1.

  4. = is the assignment operator. You want ==.

Carl Norum
  • 219,201
  • 40
  • 422
  • 469