GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 13-Oct-2005, 19:14
dave88 dave88 is offline
New Member
 
Join Date: Oct 2005
Location: Glasgow, Scotland
Posts: 7
dave88 is on a distinguished road

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:
/*
Author : David K Smart
Date   : 22:00 13 october 2005

purpose.....
A simple program to convert farenheit to celsius
and vice versa
*/

#include <stdio.h>  //include the standard library 



int main(){
    
    float temp_temperature=0, farenheit=0, celsius=0; /*temp_temperature stores the temperature
                              before the scale has being determined*/
    char input_unit, junk;//stores the initial scale and output scale 
    
    //output the title of the program
    printf("The Temperature Convertor -- Farenheit to Celsius(and vice versa)\n\n");
    
    //output explanation of syntax
    printf("The program uses the syntax ##.##x \n"); 
    printf("(where x equals the first char of the INITIAL scale)\n");
    printf("For example 50f would tell the program to convert 50f to 50c\n\n");
    //prompt user for input
    printf("Enter the temperature to be converted :");
    //read input from keyboard and assign to 
    scanf("%f%c", &temp_temperature, &input_unit);

    /*check if the values are valid using an if-statement to 
    check if any of the inputs return zero, when zero is returned
    it means the vale of a variable is invalid*/
        if(temp_temperature==0 || input_unit==0 || input_unit != 'c' || 'f'){
                               
                               //display error message
                               printf("\n\n An error was encountered, was the "); 
                               printf("input correct? (syntax error) \n\n");
                            }else{//if the zero is not returned continue the program
    
    //check what the initial scale used was
    switch(input_unit){
                             case 'f':/*f = Farenheit, when the initial 
                             value is f the program assumes that the input 
                             temperature is to be calculated as celsius*/
                             
                             farenheit=temp_temperature;/*set farenheit to
                             temp_temperature, to make comparison easy*/
                                                         
                             celsius = (5)*(temp_temperature-32)/9;/*perform
                                     farenheit to celsius conversion*/
                             
                             //display results
                             printf("\n%0.2f Degrees Farenheit is %0.2f Degrees Celsius \n", farenheit, celsius);
                            
                             break;
                             
                             case 'c':/*c = celsius, when the initial
                             value is c the program assumesthat the input
                             temperature is to be calculated as farneheit*/
                             
                             celsius=temp_temperature;/*set celsius to 
                             temp_temperature, to make comparisons easy*/ 
                             
                             farenheit = (32)+(temp_temperature*9)/(5);/*perform
                                         celsius to farenheit conversion*/
                             
                             //display results
                             printf("\n%0.2f Degrees Celsius is %0.2f Degrees Farenheit \n", farenheit, celsius);
                             
                             break;
                             
                             }//end switch statement

    
   
}//end error-checking if-statement

//clean up unwanted return chars
do{
        /*just loop and use getchar() to 
        absorb unwanted return characters*/
      junk=getchar();
       }while (junk != '\n'); 
       
       getchar();
   

return 0;
}//end int main()


Thanks in advance.
  #2  
Old 14-Oct-2005, 07:42
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,793
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Is my program coded well?


Quote:
Originally Posted by dave88
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.


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:
The program uses the syntax ##.##x
(where x equals the first char of the INITIAL scale)
For example 50f would tell the program to convert 50f to 50c

Enter the temperature to be converted :50f


An error was encountered, was the input correct? (syntax error)

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  
Old 14-Oct-2005, 08:39
dave88 dave88 is offline
New Member
 
Join Date: Oct 2005
Location: Glasgow, Scotland
Posts: 7
dave88 is on a distinguished road

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:
/*
Author : David K Smart
Date   : 22:00 13 october 2005

purpose.....
A simple program to convert farenheit to celsius
and vice versa
*/

#include <stdio.h>  //include the standard library 



int main(){
    
    float temp_temperature=0, farenheit=0, celsius=0; /*temp_temperature stores the temperature
                              before the scale has being determined*/
    char input_unit, junk;//stores the initial scale and output scale 
    
    //output the title of the program
    printf("The Temperature Convertor -- Farenheit to Celsius(and vice versa)\n\n");
    
    //output explanation of syntax
    printf("The program uses the syntax ##.##x \n"); 
    printf("(where x equals the first char of the INITIAL scale)\n");
    printf("For example 50f would tell the program to convert 50f to 50c\n\n");
    //prompt user for input
    printf("Enter the temperature to be converted :");
    //read input from keyboard and assign to 
    scanf("%f%c", &temp_temperature, &input_unit);

    /*check if the values are valid using an if-statement to 
    check if any of the inputs return zero, when zero is returned
    it means the vale of a variable is invalid*/
        if(temp_temperature==0 || input_unit==0 ){
                               
                               //display error message
                               printf("\n\n An error was encountered, was the "); 
                               printf("input correct? (syntax error) \n\n");
                            }else{//if the zero is not returned continue the program
    
    //check what the initial scale used was
    switch(input_unit){
                             case 'f':/*f = Farenheit, when the initial 
                             value is f the program assumes that the input 
                             temperature is to be calculated as celsius*/
                             
                             farenheit=temp_temperature;/*set farenheit to
                             temp_temperature, to make comparison easy*/
                                                         
                             celsius = (5)*(temp_temperature-32)/9;/*perform
                                     farenheit to celsius conversion*/
                             
                             //display results
                             printf("\n%0.2f Degrees Farenheit is %0.2f Degrees Celsius \n", farenheit, celsius);
                            
                             break;
                             
                             case 'c':/*c = celsius, when the initial
                             value is c the program assumesthat the input
                             temperature is to be calculated as farneheit*/
                             
                             celsius=temp_temperature;/*set celsius to 
                             temp_temperature, to make comparisons easy*/ 
                             
                             farenheit = (32)+(temp_temperature*9)/(5);/*perform
                                         celsius to farenheit conversion*/
                             
                             //display results
                             printf("\n%0.2f Degrees Celsius is %0.2f Degrees Farenheit \n", farenheit, celsius);
                             
                             break;
                             
                             }//end switch statement

    
   
}//end error-checking if-statement

//clean up unwanted return chars
do{
        /*just loop and use getchar() to 
        absorb unwanted return characters*/
      junk=getchar();
       }while (junk != '\n'); 
       
       getchar();
   

return 0;
}//end int main()

the error was here...
CPP / C++ / C Code:
 if(temp_temperature==0 || input_unit==0 ){
I had one more line checking that input_unit was only equal to c or f, obviously didn't work as planned!

Thanks for looking and replying, really appreciate it.
  #4  
Old 14-Oct-2005, 08:54
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,793
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Is my program coded well?


Quote:
Originally Posted by dave88
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


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:
    if (scanf("%f%c", &temp_temperature, &input_unit) != 2) {
      printf("Illegal input value\n");
      return (0); /* or do something to try to recover and continue */
    }


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:
Enter the temperature to be converted :50c

122.00 Degrees Celsius is 50.00 Degrees Farenheit

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:
For example 50f would tell the program to convert 50f to 50c
Isn't it true that a user input of "50f" would tell the program to convert 50 degrees Fahrenheit to the equivalent value in Celcius (which is not 50c).

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:
Enter the temperature to be converted :0c


An error was encountered, was the input correct? (syntax error)

(Same error report for 0f.)

Regards,

Dave
  #5  
Old 14-Oct-2005, 09:44
dave88 dave88 is offline
New Member
 
Join Date: Oct 2005
Location: Glasgow, Scotland
Posts: 7
dave88 is on a distinguished road

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:
/*
Author : David K Smart
Date   : 22:00 13 october 2005

purpose.....
A simple program to convert farenheit to celsius
and vice versa
*/

#include <stdio.h>  //include the standard library 


int main(){
    
    float temp_temperature=0, farenheit=0, celsius=0; /*temp_temperature stores the temperature
                              before the scale has being determined*/
    char input_unit, junk;//stores the initial scale and junk disposes of unwanted return chars
    
    //output the title of the program
    printf("The Temperature Convertor -- Farenheit to Celsius(and vice versa)\n\n");
    
    //output explanation of syntax
    printf("The program uses the syntax ##.##x \n"); 
    printf("(where x equals the first char of the INITIAL scale)\n");
    printf("For example 50f would tell the program to convert 50f to 50c\n\n");
    //prompt user for input
    printf("Enter the temperature to be converted :");
    
    /*read input from keyboard and assign to temp_temperature then 
    check if the values are valid using an if-statement to 
    check if any of the inputs don't return 2, when 2 isn't returned
    it means the vale of a variable is invalid*/
        if(scanf("%f%c", &temp_temperature, &input_unit) != 2){
                               
                               //display error message
                               printf("\n\n An error was encountered, was the "); 
                               printf("input correct? (syntax error) \n\n");
                               
                               //clean up 
                               do{
                               /*just loop and use getchar() to 
                                absorb unwanted return characters*/
                                junk=getchar();
                                }while (junk != '\n');
                               getchar();
                               return 0;
                            }else{//if the zero is not returned continue the program
    
    //check what the initial scale used was
    switch(input_unit){
                             case 'f':
                             case 'F' :  /*f = Farenheit, when the initial   
                                  value is f the program assumes that the input 
                                  temperature is to be calculated as celsius*/
                             
                             farenheit=temp_temperature;/*set farenheit to
                             temp_temperature, to make comparison easy*/
                                                         
                             celsius = (5)*(temp_temperature-32)/9;/*perform
                                     farenheit to celsius conversion*/
                             
                             //display results
                             printf("\n%0.2f Degrees Farenheit is %0.2f Degrees Celsius \n", farenheit, celsius);
                            
                             break;
                             
                             case 'c':
                             case 'C':/*c = celsius, when the initial
                             value is c the program assumesthat the input
                             temperature is to be calculated as farneheit*/
                             
                             celsius=temp_temperature;/*set celsius to 
                             temp_temperature, to make comparisons easy*/ 
                             
                             farenheit = (32)+(temp_temperature*9)/(5);/*perform
                                         celsius to farenheit conversion*/
                             
                             //display results
                             printf("\n%0.2f Degrees Celsius is %0.2f Degrees Farenheit \n", celsius, farenheit);
                             
                             break;
                             
                             default:
                                     printf("\nThat unit is not supported \n");
                                     break;
                             
                             }//end switch statement

    
   
}//end error-checking if-statement

//clean up unwanted return chars
do{
        /*just loop and use getchar() to 
        absorb unwanted return characters*/
      junk=getchar();
       }while (junk != '\n'); 
       
       getchar();
   

return 0;
}//end int main()

Thanks again, your advice will be be usefull.
  #6  
Old 14-Oct-2005, 10:11
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,793
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Is my program coded well?


Quote:
Originally Posted by dave88
Thanks again

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  
Old 14-Oct-2005, 10:18
dave88 dave88 is offline
New Member
 
Join Date: Oct 2005
Location: Glasgow, Scotland
Posts: 7
dave88 is on a distinguished road

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 GIDBlogUS Elections and the ?Voter?s Responsibility? by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

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

All times are GMT -6. The time now is 10:18.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.