Thread: Noob needs help with macro

  1. #1
    Registered User
    Join Date
    Jun 2007
    Posts
    17

    Question Noob needs help with macro

    Hi, that's a small part of my code

    Code:
    #define PAIRSUM(d1, d2) (d1-48)*10+(d2-48)    //macro at the top
    
    	while (element < 3)
    	{
    	  printf("Please enter number your credit card number ");
    	  scanf("%c%c%c%c%c%c%c%c%c%c", &d1[element], &d2[element], &d3[element], &d4[element], &d5[element],
    	  &d6[element], &d7[element], &d8[element], &d9[element], &d10[element]);
    	  sum_of_pairs[element] = PAIRSUM (d1[element],d2[element]) + PAIRSUM (d3[element],d4[element]) + 
    	  PAIRSUM (d5[element],d6[element]) + PAIRSUM (d7[element],d8[element]) + PAIRSUM (d9[element],d10[element]);
    	  mod_result[element] = sum_of_pairs[element] % 26;
    	  vchar[element] = 'A' + mod_result[element];
    	  ++element;
    	}
    After first entry, everything is working fine and all results are good; however, every next input by the user for some reason decreases the input by 1 digit and messes up the results.

    I'm pretty sure that when using arrays, the macro should be modified?

    PS. arrays are just so I can print report of all input/results at the end.

    Thanks in advance

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    I have no idea what you're doing but try this:
    Code:
    #define PAIRSUM(d1, d2) ((d1-48)*10+(d2-48))    //macro at the top

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,674
    > #define PAIRSUM(d1, d2) (d1-48)*10+(d2-48) //macro at the top
    1. Use '0' rather than 48 (if that is what you mean).
    2. Code macros are extremely sensitive to lots of obscure operator precedence problems, depending on how you use them. It is therefore typical to parenthesise everything in a macro
    Eg
    #define PAIRSUM(d1, d2) (((d1)-'0')*10+((d2)-'0')) //macro at the top

    > scanf("&#37;c%c%c%c%c%c%c%c%c%c",
    Unlike all the other conversions, %c does NOT skip white space / newlines. What you end up with is '\n' stored in the first character of the 2nd line of input.

    Use fgets() to read a line, then sscanf to extract.
    Code:
    	  char buff[BUFSIZ];
    	  printf("Please enter number your credit card number ");
    	  fgets( buff, sizeof buff, stdin );
    	  sscanf(buff, "%c%c%c%c%c%c%c%c%c%c", &d1[element], &d2[element], &d3[element], &d4[element], &d5[element],
    	  &d6[element], &d7[element], &d8[element], &d9[element], &d10[element]);
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Jun 2007
    Posts
    17
    Thanks for the replies; unfortunately, the problem still exists.

    when the user inputs first code number (10digit), I'm getting the results I want (code, remainder, pairsum, and encryption scheme symbol. The problem is when the loop goes back and takes another code... that's when I'm getting some crazy unrelated results.

    Is there a bug in the loop? how can there be one, if the first entry always work?

    PS. fgets() don't make any difference.

    I don't think this will be of any help, but here is how I'm reading the results

    Code:
    howmany = element;
    	  printf("\nInput Code     10 Digit Sum Code        Code Value    Code With Symbol");
    	  printf("\n------------*----------------------*----------------*-------------------\n");
    	  for ( element = 0; element < howmany; element++)
    	  {
            printf("%c%c%c%c%c%c%c%c%c%c%15d%21d%13c%c%c%c%c%c%c%c%c%c%c",d1[element], d2[element], d3[element], d4[element], d5[element],
    		d6[element], d7[element], d8[element], d9[element], d10[element], sum_of_pairs[element], mod_result[element], d1[element], d2[element],
    		d3[element], d4[element], d5[element], d6[element], d7[element], d8[element], d9[element], d10[element], vchar[element]);
    	  }

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,674
    You need to post a compilable program which shows the problem then.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User
    Join Date
    Jun 2007
    Posts
    17
    this is only a fragment of the program so to eliminate confusion I made this part as the whole thing:

    Code:
    #include <stdio.h>
    /********************************** Constants *******************************/
    
    #define PAIRSUM(d1, d2) (((d1) - '0')*10+((d2) - '0'))
    #define MY_NAME "XXXXXXXXXX"
    #define SIZE 10								   /* Size of array */
    
    int main(void)
    {
    
    /********************************** Variables *******************************/
    
    	char		d1[SIZE],
    				d2[SIZE],
    				d3[SIZE],
    				d4[SIZE],
    				d5[SIZE],
    				d6[SIZE],
    				d7[SIZE],
    				d8[SIZE],
    				d9[SIZE],
    				d10[SIZE],
    				vchar[SIZE],
    				howmany,
    				element;			//element in an array
    				
    
    	int			sum_of_pairs[SIZE],
    				mod_result[SIZE];
    
    
        printf("\n************ ");
        printf(MY_NAME);
        printf(" presents XXXXXXXXXXXXXX Program ************\n\n");
    
    	
    	for ( element =0; element < 5; element++) 
    	{
    	  printf("Please enter number your credit card number ");
    	  scanf("&#37;c%c%c%c%c%c%c%c%c%c",&d1[element],&d2[element],&d3[element],&d4[element],&d5[element],&d6[element],&d7[element],&d8[element],&d9[element],&d10[element]);
    	  sum_of_pairs[element] = PAIRSUM (d1[element],d2[element]) + PAIRSUM (d3[element],d4[element]) + 
    	  PAIRSUM (d5[element],d6[element]) + PAIRSUM (d7[element],d8[element]) + PAIRSUM (d9[element],d10[element]);
    	  mod_result[element] = sum_of_pairs[element] % 26;
    	  vchar[element] = 'A' + mod_result[element];
    	}
        howmany = element;
    	printf("\nInput Code     10 Digit Sum Code        Code Value    Code With Symbol");
    	printf("\n------------*----------------------*----------------*-------------------\n");
    	for ( element = 0; element < howmany; element++)
    	{
          printf("%c%c%c%c%c%c%c%c%c%c%15d%21d%11c%c%c%c%c%c%c%c%c%c%c",d1[element],d2[element],d3[element],d4[element],d5[element],
    	  d6[element],d7[element],d8[element],d9[element],d10[element],sum_of_pairs[element],mod_result[element],d1[element],d2[element],
    	  d3[element],d4[element],d5[element],d6[element],d7[element],d8[element],d9[element],d10[element],vchar[element]);
    	}
    	printf("\n------------------------------------------------------------------------\n");
    
    	fflush(stdin);
    	printf("\nPress any key to terminate . . . \n");
    	getchar();
        return 0;
    }
    now to verify, when you input 0000000000 then symbol should be A, when you input 5555345400 the symbol should be Q.

    Thanks bro,

  7. #7
    Registered User
    Join Date
    Jun 2007
    Posts
    17
    can somebody compile it and tell me what's wrong? pretty please?

  8. #8
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Code:
    scanf("&#37;c%c%c%c%c%c%c%c%c%c\n",
    fflush(stdin); don't do that see faq.

  9. #9
    Registered User
    Join Date
    Jun 2007
    Posts
    17
    Quote Originally Posted by robwhit View Post
    Code:
    scanf("%c%c%c%c%c%c%c%c%c%c\n",
    fflush(stdin); don't do that see faq.
    where did you found that first line in my code?

    fflush is a requirement...

  10. #10
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,786
    Code:
    scanf("&#37;c%c%c%c%c%c%c%c%c%c",&d1[element],&d2[element],&d3[element],&d4[element],&d5[element],&d6[element],&d7[element],&d8[element],&d9[element],&d10[element]);
    fflush is a requirement...
    It is not defined. Its behavior is fully implementation dependent. Could work, could not... Use some methods that always work
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  11. #11
    Registered User
    Join Date
    Jun 2007
    Posts
    17
    Quote Originally Posted by vart View Post
    Code:
    scanf("%c%c%c%c%c%c%c%c%c%c",&d1[element],&d2[element],&d3[element],&d4[element],&d5[element],&d6[element],&d7[element],&d8[element],&d9[element],&d10[element]);

    It is not defined. Its behavior is fully implementation dependent. Could work, could not... Use some methods that always work
    what is wrong with this first line?

    fflush what my proff want us to use, that's what I meant by requirement

  12. #12
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,786
    As was said - your scanf leaves the new line in the buffer
    so use fgets or add \n to the format:

    scanf("&#37;c%c%c%c%c%c%c%c%c%c\n",

    fflush what my proff want us to use,
    He is wrong - on the ANSI C this does not solve your problem - use methods mentioned above
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  13. #13
    Registered User
    Join Date
    Jun 2007
    Posts
    17
    ok thanks, will try that, and argue with him about fflush

    Appreciate all the help

  14. #14
    Registered User
    Join Date
    Sep 2006
    Posts
    8,866
    To clarify a bit:

    flushing *OUTPUT* streams is fine. Flushing the *INPUT* streams is what is not supported as part of ANSI C.

    It may be supported on YOUR (or your professor's), compiler, but it isn't supported by the standard, and isn't supported by other C, ANSI compliant, compilers.

  15. #15
    Registered User
    Join Date
    Jun 2007
    Posts
    17
    so that function should do the trick and be supported with ANSI C?

    Code:
    void flush(void)
    {
    	int ch;
    	do
    	  ch = getchar();
    	while (ch != EOF && ch != '\n');
    	clearerr(stdin);
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem building Quake source
    By Silvercord in forum Game Programming
    Replies: 16
    Last Post: 07-11-2010, 09:13 AM
  2. Errors including <windows.h>
    By jw232 in forum Windows Programming
    Replies: 4
    Last Post: 07-29-2008, 01:29 PM
  3. Quantum Random Bit Generator
    By shawnt in forum C++ Programming
    Replies: 62
    Last Post: 06-18-2008, 10:17 AM
  4. L macro
    By George2 in forum C Programming
    Replies: 1
    Last Post: 08-20-2007, 09:24 AM
  5. about Makefile and Macro
    By tom_mk in forum C++ Programming
    Replies: 1
    Last Post: 09-18-2003, 01:07 PM