|
![]() |
Overclock.net - Overclocking.net > Software, Programming and Coding > Coding and Programming > Application Programming | |
My simple Fahrenheit to Celcius converter in C#
|
||
![]() |
|
|
LinkBack | Thread Tools |
|
|
#1 (permalink) | |||||||||
|
NANI?
![]() |
I thought I'd make it easy for people to look at how C# code works. It's overly commented, for people who is not familiar to programming or C# to know what is actually going on:
Code:
using System;
using System.Collections.Generic;
using System.Text;
//
// Name: Sebastian 'gonX' Jensen
// Date: 4. Nov 09
// Program: A simple Fahrenheit from/to Celcius converter
// Known to compile fine in: Microsoft Visual Express C# 2008
// Comments: In this program I've used simple commands which should be fairly straightforward to understand.
// Please notice that I've moved Main() to the bottom of the document so that it does not complain
// that it's missing some functions that I'm calling from Main(). Yes, I could have assigned them
// earlier in the program, but this is less confusing to look at.
// Please throw me a PM on Overclock.net if you do not understand parts of my code - my username is gonX.
//
namespace switcheslawl
{
class Program
{
// Here's an example of how Main() is not the first variable in the program.
static void toFahrenheit()
{
// Let's assign the variables we need. We use double so that we don't get unnecessary rounding.
string temporary;
double input;
double temperature;
// Clear it so that it doesn't look messy.
Console.Clear();
// Here I'll be telling the user to input his temperature in degrees celcius.
// Please notice that I am not using Console.WriteLine(), but instead Console.Write()
// I'm using a temporary variable as calculating with a string is troublesome,
// it is parsed to a double throughout the code though.
// This is what makes C# different from C++ - in C++ I would not have needed to parse it.
Console.Write("Please enter the temperature in °C: ");
temporary = Console.ReadLine();
input = double.Parse(temporary);
temperature = input * 1.8 + 32;
// The :N2 tells the program to only output 2 decimals. They're rounded off, which is not that
// good for people who actively work with numbers, but it'll work for most people.
Console.Write("\n{0 :N2} °C is {1 :N2} °F\nPress a button to continue\n", input, temperature);
Console.ReadKey();
}
static void toCelcius()
{
// All this is the same as in the toFahrenheit() static, except that the formula for calculating is different.
string temporary;
double input;
double temperature;
Console.Clear();
Console.Write("Please enter the temperature in °F: ");
temporary = Console.ReadLine();
input = double.Parse(temporary);
temperature = (input - 32) / 1.8;
Console.Write("\n{0 :N2} °F is {1 :N2} °C\nPress a button to continue\n", input, temperature);
Console.ReadKey();
}
static void Main()
{
// Here I have to assign option to 0, otherwise it'd complain about option not being assigned
string option = "0";
// I can type "stop" to stop the program.
while (option != "stop")
{
Console.Clear();
Console.WriteLine("What do you want to calculate to?\n\nF. To Fahrenheit\nC. To Celcius\nstop to stop.");
option = Console.ReadLine();
// Most people should be acquainted with switches if they've been programming before. This is fairly similar to C++ syntax.
switch (option.ToLower())
{
case "f":
toFahrenheit();
break;
case "c":
toCelcius();
break;
default:
option = "stop";
break;
}
}
}
}
}
__________________
THE Mouse FAQ | 32-bit Resolution Fix | Important Information 64-Bit Driver Signing Fix | The Infraction and Warning System My Anime Progress | The HoN Discussion Thread Please direct all tech related questions to a thread in the respectable forums, and not to my PM inbox. Thank you
|
|||||||||
|
|
|
|
#2 (permalink) | ||||||||||||||
|
Intel Overclocker
![]() |
Nice work. Code seems clean to me, but I do have a couple of other suggestions if you don't mind.
I don't know about anyone else, but I always initialize my variables and assign them a value (null, blank, 0, etc). Such as: Code:
string temporary = ""; double input = 0; double temperature = 0; //You can also keep multiple variables of the same type on one line string temporary = ""; double input = 0, temperature = 0; I'm not sure how simple you want to keep this, but you could replace Parse with TryParse for an easy error checking example. Code:
// TryParse takes a string and outputs to a double, while returning a boolean value
// Create a bool variable to test whether the conversion was successful
bool convert = double.TryParse(temporary, out input);
// If the conversion was successful and the bool is true, complete the method
if (convert)
{
temperature = input * 1.8 + 32;
Console.Write("\n{0 :N2} °C is {1 :N2} °F\nPress a button to continue\n", input, temperature);
Console.ReadKey();
}
// If the conversion failed and the bool is false, display an error message
else
{
Console.WriteLine("You must enter a valid number. Press key to start over.");
Console.ReadKey();
// Run the same method
toFarenheit();
}
Again, nice work. Little examples like this would have come in handy when I was first learning C#.
__________________
Quote:
|
||||||||||||||
|
|
|
|
|
#3 (permalink) |
|
Case Modder
![]() |
The toXXX() methods do too much. Gathering input from the user and outputting the results to the console doesn't belong in "temperature conversion" methods. I suggest that you refactor the methods out into a new "temperature conversion" class.
__________________
Rich Custom Wooden Case Builder
|
|
|
|
|
|
#4 (permalink) | ||||||||||
|
NANI?
![]() |
Quote:
__________________
THE Mouse FAQ | 32-bit Resolution Fix | Important Information 64-Bit Driver Signing Fix | The Infraction and Warning System My Anime Progress | The HoN Discussion Thread Please direct all tech related questions to a thread in the respectable forums, and not to my PM inbox. Thank you
|
||||||||||
|
|
|
|
#5 (permalink) |
|
Case Modder
![]() |
No. This is what I mean:
Code:
class TemperatureConverter
{
public double ToFahrenheit() { ... }
public double ToCelcius() { ... }
}
void Main()
{
TemperatureConverter converter = new TemperatureConverter();
// read temp and scale from user...
double result = converter.ToXXX();
// display temp to user...
}
__________________
Rich Custom Wooden Case Builder
|
|
|
|
|
|
#6 (permalink) | ||||||||||||||
|
Programmer
![]() |
Thanks for this, I'm currently taking a C# class right now, so this will be quite a bit of help. We learned about setting up multiple methods last night, but the parameters threw me off a bit, heh.
__________________
_██_
(ಠ_ರೃ) Quote:
|
||||||||||||||
|
|
|
![]() |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
|
|