C code question

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

  1. cybrscot macrumors 6502

    cybrscot

    Joined:
    Dec 7, 2010
    Location:
    Somewhere in Southeast Asia
    #1
    This seems like an easy program, ask the user to input two digits, then have the program display them in reverse. Yes I thought it was soooo easy too, but after user enters the two digits, my gcc compiler is getting hung up, I have to hit enter a bunch of times, then some key, and it finally shows the printf, ("the reversal is: ) BUT it doesn't show the reversal, it just put a 0 in front of the two digit number? Why? I thought it was an easy one, code looks logical to me. What did I miss?

    Code:
    #include <stdio.h>
    
    main ()
    {
     
    /*this program allows a user to enter a 2 digit number and the output displays the reverse*/
    
    
    	int first_digit ;
    	int second_digit ;
    	
    
    	printf ("Enter a two-digit number: ") ;
    	scanf ("%d%d", &first_digit, &second_digit) ;
    	printf ("The reversal is: %d%d\n", second_digit, first_digit) ;
    	
    	
    	return 0 ;
    	}	
     
  2. misee macrumors member

    Joined:
    Jul 4, 2010
    #2
    %d reads an entire number, not single digits. If you want two characters to be read, you should probably use %c instead:
    Code:
    #include <stdio.h>
    #include <ctype.h>
    
    int main (int argc, const char * argv[]) {
        /*this program allows a user to enter a 2 digit number and the output displays the reverse*/
    	char first_digit ;
    	char second_digit ;
    	
        
    	printf ("Enter a two-digit number: ") ;
    	scanf ("%c%c", &first_digit, &second_digit) ;
        if (!isnumber((int)first_digit) || !isnumber((int)second_digit)) {
            printf("'%c%c' is not a number!", first_digit, second_digit);
            return 1;
        }
        
    	printf ("The reversal is: %c%c\n", second_digit, first_digit) ;
    	
    	return 0 ;
    }
    
    Note that this code does not check whether the user enters more than one digits or less. You could also use one %d and then use some basic arithmetics to separate the number into two digits.
     
  3. cybrscot thread starter macrumors 6502

    cybrscot

    Joined:
    Dec 7, 2010
    Location:
    Somewhere in Southeast Asia
    #3
    ahhhh, very interesting. I learn a lot here! I haven't come across the char or %c yet. My book wants me to do the arithmetic method, BUT I love the fact that it can be done the way I was thinking, just needed to use %c instead of %d. Cool Thanks!



     
  4. balamw Moderator

    balamw

    Staff Member

    Joined:
    Aug 16, 2005
    Location:
    New England
    #4
    Have you had conditionals (if) yet? (Nope, next chapter).

    Keep in mind as you work through the arithmetic version what happens if the number you enter is <10 or >99. Once you learn conditionals, try to implemet some basic range checking along the lines of what misee did to check if the chars were digits.

    B
     
  5. cybrscot thread starter macrumors 6502

    cybrscot

    Joined:
    Dec 7, 2010
    Location:
    Somewhere in Southeast Asia
    #5
    No I haven't learned conditionals yet. However, I've watched some tutorials on youtube, it seems that other books introduce them earlier. I've also seen that other programmers do things differently. I've seen some put an int in front of main.

    int main () my book doesn't teach this.
     
  6. cybrscot thread starter macrumors 6502

    cybrscot

    Joined:
    Dec 7, 2010
    Location:
    Somewhere in Southeast Asia
    #6
    I also just found out another way to do it, without the char. But similar to the way I tried to do it the first time. Here's the code. I guess in C there are many more than 1 way to skin a cat!

    Code:
    #include <stdio.h>
    
    main()
    {
    
    	int i1, i2, i3 ;
    	
    	
    	printf ("Enter a 3 digit number: ") ;
    	scanf ("%1d%1d%1d", &i1, &i2, &i3) ;
    	printf ("The reversal is: %d%d%d\n", i3, i2, i1) ;
    	
    	return 0 ;
    	
    }	







     
  7. balamw Moderator

    balamw

    Staff Member

    Joined:
    Aug 16, 2005
    Location:
    New England
    #7
    IMHO that's sloppy, but the author may have good intentions or reasons for it.

    int the default return type so it is assumed. When you
    Code:
    return 0;
    your function returns that as a type int. This value can then be used by other programs to know if your code completed properly or not.

    B
     
  8. mac2x macrumors 65816

    Joined:
    Sep 19, 2009
    #8
    You are using K.N. King, right? If so he covers this (in the second edition, which I assume you have), on pp 13-14. All programs he uses from the beginning use int main().

    Chapter 9.5 covers the return value of main in greater detail.
     
  9. dmi macrumors regular

    Joined:
    Dec 21, 2010
    #9
    Code:
    	printf ("Enter a two-digit number: ") ;
    	scanf ("%d", &digits) ;
    	printf ("The reversal is: %d%d\n",digits%10,digits/10 ) ;
    
     

Share This Page