Darn! Can I figure out how to do just one program without needing help!!!!!!

Discussion in 'Mac Programming' started by DoodleNoodle, Oct 25, 2011.

  1. DoodleNoodle, Oct 25, 2011
    Last edited by a moderator: Oct 25, 2011

    DoodleNoodle macrumors newbie

    Joined:
    Oct 25, 2011
    #1
    Book's Question:

    "Write a program that asks the user for a two digit number, then prints the English word for the number. HINT: Break the number into two digits. Use one switch statement to print the word for the first digit (twenty, thirty, etc). Use a second switch statement to print the word for the second digit. Don't forget that the numbers between 11 and 19 require special treatment (and damn those numbers say I!!!!!)"

    I've been working on this little program for about 3 hours straight now, so I'm getting ready to throw my computer out the window. The past 2.5 hours of which spent on the 11 - 19 "special treatment" numbers!!!! Arrgggghhh!!!

    Well either way, when I run my program, and enter a number (lets say 11) it displays;
    The number is 11
    & The number is Illegal entry-1

    I realize there are two things wrong, but at the moment I'm currently worried about the fact that both printf(blahblah) are being displayed. How do I display only one? Also any information/ideas on how to make something like 9b, b9, bb, or 05 an illegal entry Below is my program:

    Code:
    #include <stdio.h>
    main(void)
    {
    
    	int number, first_digit, second_digit;
    	
    	
    	printf ("Enter a two digit number: ") ;
    	scanf_s ("%d", &number) ;
    
    	first_digit=number/10;
    	second_digit = number-((first_digit)*10);
    	
    
    	printf ("the number is ");
    	if (number==11)
    		printf ("eleven\n") ;
    		
           else if (number == 12) 
    		printf ("twelve\n") ;
    		
    	else if (number == 13) 
    		printf ("thirteen\n") ;
    	
    	else if (number == 14) 
    		printf ("fourteen\n") ;
    		
    	else if (number == 15) 
    		printf ("fifteen\n") ;
    		
    	else if (number == 16) 
    		printf ("sixteen\n") ;
    		
    	else if (number == 17) 
    		printf ("seventeen\n") ;
    	    
    	else if (number == 18) 
    		printf ("eighteen\n") ;
    		
    	else if (number == 19) 
    		printf ("nineteen\n") ;
    	
    
    		printf("The number is: ");
    	switch (first_digit) {
    		case 9: printf ("ninety"); break;
    		case 8: printf ("eighty"); break;
    		case 7: printf ("seventy"); break;
    		case 6: printf ("sixty"); break;
    		case 5: printf ("fifty"); break;
    		case 4: printf ("forty"); break;
    		case 3: printf ("thirty"); break;
    		case 2: printf ("twenty"); break;
    		default: printf ("Illegal Entry"); break;
    		}
    		
    	switch (second_digit) {
    		case 9: printf ("-nine\n"); break;
    		case 8: printf ("-eight\n"); break;
    		case 7: printf ("-seven\n"); break;
    		case 6: printf ("-six\n"); break;
    		case 5: printf ("-five\n"); break;
    		case 4: printf ("-four\n"); break;
    		case 3: printf ("-three\n"); break;
    		case 2: printf ("-two\n"); break;
    		case 1: printf ("-one\n"); break;
    		case 0: break;
    		default: printf ("Illegal Entry"); break;
    	}
    	
    	
    	
    	return 0 ;
    }
     
  2. jiminaus macrumors 65816

    jiminaus

    Joined:
    Dec 16, 2010
    Location:
    Sydney
    #2
    Do you know your code? Do you know why it's printing two messages? If you don't, put a breakpoint on the line after scanf and trace through the execution of your program line by line. What do you observe happens when you enter different numbers?

    (BTW I'm not being difficult here by not directly giving you your answer. I'm attempting to show you how to debug, particularly how to reason about the behaviour of a program. This is a very important skill.)
     
  3. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #3
    You could add a final else case and put your switch in there, or you could add returns in all of your special cases.

    -Lee
     
  4. DoodleNoodle thread starter macrumors newbie

    Joined:
    Oct 25, 2011
    #5
    the code should be there, sorry it's not in a great format or to clear on this
     
  5. jiminaus macrumors 65816

    jiminaus

    Joined:
    Dec 16, 2010
    Location:
    Sydney
    #6
  6. DoodleNoodle thread starter macrumors newbie

    Joined:
    Oct 25, 2011
    #7
    aye i looked at his first, then copied/pasted his first bit since its the same question, sorry for lack of originality in that part.

    so how do I go about bypassing the first part if the number is greater than 19, like can I do something like if (number>19) go to the second part, then if(number<20) use the first part?
     
  7. jiminaus macrumors 65816

    jiminaus

    Joined:
    Dec 16, 2010
    Location:
    Sydney
    #8
    You could, but that's not the way to think of it. You should think of it as "if (number < 20) do the first part, else do the second part".
     
  8. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #9
    Actually, think of it as "if (number < 20) { first part } else { second part }". Seriously, the sooner you become familiar with the role of C's punctuation, the better.
     
  9. jiminaus macrumors 65816

    jiminaus

    Joined:
    Dec 16, 2010
    Location:
    Sydney
    #10
    I didn't want to give so much away. :p
     
  10. balamw Moderator

    balamw

    Staff Member

    Joined:
    Aug 16, 2005
    Location:
    New England
    #11
    Questions for you to consider:

    What constitutes a valid two digit number? Is zero a digit? Is "00" or "09" a valid two digit number? What about any non-digit input? (e.g. "ab").

    If not how do you deal with these?

    B
     
  11. DoodleNoodle thread starter macrumors newbie

    Joined:
    Oct 25, 2011
    #12
    Wow, thanks jiminaus and chown33, got it to work perfectly. Now tho, to address the second problem with a number out of the domain. the domain is basically 10-99. numbers like 04 aren't included in this assignment. I am not sure how to handle these tho. Right now (as posted in the code), I have the default, where the program replies illegal entry, but if the user inputs bb or 00, the output is Illegel Entry-Illegal Entry. or if the user inputs 9b, the output is 9Invalid Entry. *epiphany while typing this* can I just have a great statement of if (9<number && number<100) {everything i have} else {Invalid entry}??
     
  12. balamw Moderator

    balamw

    Staff Member

    Joined:
    Aug 16, 2005
    Location:
    New England
    #13
    You can do the bounds checking/input validation as you suggest.

    I think you may want to actually try your code with some of the invalid entries. As posted originally at least "00" should not give you two "Illegal Entry"s and "9b" should probably output something other than "9Illegal Entry". ;)

    Can you extend your code to work for numbers 00-99?

    B
     
  13. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #14
    Try it. See what happens. If you have problems, post your code.

    Many programming questions are best answered by applying the above. Because trying to answer the question yourself is at the heart of programming.
     
  14. DoodleNoodle thread starter macrumors newbie

    Joined:
    Oct 25, 2011
    #15
    Went on and tried it, worked beautifully. The project only asked for having numbers between 10 and 99 (ends included). Once again, thank you all for the advice, if I have future problems (hopefully aren't as trivial as this one) i'm glad to know y'all can help.
     

Share This Page