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

Discussion in 'Mac Programming' started by cybrscot, Feb 1, 2011.

  1. cybrscot, Feb 1, 2011
    Last edited: Feb 1, 2011

    cybrscot macrumors 6502

    cybrscot

    Joined:
    Dec 7, 2010
    Location:
    Somewhere in Southeast Asia
    #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!!!:eek::eek::eek::eek::eek::eek:

    My switch statements worked fine with numbers that were not 11 - 19, so I was comfortable with my code at that point, all was well. After I had that working, I decided to add the "if" statement block you see to handle numbers 11 -19. Please don't laugh at my "if" statements. I've written and rewritten them about 20 different ways, sometimes they would compile but produce the wrong output and sometimes they wouldn't compile at all. When they did compile, no matter what two digit number I was inputting, the output always was eleven. Why? I don't know. So anyway, the "if" statements are pretty much garbage, but this is just how I'm leaving them in there in case I'm somehow close.

    I thought it would be quite simple, and I followed my code example from last night to handle the special numbers. Like, if (number = 11) print ("eleven"), but every number was printing eleven. Any help would be great. My book doesn't have this one written out on it's website like some others.

    Below, I've tried if, and if else, and if else if, but maybe some brackets weren't in the right spot or something, after two hours just plain tired!!

    Code:
    #include <stdio.h>
    
    main () 
    {
    
    	int first_number, second_number, number ;
    	
    	
    	printf ("Enter a two digit number: ") ;
    	scanf ("%d", &number) ;
    	
    	
    	if (number = 11) {
    		printf ("eleven\n") ;
    		}
        else (number = 12) {
    		printf ("twelve\n") ;
    		}
    	else (number = 13) {
    		printf ("thirteen\n") ;
    		}
    	else (number = 14) {
    		printf ("fourteen\n") ;
    		}
    	else (number = 15) {
    		printf ("fifteen\n") ;
    		}
    	else (number = 16) {
    		printf ("sixteen\n") ;
    		}
    	else (number = 17) {
    		printf ("seventeen\n") ;
    	    }
    	else (number = 18) {
    		printf ("eighteen\n") ;
    		}
    	else (number = 19) {
    		printf ("nineteen\n") ;
    		}
    		
    		
    		
    		
    		printf ("You Entered the Number %d\n", number) ;
    		first_number = number / 10 ;
    	    second_number = number % 10 ;
    	
    	switch (first_number) {
    		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;
    		}
    		
    	switch (second_number) {
    		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;
    		}
    		
    		
    	
    	
    	return 0 ;
    	
    	}
     
  2. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #2
    = is assignment, and evaluates to the right operand. Anything non-zero is true. == tests equivalence of its operands.

    use:
    If
    Else if
    Else if
    Else
    (capitalization is my phone's fault)

    For the regular case, print the hyphen with the second digit so 20 doesn't print as twenty-.

    Add validation that a two digit number was entered.

    Add an if for the first digit being one. Move your 10-19 case there, and the two switches into the else.

    -Lee
     
  3. ulbador macrumors 68000

    ulbador

    Joined:
    Feb 11, 2010
    #3
    Make sure you declare and init everything. numbera?

    Also, I'm not sure how you are compiling everything, but with gcc you can add the switch "-Wall" to the compile. This will prevent "allowed" things such as you have:

    if (i = 1) //This is bad. It assigns the value 1 to i. It evaluates to TRUE every time. -Wall should prevent this
    if (i == 1) //This is ok
     
  4. cybrscot thread starter macrumors 6502

    cybrscot

    Joined:
    Dec 7, 2010
    Location:
    Somewhere in Southeast Asia
    #4
    Thanks, pay no attention to the numbera, I was experimenting with some different things and put an "a" at the end of number to differentiate it from number. It was declared as an int, but I deleted it, just forgot to change the numbera. But I knew the code wasn't working anyway, so I didn't go over it with a fine tooth comb. I was just hoping to get someone to point me in the right direction as to how to structure the code for the special case numbers 11 -19. But thanks for pointing it out anyway.
     
  5. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #5
    I tried to above:
    Code:
    if (number == 11) {
    } else if (number == 12) {
    
    } else {
    
    }
    ulbador also pointed out what I mentioned with = vs. ==.

    Do you have to use if-else if-else? A switch for 10-19 would work, too.
     
  6. cybrscot thread starter macrumors 6502

    cybrscot

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


    Answering from your phone? Lee, you da' man, thanks for taking the time! I'll restructure all that with if, else if, else if, else and see what happens. I like to take it one step at a time if the compiler will let me.

    I don't understand yet what you mean when you suggested....
    Add validation that a two digit number was entered. (not sure how to do that)

    Add an if for the first digit being one. Move your 10-19 case there, and the two switches into the else. (don't understand this either, which I think is of primary importance since it's the 11-19 case I'm trying to get correct, but I think you mean something like

    if (statement)
    else
    switch
    else
    switch,

    not sure how to write that yet, I'll unfortunately have to spend until 4am playing around with different combinations of how to write all this and see if I stumble upon the correct combination of code like I finally did last night.

    Unfortunately my book shows several different ways to do things, but sometimes I'm not sure which way to use. Further, the example in the book is a different problem, so then the exercises at the back are an entirely new type of problem, something that hasn't been shown before. Sure, he's shown how to write switches and breaks and if stamts, but under different circumstances with different problems/criteria/situations.
     
  7. Manty macrumors member

    Joined:
    Mar 18, 2008
    Location:
    Lisbon, Portugal
    #7
    Your question belongs on a C programming forum. I'm not saying that with a bad tone, but I follow this forum RSS feed because I am interested in Mac Programming. Good luck with your problem.
     
  8. cybrscot thread starter macrumors 6502

    cybrscot

    Joined:
    Dec 7, 2010
    Location:
    Somewhere in Southeast Asia
    #8
    No Lee, I don't think so, the book's question doesn't say that. I think I'm free to do whatever I know how to do, but just follow what the book does specifically ask. So he asked that we use two switches for the first digit and the second digit, so I want to follow that, (and I did it right), but the If stmt was my idea after using it last night successfully. Last night I needed a special case for number grades of 0 and 100, so we did something like if (a < 0 || a > 100), printf ("error");

    So I figured it should work nicely for numbers 11 -19, and I would just write an if...printf statement for each number, but it wasn't so easy.
     
  9. Cromulent macrumors 603

    Cromulent

    Joined:
    Oct 2, 2006
    Location:
    The Land of Hope and Glory
    #9
    This forum is a C programming forum. In fact it is a forum for any programming language that can target a Mac. Not every post here will be about Objective-C (and bloody boring it would be to if they were).

    Frankly your post if absurd. Get over it.
     
  10. cybrscot, Feb 1, 2011
    Last edited by a moderator: Feb 1, 2011

    cybrscot thread starter macrumors 6502

    cybrscot

    Joined:
    Dec 7, 2010
    Location:
    Somewhere in Southeast Asia
    #10
    I need to clear my head, so I'm going to watch a movie with my wife on the laptop, recharge MY batteries, get myself back to neutral, and come back to this thread with a clear and unfrustrated mind in a couple hours. Hoping it will all click then!!


    Hey I see you are a newbie, welcome. If you can help, feel free to do so. I see you don't intend a bad tone, and thank you for that. But C is a language that can be used with Mac, so is Cocoa, and Ruby, and a bunch of others. In fact, I want to write some applications for the IOS, but before I can jump to it, it was suggested I learn C first, which I will apply to objective c (hopefully in 9- 12 months), then I will learn Cocoa and put together apps for IOS. So it really is about Mac. This is where I want to be. These people know Macs, and I want to be in this environment so I can grow more knowledgable and comfortable when it comes time for the real thing. I'm also a new Mac user, bought this MB Pro in December, so the last 45 days have been hard on my brain!!
     
  11. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #11
    My thought was, in pseudo-code:
    if number greater than 99 or less than 10 (or 1 or 0? whatever you want)
    print error
    end if

    if number less than 20
    switch with cases for 10-19
    if you prefer, if/else if/else for 10-19
    else
    switch for digit 1
    switch for digit 2
    end if

    Sometimes the best remedy is to leave it alone. Often the hard problems solve themselves in your sleep, in the shower, in the car, etc.

    -Lee
     
  12. naples98 macrumors member

    naples98

    Joined:
    Sep 9, 2008
    Location:
    Houston
    #12
    I've been watching a lot of your posts and good luck with learning C. I originally learned C++ (quite a bit of C too helping out the engineering majors) and I think both are great languages to learn although not the easiest. My foundations in C++ helped me pick up Java very quickly and recently Python (although that took a major shift in my thinking).

    Lee always provides great advice and he touched on something here that I think needs to be pointed out. Pseudo-code is one of your best tools to use and I recommend solving the problem in Pseudo-code before you ever write a line of code.

    Programming can be frustrating and rewarding all at the same time but it is exceptionally frustrating if you don't know whether you have a flaw in your logic or an error in your code (i.e. using = instead of ==). Solving the problem in Pseudo-code allows you to focus on solving the problem first and figuring out what kind of techniques (i.e. switch statements, if-else statements, etc.) you are going to use and then focusing on writing your solution into the syntax of the language. Trying to do both at the same time is really, really hard (I speak from experience). Try separating the two.

    Finally, Lee is right, we all solve problems at the weirdest times and then you can't wait to get to the computer to write up your solution.

    Good luck and I'll help out when I can but you are in good hands.
     
  13. balamw Moderator

    balamw

    Staff Member

    Joined:
    Aug 16, 2005
    Location:
    New England
    #13
    Yup. My college programming classes required a flowchart or pseudo-code to be delivered with the code as a way of reinforcing this.

    This kind of thing is exactly why I was mentioning data verification in the other thread.

    First define the range of acceptable values. Discard anything else.

    Are there rules that cover most cases (yes for 1-9,20-99)?
    Are there exceptions that need to be handled separately? (yes 0,10-19).

    So you can think of it as a funnel.

    Define the largest range, then a smaller range, and a smaller range, etc...

    The wind speed and grade letter problem are both closely related to this one.

    B
     
  14. firewood macrumors 604

    Joined:
    Jul 29, 2003
    Location:
    Silicon Valley
    #14
    This is true for a lot of people. What it means for someone having trouble, they should pick a programming language closer to pseudocode for them to first learn. Python or Squeak maybe instead of C.
     
  15. notjustjay macrumors 603

    notjustjay

    Joined:
    Sep 19, 2003
    Location:
    Canada, eh?
    #15
    Absolutely.

    The real trick about programming is not learning the specific language but learning about "how to think like a computer". How to break down big problems into little ones, and how to solve those problems within the confines of the operators and data structures you have available.

    Virtually all modern programming languages offer the same constructs. Some have clever twists and others do some things better than others, but for the most part, it's all the same. Once you've got a problem broken down into a block of pseudocode, it's fairly easy to translate it into statements in C, C++, Objective C, Java, Python, Ada, or whatever.

    But the trick is getting to the pseudocode. Often when I have a tricky algorithm to implement, I'll start a comment block and write out the pseudocode in as much detail as I can. Then I break up the comments and start writing the real code underneath. As a bonus, the algorithm is already documented.

    cybrscot, I recommend you give that approach a try. Write out complete, detailed pseudocode that explains exactly what your program will do, step by step. THEN implement it in code.

    Eventually once you get the "thinking like a compiler" mindset down, you can code-as-you-type, but until then, you need to think very carefully about everything, and writing it out in advance keeps you from getting confused or solving the wrong problems.
     
  16. McGordon macrumors member

    Joined:
    Dec 28, 2010
    Location:
    Scotland
    #16
    You've probably got your program working now with the above help, but I'd just like to give some advice about what to do when you're stuck. The pseudo code ideas are good, but even if you'd done that, you could have still ended up stuck and pretty frustrated when you didn't realise what was going on when you were using assignment (=) instead of equality test (==).

    When things are going bad, try making a really simple test case. Get rid of all those else-if statements. Comment them out or cut and paste them into another file to keep them safe, so you can bring them back later. Remove the unknown quantity of the scanf statement. Comment it out and assign the number to a fixed value for testing. Something like this:

    Code:
    //	Commented out scanf and assigning number instead
    //	scanf ("%d", &number) ;
    	number = 12;
    	
    	
    	if (number = 11) {
    		printf ("eleven\n") ;
    		}
    /*  Comment out all these else's (which should be else-if)
        else (number = 12) {
    		printf ("twelve\n") ;
    		}
    	else (number = 13) {
    		printf ("thirteen\n") ;
    		}
    	else (number = 14) {
    		printf ("fourteen\n") ;
    		}
    	else (number = 15) {
    		printf ("fifteen\n") ;
    		}
    	else (number = 16) {
    		printf ("sixteen\n") ;
    		}
    	else (number = 17) {
    		printf ("seventeen\n") ;
    	    }
    	else (number = 18) {
    		printf ("eighteen\n") ;
    		}
    	else (number = 19) {
    		printf ("nineteen\n") ;
    		}
    */
    
    
    You'd see that number was definitely set to 12, but still prints "eleven" so you'd know there was something wrong with the 'if' statement logic. If you really don't believe that number is 12, put a printf statement before the 'if' statement to print its value. You could do something like this in a debugger too, but I don't think you have covered that yet.

    For a two digit number, I'd say 10-19 would be a special case, not just 11 to 19. Did the book say 11-19?

    He's programming in C and he's doing it on a Mac. What's the problem? Even in C, I think he asked Mac specific questions about getting set up to edit, compile and run programs on a Mac.
     
  17. balamw Moderator

    balamw

    Staff Member

    Joined:
    Aug 16, 2005
    Location:
    New England
    #17
    We're not picky, apparently we'll even talk about Arduino code and sometimes even answer stuff about other languages and platforms.

    The next major step to thinking like a programmer is dealing with the unexpected (usually created by the problem that exists between the keyboard and chair). Anticipating that the user can (and will) do things with and to your code other than what you want them to do.

    Data validation like what is being asked for here is one key technique to avoiding the unexpected.

    B
     
  18. SidBala macrumors 6502a

    Joined:
    Jun 27, 2010
    #18
    Your post belongs in a trash can. I'm not saying that with a bad tone.

    C is one of the very many languages that can be used on a Mac.
     
  19. talmy macrumors 601

    talmy

    Joined:
    Oct 26, 2009
    Location:
    Oregon
  20. talmy macrumors 601

    talmy

    Joined:
    Oct 26, 2009
    Location:
    Oregon
    #20
    How about nested switch statements?


    Code:
    switch (number/10) {
        case 0: break;
        case 1: switch (number % 10) {
                        case 0: printf("ten\n"); break;
                        case 1: printf("eleven\n"); break;
                        ...
                    }
                    return; 
        case 2: printf("twenty-"); break;
        ...
    }
    switch (number % 10) {
        case 0: printf("\n"); break;
        case 1: printf("one\n"); break;
        ...
    }
    
     
  21. cybrscot thread starter macrumors 6502

    cybrscot

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

    Yeah, the book says 11 - 19, Thanks for the idea of commenting out. I do like to reduce my code to a minimum to troubleshoot, such as one statement, etc. and see if it works, then if it does, continue. I copied and pasted all those if stmts into another editor to try something, then put them back (so I don't have to re-type everything. But I never thought of commenting out the code, that's a great idea and quick and easy too.

    And no I haven't fixed my code yet. I've been avoiding coming back to it! I just know I can't get it done quickly/correctly the first time, and I'm afraid I'm going to get stuck playing with it for 3 hours again with no luck!! Thus, frustrate me more. I really don't think that easy exercises such as these should take me 6-7 hours each time, thats ridiculous. It takes me more than a week to get through the exercises at the end of the chapter. I want to move through this book more quickly, but I keep getting hung up on stupid stuff that doesn't present a solution to me. It seemed so simple to test if the variable was == to something, then print, but NOOOOO, it can't be that easy. Then I start moving around brackets, add them, delete them, do else if, or just else, or just printf, and a bunch of stuff to see what works. That's why it takes soooooo long!

    I wrote all the code with the switch stmts, minus the "if" stmts, in like 10 minutes, and it worked! I was happy, but then trying to figure out how to handle the 11-19 completely threw me into hours of wasted time!!!
     
  22. balamw Moderator

    balamw

    Staff Member

    Joined:
    Aug 16, 2005
    Location:
    New England
    #22
    Looks like that will yield elevenone, No?

    B
     
  23. slashlos macrumors member

    slashlos

    Joined:
    Aug 14, 2006
    Location:
    These United States
    #23
    Let the formatter do it...

    given a number in 'number'...



    NSNumberFormatter * wordFormat =
    [[[NSNumberFormatter alloc] init] autorelease];
    [wordFormat setNumberStyle:NSNumberFormatterSpellOutStyle];

    That create a format to spell out the number.

    NSString * words = [wordFormat stringFromNumber:
    [NSNumber numberWithInt:number]];

    That creates a 'string' with the words formatter.

    NSLog(@"You're number is %@", words);

    That logs it to the console, or

    printf("%.*s", [words length], [words UTF8String]);
     
  24. talmy macrumors 601

    talmy

    Joined:
    Oct 26, 2009
    Location:
    Oregon
    #24
    No, because there is a "return" in there.
     
  25. talmy macrumors 601

    talmy

    Joined:
    Oct 26, 2009
    Location:
    Oregon
    #25
    However the OP is starting out learning C, not Objective-C.
     

Share This Page