Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.
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.

Got it, so basic haskell programming is mac programming, nice.

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!!

Not a newbie, where did you get that? No doubt you need to learn at leat a bit of C to code for MacOS (at least in the "app" sence), basic syntax, pointers, memory managment... But what I was trying to tell you is that your question can be related to any OS and is a pretty general C programming question, you would get better help on a specific c forum. Seems I was wrong, you got the same help here :) btw, you know a OO programming language right?
 
Not a newbie, where did you get that?
It's under your username at the left of your post. cybrscot has been participating in the forums here at MR longer than you and is now a "regular".

Background: cybrscot is working his way through KN King's book on C until he feels comfortable enough to move back to Objective-C, so no he does not know any OO languages yet.

B
 
Ops, ofc, a MR newbie. I don't post much.
No worries, and I should have phrased that as "has been participating more actively" since you have actually been here longer in calendar time.

One of the great things I like about MR is that you can actually get help installing Linux in the "Windows on Mac" forum and find discussions on anything from Logo to Haskell via C and Pascal in the "Mac Programming" forum.

If the mods decide to focus exclusively on Objective-C/AppleScript/Dashcode then we'll do that instead.

B
 
It's doing exactly what you are telling it to. The line in the program:
Code:
		printf ("You Entered the Number %d\n", number) ;

should probably be:
Code:
		printf ("You Entered the Number ") ;

and should probably be placed before any of the other printf calls.

Okay, I'm back from last night folks!! Finally finished with this project. I stepped away from the programming for 14 hours to take a breather. Sometimes it's good just to step back and take some time away. Just came back to it 30 minutes ago, made some quick changes, and it's working! So happy!! Definitely couldn't have done it without all your help. I was having one problem with numbers like 20, 30, 40, 50, etc because there is no "new line, \n" in the first number cases because I didn't want the first word and second word to be on different lines, such as
forty-
five.
But then without the need for a second word (like 20, 30 etc), it was printing 40, as forty but with no new line character it was being pushed up against my next command line in terminal. So honestly, with a clear head, I thought about it for about 1 minute and decided to try to add a case 0 in the second number switch with the printf ("\n") ; AND IT WORKED!!! Yippee!!
Not to brag at all, because I'm not yet good at this, but that was purely an instinctive idea and I'm glad my mind is working!

Programming is so far one of the most challenging things I've done. It's like a very difficult puzzle or riddle, requires a lot of thought. It can be soooooo frustrating when one can't find the answer on his own and yet everybody else can whip it together in like 4 minutes. At the same time, because of the tremendous difficulty sometimes and the amount of time invested, it feels sooooo good when you finally get it right and it works, and your finished! So pardon the rambling, but after these marathon evenings, I'm on cloud 9 once I finally get it!!

Programming at this stage in my development makes macroeconomics and finance look easy

Thanks again everybody, now on to Chapter 6, loops/iteration statements. This should be fun!! See you all again when I get to the exercises at the back of the chapter, should give you all a few days rest ! ;) Literally, you are all playing an important role in helping me to understand and learn this subject and I appreciate it very much. You folks are great with responding quickly and in a detailed manner, I couldn't ask for more. Although it's funny sometimes to see the thread take on a life of it's own well past my question being answered as everybody debates the subtleties and nuances of their interpretation of the C language. It's certainly a matter of style in my opinion as I've seen there are many ways to do the same thing. Here is the final working code if anybody's interested, thanks!

Code:
#include <stdio.h>

main () 
{

	int first_number, second_number, number ;
	
	
	printf ("Enter a two digit number: ") ;
	scanf ("%d", &number) ;
	
	printf ("You Entered the Number: ") ;
	
	
	if (number == 11) {
		printf ("eleven\n") ;
		return 0 ;
		}
    else if (number == 12) {
		printf ("twelve\n") ;
		return 0 ;
		}
	else if (number == 13) {
		printf ("thirteen\n") ;
		return 0 ;
		}
	else if (number == 14) {
		printf ("fourteen\n") ;
		return 0 ;
		}
	else if (number == 15) {
		printf ("fifteen\n") ;
		return 0 ;
		}
	else if (number == 16) {
		printf ("sixteen\n") ;
		return 0 ;
		}
	else if (number == 17) {
		printf ("seventeen\n") ;
		return 0 ;
	    }
	else if (number == 18) {
		printf ("eighteen\n") ;
		return 0 ;
		}
	else if (number == 19) {
		printf ("nineteen\n") ;
		return 0 ;
		}
		
		
		
		
		
		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;
		case 0: printf ("\n"); break;
		}
		
		
	
	
	return 0 ;
	
	}
 
What about 10? What about if the user disobeys and enters 2103? Why not pull the \n out of the 10 cases for your switch statement and just print it unconditionally afterwards (and then you can delete your 0 case)?
Also 09 is a perfectly valid two digit number. (zero is a digit, nine is a digit). This code will output "-nine".

B
 
@The OP: In response to your thread title: "Darn! Can I figure out how to do just one program without needing help!!!!!!!"

You'll get there :) It takes awhile. And even when you think you are to a point where not much help is needed you'll end up needing more help. Its just how programming is.

I've programmed off and on for years and still find myself struggling in my 3D graphics programming class where we are learning OpenGL and Direct X (at the same time :eek:). It takes me almost the entire week to do the labs and I'm usually up late on Sundays trying to get them working correctly. (Luckily though I still am holding on with a 94%)

I know though with time I'll grasp this stuff since its like that with many new things in programming. It just comes with time.
 
@The OP: In response to your thread title: "Darn! Can I figure out how to do just one program without needing help!!!!!!!"

You'll get there :) It takes awhile. And even when you think you are to a point where not much help is needed you'll end up needing more help. Its just how programming is.

I've programmed off and on for years and still find myself struggling in my 3D graphics programming class where we are learning OpenGL and Direct X (at the same time :eek:). It takes me almost the entire week to do the labs and I'm usually up late on Sundays trying to get them working correctly. (Luckily though I still am holding on with a 94%)

I know though with time I'll grasp this stuff since its like that with many new things in programming. It just comes with time.
I agree. I've talked with some people who don't know how much complexity a GUI and other tools hide. Programming is one of the most difficult skills I know of, too. I've been programming for years, but I still feel as though I'm a beginner. Some professors tell me that I write "damn good code" in Pascal. But my programs always disappoint me. As B says, we're always looking at the question mark in the flowchart about how to write good code.
 
It takes time to think in a programming language. I wrote my first program in college in 1968. I can pretty much write code as fast as I can type and almost do it in my sleep. :)
 
Also 09 is a perfectly valid two digit number. (zero is a digit, nine is a digit). This code will output "-nine".

B

Actually the code will work fine, since scanf is reading the input as %d (integer as signed decimal). Any preceding zeros are ignored, thus 01==001==1==000001.
 
Actually the code will work fine, since scanf is reading the input as %d (integer as signed decimal). Any preceding zeros are ignored, thus 01==001==1==000001.

The input is accepted alright, it's the output I'm concerned about:
Code:
$ ./deleteme 
Enter a two digit number: 09
You Entered the Number: [B]-[/B]nine

(The dash should be with the tens words).

B
 
What about 10? What about if the user disobeys and enters 2103? Why not pull the \n out of the 10 cases for your switch statement and just print it unconditionally afterwards (and then you can delete your 0 case)?

-Lee

Hey Lee, Yeah, my book says don't forget about 11-19 special cases, but you're right I also need one for 10 so I added an "if" stmt for 10, that's done.

What do you mean and why do you mean it when you say pull out the \n in the 10 cases and print it unconditionally afterwards? My \n is there so my program advances to the next line. If I take it out, My output from the program will be bunched up next to the new command line prompt in my terminal, looking pretty nasty. I'm sure you aren't suggesting to do that, but if I remove the \n what's the alternative? How do I print if unconditionally afterwards?

Would you mind giving me just a little snippet of example code in this case if you have the time so I can see and learn from what your talking about?

Thanks
 
Here is my almost finished code again.


Code:
#include <stdio.h>

main () 
{

	int first_number, second_number, number ;
	
	
	printf ("Enter a two digit number: ") ;
	scanf ("%d", &number) ;
	
	printf ("You Entered the Number: ") ;
	
	
	if (number == 10) {
		printf ("ten\n") ;
		return 0 ;
		}
	if (number == 11) {
		printf ("eleven\n") ;
		return 0 ;
		}
    else if (number == 12) {
		printf ("twelve\n") ;
		return 0 ;
		}
	else if (number == 13) {
		printf ("thirteen\n") ;
		return 0 ;
		}
	else if (number == 14) {
		printf ("fourteen\n") ;
		return 0 ;
		}
	else if (number == 15) {
		printf ("fifteen\n") ;
		return 0 ;
		}
	else if (number == 16) {
		printf ("sixteen\n") ;
		return 0 ;
		}
	else if (number == 17) {
		printf ("seventeen\n") ;
		return 0 ;
	    }
	else if (number == 18) {
		printf ("eighteen\n") ;
		return 0 ;
		}
	else if (number == 19) {
		printf ("nineteen\n") ;
		return 0 ;
		}
		
		
		
		
		
		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;
		case 0: printf ("\n"); break;
		}
		
		
	
	
	return 0 ;
	
	}
 
Would you mind giving me just a little snippet of example code in this case if you have the time so I can see and learn from what your talking about?

Thanks

I believe Lee is saying you should pull out all of the line breaks in your switch statements and replace them with a single

printf ("\n");

just before you return at the end of your program.

EDIT: A couple of more things, if you wrap your switch statements in an else statement, you can get rid of all the return statements in your if-else if statements. I really like seeing only 1 return statement if possible. I also agree with B that the dash should be with the tens words since 09 should be "nine" not "-nine"
 
Last edited:
Also 09 is a perfectly valid two digit number. (zero is a digit, nine is a digit). This code will output "-nine".

B

Yeah, How would I get rid of that - sign if someone types 09? I don't want -nine, I want nine. (Don't get me wrong, I think we accomplished what the book wanted, after all there example was 45, forty-five. And he didn't indicate it as a tricky or special case for a user input of 09......but it very well could happen. Just for the sake of learning how to do it, how could I modify this without drastically changing it? I need a - for all two digit cases above 20, excluding 30, 40, etc, etc. Is the only way, (based on what I currently know and in the spirit of this exercise) to make another group of "if" statements for 01 02 03 04, etc, etc??
 
Am I the only one who really doesn't like the ten extra "return 0;" statements this approach requires instead of a simple

Code:
if ((number >= 10 ) && (number <= 19)) {}
else {}

Your code as it stands still doesn't handle the cases "00"-"09" properly and anything < 0 or >99.

Just for the sake of learning how to do it, how could I modify this without drastically changing it?

I put it on the tens words in the version of the code I did earlier but even that isn't quite right since you only want it when you output both. (My code doesn't handle "90" properly).

B
 
I believe Lee is saying you should pull out all of the line breaks in your switch statements and replace them with a single

printf ("\n");

just before you return at the end of your program.

EDIT: A couple of more things, if you wrap your switch statements in an else statement, you can get rid of all the return statements in your if-else if statements. I really like seeing only 1 return statement if possible. I also agree with B that the dash should be with the tens words since 09 should be "nine" not "-nine"

Yes, but if the dash is with the tens words, then 20 will be twenty-, and 30 will be thirty- right?

Is another group of special cases "if, else" in need for all cases 01 to 09???
Thanks
 

Code:
		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;
		case 0: printf ("\n"); break;
		}

My suggestion, which naples98 was clarifying, was to change that into something like:

Code:
	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;
		}

	if(first_number != 0 && second_number != 0) {
		printf("-");
	}

	switch (second_number) {
		case 9: printf ("nine"); break;
		case 8: printf ("eight"); break;
		case 7: printf ("seven"); break;
		case 6: printf ("six"); break;
		case 5: printf ("five"); break;
		case 4: printf ("four"); break;
		case 3: printf ("three"); break;
		case 2: printf ("two"); break;
		case 1: printf ("one"); break;
		}
	printf("\n");

This also incorporates balmw's idea re: the - and single digit numbers.

Also, you might want to add some error checking as we have discussed previously. If the user enters -234 or 2114 the code won't work properly. You ask them to enter a 2 digit number. If they disobey (maybe you can be lenient about a 1 digit number) you should tell them so. Just check if what's entered is < 1 or > 99. You didn't specify the number must be positive, but obviously the code only handles positives right now.

-Lee
 
Am I the only one who really doesn't like the ten extra "return 0;" statements this approach requires instead of a simple

Code:
if ((number >= 10 ) && (number <= 19)) {}
else {}

Your code as it stands still doesn't handle the cases "00"-"09" properly and anything < 0 or >99.



I put it on the tens words in the version of the code I did earlier but even that isn't quite right since you only want it when you output both. (My code doesn't handle "90" properly).

B


I see your code snippet, but I don't understand it, sorry! How do I use something like that in my code? Do I write that 10 times? I don't understand what goes in the {} {} you have twice. Remember this exercise was my first using a combination of switches and if, else and I barely tip toed my way through it to get it compiling and working. So I've only done something like this "one way" which is "this way", the way you suggest would be another first for me, so forgive me for not understanding it. Please show me, and I will remember. (show me more)
 
Am I the only one who really doesn't like the ten extra "return 0;" statements this approach requires instead of a simple

B

I totally agree with you. I edited my earlier post to address the return statements. I always try to just have 1 return statement if possible. You are asking for trouble with multiple return statements and it is a lot more work to maintain.
 
I see your code snippet, but I don't understand it, sorry! How do I use something like that in my code? Do I write that 10 times? I don't understand what goes in the {} {} you have twice. Remember this exercise was my first using a combination of switches and if, else and I barely tip toed my way through it to get it compiling and working. So I've only done something like this "one way" which is "this way", the way you suggest would be another first for me, so forgive me for not understanding it. Please show me, and I will remember. (show me more)

In the first set of brackets you would put your big if-else if-else block that handles 10-19, in the second set of brackets you would put your "regular" handling of 1-9 and 20-99 (your switch statements). Then you can pull out the return 0; littered in your if-else if-else block.

-Lee
 
No offense targeting you but I know programmers like that. But the problem with their code is it usually requires a great deal of debugging!

It would if done without any thought, but with experience comes knowledge of programming paradigms that lead to well structured and easily maintained code. And when you have spent enough time clean up predecessors messes you realize the importance of tried and true techniques. There needs to be a reason behind every line of code and textbooks concentrate on the how and not the why. The best course I took way back in college was one on data structures, and we never wrote a single program. Once you understand the principles and they become engrained in what you do, the choice of language becomes pretty much irrelevant and the code tends to just work.

I feel the OP's frustrations will be overcome when he understands how programs work. Currently he's dealing with a pile of different statements with no direction. Eventually he will get that "ah ha" moment and it will all make sense, but it can take time.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.