C# School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become a C# Expert!

Join 307,117 C# Programmers for FREE! Get instant access to thousands of C# experts, tutorials, code snippets, and more! There are 1,997 people online right now. Registration is fast and FREE... Join Now!




First C# application and having problems

 

First C# application and having problems

phillover80

28 Jun, 2009 - 05:08 PM
Post #1

New D.I.C Head
*

Joined: 28 Jun, 2009
Posts: 9

I'm new to C# programming language. This is my first assignment that is supposed to convert a temperature from Fahrenheit to Celsius. It should use three different methods; one for input, one for the conversion and one to display the results.
I've been working on this for a couple days and have taken out some things, added some things and switched some things around but I keep getting different errors. Can anyone take a look and tell me what I am doing wrong? Your help is very much appreciated. I though this was fairly simple at first but it's just not working for me.

CODE
using System;

namespace TemperatureConverter
{
    class FahrenheitConverter
    {
        
        //User Input
        static void Main()
        {
            double cTemp;
            double fTemp;

            InputValues();
            ConvertToCelsius(fTemp);
            DisplayResults(cTemp);
            Console.Read();        
        }
        public static void InputValues()
        {
            string inputTemp;

            Console.Write("Please enter the Fahrenheit temperature: ");
            inputTemp = Console.ReadLine();
            
                
        }
        //To convert from Fahrenheit to celsius
        public static double ConvertToCelsius(double fTemp)
        {
            return (fTemp - 32) * 5/9;
        }
        //Display the results
        public static void DisplayResults(double cTemp)
        {
            Console.Write("{0} degrees Fahrenheit is the same as ");
            Console.WriteLine("{1} degrees Celsius");
            

        }
    }
}


User is offlineProfile CardPM
+Quote Post


SixOfEleven

RE: First C# Application And Having Problems

28 Jun, 2009 - 06:55 PM
Post #2

lives.ToCode();
Group Icon

Joined: 18 Oct, 2008
Posts: 3,066



Thanked: 170 times
Dream Kudos: 775
Expert In: C, C#, XNA, Game Programming, Programming Concepts

My Contributions
The first thing I see that I would change is your InputValues method. You would want it to return the value that was inputted, like you returned the double in the ConvertToCelcius method. You would want to convert it to a double as when it is inputted it is a string.

You can convert the string to a double using this method.

CODE

Double.Parse(inputTemp);
[/cdoe]

Then, in your main when you call the [b]InputValues[/b] method you would say:

[code]
fTemp = InputValues();


That should give you a good start on finishing things.
User is offlineProfile CardPM
+Quote Post

eclipsed4utoo

RE: First C# Application And Having Problems

29 Jun, 2009 - 05:57 AM
Post #3

Not Your Ordinary Programmer
Group Icon

Joined: 21 Mar, 2008
Posts: 1,846



Thanked: 205 times
Dream Kudos: 500
Expert In: .NET

My Contributions
here is one way...

c#

class Program
{
static void Main(string[] args)
{
double cTemp = 0;
double fTemp = 0;

fTemp = InputValues();
cTemp = ConvertToCelsius(fTemp);
DisplayResults(fTemp, cTemp);
}

static double InputValues()
{
Console.Write("Please enter the Fahrenheit temperature: ");
return double.Parse(Console.ReadLine());
}

static double ConvertToCelsius(double fTemp)
{
return (fTemp - 32) * 5 / 9;
}

static void DisplayResults(double fTemp, double cTemp)
{
Console.WriteLine("{0} degrees Fahrenheit is the same as {1} degrees Celsius", fTemp, cTemp);
Console.Read();
}
}

User is offlineProfile CardPM
+Quote Post

phillover80

RE: First C# Application And Having Problems

29 Jun, 2009 - 04:45 PM
Post #4

New D.I.C Head
*

Joined: 28 Jun, 2009
Posts: 9

QUOTE(SixOfEleven @ 28 Jun, 2009 - 06:55 PM) *

The first thing I see that I would change is your InputValues method. You would want it to return the value that was inputted, like you returned the double in the ConvertToCelcius method. You would want to convert it to a double as when it is inputted it is a string.

You can convert the string to a double using this method.

CODE

Double.Parse(inputTemp);
[/cdoe]

Then, in your main when you call the [b]InputValues[/b] method you would say:

[code]
fTemp = InputValues();


That should give you a good start on finishing things.


Ok thats helpful thanks. I had that originally but I had it backwards and ended up deleting it.

User is offlineProfile CardPM
+Quote Post

phillover80

RE: First C# Application And Having Problems

29 Jun, 2009 - 04:52 PM
Post #5

New D.I.C Head
*

Joined: 28 Jun, 2009
Posts: 9

QUOTE(eclipsed4utoo @ 29 Jun, 2009 - 05:57 AM) *

here is one way...

c#

class Program
{
static void Main(string[] args)
{
double cTemp = 0;
double fTemp = 0;

fTemp = InputValues();
cTemp = ConvertToCelsius(fTemp);
DisplayResults(fTemp, cTemp);
}

static double InputValues()
{
Console.Write("Please enter the Fahrenheit temperature: ");
return double.Parse(Console.ReadLine());
}

static double ConvertToCelsius(double fTemp)
{
return (fTemp - 32) * 5 / 9;
}

static void DisplayResults(double fTemp, double cTemp)
{
Console.WriteLine("{0} degrees Fahrenheit is the same as {1} degrees Celsius", fTemp, cTemp);
Console.Read();
}
}



Aha! I didnt know I had to give fTemp and cTemp a value at first. That was what I was missing when i first wrote my program.
This is the other line that threw me off.
CODE
  return double.Parse(Console.ReadLine());    


Thank you both for your help.
This is a great site.
User is offlineProfile CardPM
+Quote Post

eclipsed4utoo

RE: First C# Application And Having Problems

29 Jun, 2009 - 06:16 PM
Post #6

Not Your Ordinary Programmer
Group Icon

Joined: 21 Mar, 2008
Posts: 1,846



Thanked: 205 times
Dream Kudos: 500
Expert In: .NET

My Contributions
QUOTE(phillover80 @ 29 Jun, 2009 - 07:52 PM) *

QUOTE(eclipsed4utoo @ 29 Jun, 2009 - 05:57 AM) *

here is one way...

c#

class Program
{
static void Main(string[] args)
{
double cTemp = 0;
double fTemp = 0;

fTemp = InputValues();
cTemp = ConvertToCelsius(fTemp);
DisplayResults(fTemp, cTemp);
}

static double InputValues()
{
Console.Write("Please enter the Fahrenheit temperature: ");
return double.Parse(Console.ReadLine());
}

static double ConvertToCelsius(double fTemp)
{
return (fTemp - 32) * 5 / 9;
}

static void DisplayResults(double fTemp, double cTemp)
{
Console.WriteLine("{0} degrees Fahrenheit is the same as {1} degrees Celsius", fTemp, cTemp);
Console.Read();
}
}



Aha! I didnt know I had to give fTemp and cTemp a value at first. That was what I was missing when i first wrote my program.
This is the other line that threw me off.
CODE
  return double.Parse(Console.ReadLine());    


Thank you both for your help.
This is a great site.


You aren't required to give the "doubles" a value, but it's good programming practice to always give variables a default value.
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/21/09 01:29PM

Live C# Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

C# Tutorials

Reference Sheets

C# Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month