Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

cybrscot

macrumors 6502
Original poster
Dec 7, 2010
282
0
Somewhere in Southeast Asia
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

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 ;
	
}
 
Code:
if ((9 - ((total - 1) % 10 )) == checkdigit)
			printf ("VALID\n") ;
		else if ((9 - ((total - 1) % 10 )) !== checkdigit)
			[COLOR="Red"]print[/COLOR] ("INVALID\n") ;

What is this "print" function you speak of?

I see no print function here.
 
hahaha!! I did it!! Awesome, this was a bit challenging for me, but I'm glad I figured it out!



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, ucd ;         /*ucd is user check digit*/
		
	
	
	printf ("Enter the 12 digit UPC: ") ;
	scanf ("%1d%1d%1d%1d%1d%1d%1d%1d%1d%1d%1d%1d", &d, &i1, &i2, &i3, &i4, &i5, &j1, &j2, &j3, &j4, &j5, &ucd) ; 
	
	first_sum = d + i2 +i4 +j1 + j3 +j5;
	second_sum = i1 + i3 + i5 + j2 + j4;
	total = 3 * first_sum + second_sum;
	checkdigit = 9 - ((total - 1) % 10); 
	
		if ((9 - ((total - 1) % 10 )) == ucd)
			printf ("VALID\n") ;
		else
			printf ("INVALID\n") ;
		

	
	return 0 ;
	
}
 
Code:
	checkdigit = 9 - ((total - 1) % 10); 
	
		if ((9 - ((total - 1) % 10 )) == ucd)
			printf ("VALID\n") ;
		else
			printf ("INVALID\n") ;

NOTE: You're not actually using checkdigit.

I presume you meant:

Code:
	checkdigit = 9 - ((total - 1) % 10); 
	
		if (checkdigit == ucd)
			printf ("VALID\n") ;
		else
			printf ("INVALID\n") ;

B
 
NOTE: You're not actually using checkdigit.

I presume you meant:

Code:
	checkdigit = 9 - ((total - 1) % 10); 
	
		if (checkdigit == ucd)
			printf ("VALID\n") ;
		else
			printf ("INVALID\n") ;

B


Yeah, you're right, I just used the formula itself rather than "checkdigit", so there was really no need to leave that in there. I was happy it worked, so I neglected to remove it as it doesn't do any harm. But in the interest of clean and easy to read code, I should've and will remove it.
 
Yeah, you're right, I just used the formula itself rather than "checkdigit", so there was really no need to leave that in there. I was happy it worked, so I neglected to remove it as it doesn't do any harm. But in the interest of clean and easy to read code, I should've and will remove it.

It's a style thing, but IMHO, keeping and using checkdigit is better than embedding the formula in the if statement. Your meaning becomes clear, and the purpose of the formula is transparent.

i.e.

Code:
		if ((9 - ((total - 1) % 1) == ucd)
			printf ("VALID\n") ;
		else
			printf ("INVALID\n") ;

is false economy.

B
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.