Quantcast

Maximum PC

It is currently Fri Sep 19, 2014 7:28 am

All times are UTC - 8 hours




Post new topic Reply to topic  [ 10 posts ] 
Author Message
 Post subject: Temperature program
PostPosted: Fri Sep 25, 2009 11:01 am 
8086
8086

Joined: Wed Sep 23, 2009 2:41 am
Posts: 12
Hey guys, this is my literal first program ever written without using my reference book or any other help. Just wondering if anyone could give some constructive criticism on it.


#include <iostream>
int celtofahr(int);


int main()
{
using namespace std;
int cel;
cout << "Please enter the temperature in Celcius: " << endl;
cin >> cel;
int fahr = celtofahr(cel);
cout << cel << " degrees in Celcius = " << fahr << " degrees in Fahrenheit." << endl;
return 0;
}

int celtofahr(int ctf)
{
return 1.8 * ctf + 32;
}


Any comments would be helpful. Thank!


Top
  Profile  
 
 Post subject:
PostPosted: Fri Sep 25, 2009 11:49 am 
Java Junkie
Java Junkie
User avatar

Joined: Mon Jun 14, 2004 10:23 am
Posts: 24224
Location: Granite Heaven
Please use the [ code] tags when posting code.

Eg:
Code:
#include <iostream>
int celtofahr(int);


int main()
{
using namespace std;
int cel;
cout << "Please enter the temperature in Celcius: " << endl;
cin >> cel;
int fahr = celtofahr(cel);
cout << cel << " degrees in Celcius = " << fahr << " degrees in Fahrenheit." << endl;
return 0;
}

int celtofahr(int ctf)
{
return 1.8 * ctf + 32;
}

Otherwise, your code looks like a standard little program written for an Intro to Programming course. If it works, thumbs up!


Top
  Profile  
 
 Post subject:
PostPosted: Fri Sep 25, 2009 3:04 pm 
8086
8086

Joined: Sun Jan 06, 2008 2:40 pm
Posts: 37
Location: Massachusetts
yea, looks good. The only thing I can think of is your naming scheme, Everybody has their own, but I personally don't use abbreviations, i think it looks more professional.

But if it works for you, good job!


Top
  Profile  
 
 Post subject: Re: Temperature program
PostPosted: Fri Sep 25, 2009 7:38 pm 
Willamette
Willamette
User avatar

Joined: Fri Jul 06, 2007 9:29 am
Posts: 1447
jnich91 wrote:
Just wondering if anyone could give some constructive criticism on it.


Let's say you take a thermometer out of your mouth and it says 37 degrees Celsius. You convert it to Fahrenheit, is the temperature accurate? Or are you sick/ill?


Last edited by jcollins on Sat Sep 26, 2009 8:54 am, edited 1 time in total.

Top
  Profile  
 
 Post subject:
PostPosted: Fri Sep 25, 2009 7:54 pm 
Java Junkie
Java Junkie
User avatar

Joined: Mon Jun 14, 2004 10:23 am
Posts: 24224
Location: Granite Heaven
Shutout makes a good point. I see no reason to abbreviate any names in a program and many reasons not to do so. Now that we can autocomplete any name, it makes no sense to use a name like celtofahr when celciusToFahrenheit is possible.


Top
  Profile  
 
 Post subject:
PostPosted: Sat Sep 26, 2009 4:06 am 
8086
8086

Joined: Wed Sep 23, 2009 2:41 am
Posts: 12
Good point. I'm not sure why I used an abbreviation, after shoutout mentioned that I remembered that C++ imposes no limit on the length of a variable name. I'll remember that next time, thanks guys.


Top
  Profile  
 
 Post subject:
PostPosted: Sun Sep 27, 2009 7:01 am 
8086
8086

Joined: Wed Sep 23, 2009 2:41 am
Posts: 12
So I kept everyones advice in mind while completing the last project I had. It's a program to calculate your Body Mass Index based on your height and weight. Again any critism is good. Thanks in advance.


Code:
#include <iostream>
#include <cmath>

int main()
{
    using namespace std;

    cout << "Please enter your height:______\b\b\b\b\b\b";
    int heightInInches;
    cin >> heightInInches;
   
    cout << "Please enter your weight in pounds:______\b\b\b\b\b\b";
    int weightInPounds;
    cin >> weightInPounds;
   
    double heightInMeters = heightInInches * 0.0254;                        // these two statements are for converting the height
    double weightInKilograms = weightInPounds / 2.2;                        // and weight to metric units
    double heightInMetersSquared = pow(heightInMeters, 2);
    long double bodyMassIndex = weightInKilograms / heightInMetersSquared;
    cout << "Your BMI is: " << bodyMassIndex << endl;

    return 0;
}
[/code]


Top
  Profile  
 
 Post subject:
PostPosted: Wed Nov 11, 2009 1:25 am 
Bitchin' Fast 3D Z8000*
Bitchin' Fast 3D Z8000*
User avatar

Joined: Tue Jun 29, 2004 11:32 pm
Posts: 2555
Location: Somewhere between compilation and linking
These are relatively simple programs...

Putting the namespace statement inside the main block is a bit odd.

* at least it _used_ to be odd, hopefully one of the younger bit-twiddlers will let me know if something changed!


Top
  Profile  
 
 Post subject:
PostPosted: Wed Nov 11, 2009 5:27 am 
SON OF A GUN
SON OF A GUN
User avatar

Joined: Mon Nov 01, 2004 5:41 am
Posts: 11605
shutout5591 wrote:
yea, looks good. The only thing I can think of is your naming scheme, Everybody has their own, but I personally don't use abbreviations, i think it looks more professional.

But if it works for you, good job!
It isn't a matter of professionalism. It is a matter of readability. With auto-complete and intelli-sense type functionality in modern IDEs, having variable and function names that are 20 characters isn't a big deal.. you type the first 4 and hit a couple key short cuts and boom it is filled in for you. Much easier to read.

Gadget wrote:
These are relatively simple programs...

Putting the namespace statement inside the main block is a bit odd.

* at least it _used_ to be odd, hopefully one of the younger bit-twiddlers will let me know if something changed!
I'd say it is odd... lol

Code:
#include <iostream>
#include <cmath>

using namespace std;

int main()
{
    cout << "Please enter your height:______\b\b\b\b\b\b";
    int heightInInches;
    cin >> heightInInches;
   
    cout << "Please enter your weight in pounds:______\b\b\b\b\b\b";
    int weightInPounds;
    cin >> weightInPounds;
   
    // these two statements are for converting the height
    double heightInMeters = heightInInches * 0.0254;
    // and weight to metric units
    double weightInKilograms = weightInPounds / 2.2;
    double heightInMetersSquared = pow(heightInMeters, 2);
    long double bodyMassIndex = weightInKilograms / heightInMetersSquared;
    cout << "Your BMI is: " << bodyMassIndex << endl;

    return 0;
}


Other than being piddly little example programs, things are looking good! Getting your style down early is important. I wish I had that focus because I wrestle with bad style sometimes even today.


Top
  Profile  
 
 Post subject:
PostPosted: Wed Nov 11, 2009 7:23 am 
8086
8086

Joined: Wed Sep 23, 2009 2:41 am
Posts: 12
Thanks to all of you for the help. I was just piddling with C++ to get the hang of it before I started school. My curriculum starts with HTML and Java servlets then moves on to Java Programs, then to C++ and C# programs. But everyone has been helpful, thanks again!


Top
  Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 10 posts ] 

All times are UTC - 8 hours


Who is online

Users browsing this forum: No registered users and 4 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group