![]() |
|
#1
|
|||
|
|||
Is my program coded well?Hi, I just wrote a program in c which converts farenheit to celsius and vice versa it does works but I was hoping someone would look at the source code and tell me what they think. I'm mainly talking about the way the source code is written and the commenting, but views on the coding is welcome.
heres the source code CPP / C++ / C Code:
Thanks in advance. |
|
#2
|
|||
|
|||
Re: Is my program coded well?Quote:
I'm not sure exactly what you are asking. When you say "... the way the source code is written" do you mean the indentation style? That's a personal preference, and although I wouldn't do everything exactly the way that you did (that's just me, remember), I applaud the fact that you are thinking about such things as style. No doubt your personal style will be refined as you look at more of your programs and other programs in books and on the web. As important as I believe style to be, I think functionality is also vital. Did you try your program? I compiled (no compiler warnings or errors --- congratulations on that) and executed the code. When I entered 50f as the "usage" message suggested, here's what I got: Quote:
You didn't ask for debugging advice, so I won't give any at this time. I do think that a program isn't a program until it is tested. You can't prove a program works just by testing, but you can find lots of bugs by testing, and you must test. (Test early, test often.) Regards, Dave Last edited by davekw7x : 14-Oct-2005 at 08:36.
|
|
#3
|
|||
|
|||
Re: Is my program coded well?Thanks for looking, the error is caused by a last minute change I made (doh!).
I'm mainly looking for advice on making my code readable and using 'proper coding', I read that you shoudnt use scanf for float's but instead you should use fgets then sscanf because its more accurate. heres my program fixed CPP / C++ / C Code:
the error was here... CPP / C++ / C Code:
Thanks for looking and replying, really appreciate it. |
|
#4
|
||||
|
||||
Re: Is my program coded well?Quote:
OK. Good work. This is a very good effort. You may not realize it but there are several (maybe several dozen) people who will read this thread, but will never be so brave as to actually post something. You put it out there and asked for criticism. That's super. I want you to know that just because someone actually gives criticism, that doesn't mean that your program is bad. It is a very good effort. However (and there's always that "however..."): 1. (Style) I have no problem with using scanf for floats. Whether you use scanf or fgets with sscanf, you should check the return value of scanf (or sscanf) to see that the entry was valid. Maybe something like this: CPP / C++ / C Code:
2. (Functionality/style) Now, I know that you haven't asked for a critique on the actual program, but I just can't let you think that I think it's OK because I like your style. I hate to repeat myself, but a program is not a program until it has been tested. Functionality is vital. Did you test your program with various inputs? What happens when you enter a Celcius value: Quote:
A "minor" error in the printf statement, perhaps? Remember the user isn't going to see or appreciate all of the neat things you do inside your program; all he/she will see is the (wrong, in this case) answer. Oh, yeah, on that same note, you misspelled Fahrenheit here and in the opening message. And your opening message is not very good: Quote:
3. (Style and limited functionality) If you use the test on scanf() that I suggested above your program can handle conversions from zero instead of bailing out. Here's what I got when I told it to convert zero C to Fahrenheit: Quote:
(Same error report for 0f.) Regards, Dave |
|
#5
|
|||
|
|||
Re: Is my program coded well?I'll remeber this "but a program is not a program until it has been tested. Functionality is vital."
It was the printf statement, another last minute change! heres the fixed code if you want to look at it CPP / C++ / C Code:
Thanks again, your advice will be be usefull. |
|
#6
|
|||
|
|||
Re: Is my program coded well?Quote:
If you are happy, I am happy (actually I am really happy). I have one more piece of advice: Save this program somewhere so that you can come back and look at it again in about, say, six months (assuming you are still interested in programming six months from now). Then pretend that someone else wrote the program and asked for your critique. The reason that I suggest this is that I have seen a lot of programmers looking at someone else's code and saying things like, "Look at this junk! What was he/she thinking? What crap!" I look at some of my old stuff and think, "Look at this junk! What was I thinking? ..." (You get the idea.) I do this from time to time with my programs and with my hardware designs and my documentation, and I am amazed at some of the things that I did six months ago. (Heck, sometimes I am amazed at some of the things that I did last Tuesday.) Everything that you experience in life results in new connections between the little gray cells in your brain (unless, of course, one of the experiences is death). That's what defines you, you know, not the collections of chemicals that make up your physical self --- its those connections. I am not the same person I was six months ago. I hope that some of my recent life experience has helped me to be a better programmer (who knows, maybe even a better person). Anyhow, I find this a useful exercise. Regards, Dave |
|
#7
|
|||
|
|||
Re: Is my program coded well?What you've said really gets me thinking, this is advice I'll be sure take!
not to sound like a broken record, but thanks agian. |
Recent GIDBlog
US Elections and the ?Voter?s Responsibility? by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Airport Log program using 3D linked List : problem reading from file | batrsau | C Programming Language | 11 | 29-Feb-2008 08:44 |
| Type casts ? | kai85 | C++ Forum | 12 | 23-Jun-2005 13:04 |
| [TUTORIAL] Calling an external program in C (Linux) | dsmith | C Programming Language | 4 | 22-Apr-2005 14:30 |
| fltk-2.0 cvs | Plumb | FLTK Forum | 20 | 13-Nov-2004 08:10 |
| Need help with a C program (Long) | McFury | C Programming Language | 3 | 29-Apr-2004 21:06 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The