Whats wrong with this code??

Discussion in 'Mac Programming' started by cybrscot, Jan 26, 2011.

  1. cybrscot macrumors 6502

    cybrscot

    Joined:
    Dec 7, 2010
    Location:
    Somewhere in Southeast Asia
    #1
    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 ;
    	
    }	
    	
     
  2. iShater macrumors 604

    iShater

    Joined:
    Aug 13, 2002
    Location:
    Chicagoland
    #2
    Shouldn't

    Code:
    !== checkdigit
    be

    Code:
    != checkdigit
    ?
     
  3. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
  4. whooleytoo macrumors 603

    whooleytoo

    Joined:
    Aug 2, 2002
    Location:
    Cork, Ireland.
    #4
    ...or to rephrase the answers above:

    !== != != !!!
     
  5. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #5
    What is this "print" function you speak of?

    I see no print function here.
     
  6. cybrscot thread starter macrumors 6502

    cybrscot

    Joined:
    Dec 7, 2010
    Location:
    Somewhere in Southeast Asia
    #6
    whoops!
     
  7. iShater macrumors 604

    iShater

    Joined:
    Aug 13, 2002
    Location:
    Chicagoland
    #7
    So line 21 has the !== problem, then we will break again with the print function. So two bugs.
     
  8. cybrscot thread starter macrumors 6502

    cybrscot

    Joined:
    Dec 7, 2010
    Location:
    Somewhere in Southeast Asia
    #8
    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 ;
    	
    }	
    	
     
  9. balamw Moderator

    balamw

    Staff Member

    Joined:
    Aug 16, 2005
    Location:
    New England
    #9
    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
     
  10. cybrscot thread starter macrumors 6502

    cybrscot

    Joined:
    Dec 7, 2010
    Location:
    Somewhere in Southeast Asia
    #10

    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.
     
  11. balamw Moderator

    balamw

    Staff Member

    Joined:
    Aug 16, 2005
    Location:
    New England
    #11
    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
     

Share This Page