My book asks me to write a program that asks a user to input a upc code, then the program will display either valid, or invalid. I know the first sum, second sum, and total expressions are correct. The last digit of a UPC code is the check digit, which is determined by a combination of the first 11 digits. So this program will calculate what the check digit should be based on the user input, and compare with the check digit the user actually input, then determine validity. The complete code is further down below, I think my error lies in this code snippet here. I'm saying if the computed checkdigit is equal to the int checkdigit, print valid, then I'm saying if it's not equal, print invalid.
Now that I write this I guess I could have just made an easier else statemnet that said, else printf ('invalid"), no need to check for inequality, because anything other than equal is false. Anyway, when I compiled it, my compiler said
Not sure exactly what I can do to change the expression before the = token.
Now that I write this I guess I could have just made an easier else statemnet that said, else printf ('invalid"), no need to check for inequality, because anything other than equal is false. Anyway, when I compiled it, my compiler said
Code:
/Users/scottdean/documents/upc2.c: In function main:
/Users/scottdean/documents/upc2.c:21: error: expected expression before = token
Scott-Deans-MacBook-Pro:documents scottdean$
Not sure exactly what I can do to change the expression before the = token.
Code:
if ((9 - ((total - 1) % 10 )) == checkdigit)
printf ("VALID\n") ;
else if ((9 - ((total - 1) % 10 )) !== checkdigit)
print ("INVALID\n") ;
Code:
#include <stdio.h>
/*user enters a upc code, program computes check digit and compares with the user input checkdigit, then determines valid, or invalid */
main ( )
{
int d, i1, i2, i3, i4, i5, j1, j2, j3, j4, j5,
first_sum, second_sum, total, checkdigit ;
printf ("Enter the first (single) digit: ") ;
scanf ("%1d%1d%1d%1d%1d%1d%1d%1d%1d%1d%1d%1d", &d, &i1, &i2, &i3, &i4, &i5, &j1, &j2, &j3, &j4, &j5, &checkdigit) ;
first_sum = d + i2 +i4 +j1 + j3 +j5;
second_sum = i1 + i3 + i5 + j2 + j4;
total = 3 * first_sum + second_sum;
if ((9 - ((total - 1) % 10 )) == checkdigit)
printf ("VALID\n") ;
else if ((9 - ((total - 1) % 10 )) !== checkdigit)
print ("INVALID\n") ;
return 0 ;
}