I've managed to make a bit of progress in C# But I'm currently stuck

Started by
13 comments, last by Khaiy 13 years ago
I feel like I have made alot of progress between now and yesterday, even though this is not very much.
The issues I am working on trying to solve:
Taking the input from the "temp" int variables, sending it to the proper conversion method, and having the converted temperature sent back to the screen.
Not having the "Invalid type, please type Celsius or Fahrenheit" line execute when I type exit.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string input = "";
Console.WriteLine("Please enter a conversion type: ");

while (input.ToLower() != "exit")
{
input = Console.ReadLine();
Console.Write("\n");

if (input == "fahrenheit".ToLower())
{
Console.WriteLine("Fahrenheit it is!, Please enter a temperature to convert: ");
int temp;
temp = int.Parse(Console.ReadLine();
//temp = ToFahrenheit();
}

if (input == "celsius".ToLower())
{
Console.WriteLine("Celsius it is!, Please enter a temperature to convert: ");
int temp;
temp = int.Parse(Console.ReadLine());
//temp = ToCelsius();
}

if ((input != "celsius".ToLower()) && (input != "fahrenheit".ToLower()))
{
Console.WriteLine("Invalid type, please type Celsius or Fahrenheit\n");
}
}
}

static string ToFahrenheit(string fTemp)
{
int fahrenheit;
fahrenheit = int.Parse(fTemp);
fahrenheit = fahrenheit * 9;
fahrenheit = fahrenheit / 5;
fahrenheit = fahrenheit + 32;
Console.WriteLine(fahrenheit);
return fTemp;
}

static string ToCelsius(string cTemp)
{
int celsius;
celsius = int.Parse(cTemp);
celsius = celsius - 32;
celsius = celsius * 9;
celsius = celsius / 5;
Console.WriteLine(celsius);
return cTemp;
}

}
}


There are probably some rookie mistakes/obvious mistakes that I am too tired to notice right now, as in I think the Console.Writline(celsius/fahenheit) lines will not work as the value is returned afterwards, or w/e. But help would be appreciated.
Advertisement
You test whether the input is "exit" at the start of the loop. However, this is after you call ReadLine(). The loop body is executed as a block, it is never interrupted even if the loop condition is hit.

You could re-write your loop in terms of a boolean condition:

bool running = true;
while(running)
{
string input = Console.ReadLine();

if(input.ToLower() == "exit")
{
running = false;
}
else
{
// ...
}
}


Also, you are converting your lowercase string literals ToLower(), which isn't going to work for you. Instead, after you call ReadLine, use "input = input.ToLower()". This way you lowercase the user's string once, so after that you don't need to worry about case any more.

Consider chaining your if statements with else statements:

// Instead of

if(condition_one)
{
// outcome one
}

if(condition_two)
{
// outcome two
}

if( (!condition_one) && (!condition_two) )
{
// default outcome
}

// Use this:

if(condition_one)
{
// outcome one
}
else if(condition_two)
{
// outcome two
}
else
{
// default outcome
}

Note that this transformation is only valid in the case each conditional is exclusive, which is true here.

I feel like I have made alot of progress between now and yesterday, even though this is not very much.
The issues I am working on trying to solve:
Taking the input from the "temp" int variables, sending it to the proper conversion method, and having the converted temperature sent back to the screen.
Not having the "Invalid type, please type Celsius or Fahrenheit" line execute when I type exit.

There are probably some rookie mistakes/obvious mistakes that I am too tired to notice right now, as in I think the Console.Writline(celsius/fahenheit) lines will not work as the value is returned afterwards, or w/e. But help would be appreciated.


In addition to Rip's advice, I'd probably move this line:

Console.WriteLine("Please enter a conversion type: ");

into your loop so that when you're done with your conversion it reprints the instructions on the screen each iteration. That said, in order to get your conversions working, changing what you currently have to:


Console.WriteLine("Fahrenheit it is!, Please enter a temperature to convert: ");
string temp = Console.ReadLine();
temp = ToFahrenheit(temp);


should fix your issue. If you would rather parse your integer before the method call, you could change your method parameter to an integer so you didn't have to parse it to an int again after you did in your original "if" statement. A word of caution though, whenever you try to parse data types you run the risk of throwing an exception which you aren't dealing with here. If the user enters "banana" when you're expecting a temperature, you're going to crash out of your program so it's generally a good idea to wrap your parses in a try/catch block. Also you're returning an integer when your method return type is a string. C# apparently doesn't care about that (at least visual studio didn't seem to yell at me about it) but that may be an issue in other languages.
I haven't done something about the "crashing" bit if a user types something other than a number, but:
The issue i'm having now is, every time I do a Fahrenheit conversion, it only adds 32 to the number I type in. It doesn't do the 9 / 5 * Fahrenheit.
At first, I thought changing the variables to float would fix the issue, but it didn't. I haven't fixed Celsius yet..

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string input = "";

while (input.ToLower() != "exit")
{
Console.WriteLine("Please enter a conversion type: ");
input = Console.ReadLine();
Console.Write("\n");

if (input == "fahrenheit".ToLower())
{
Console.WriteLine("Fahrenheit it is!, Please enter a temperature to convert: ");
string temp = Console.ReadLine();
temp = ToFahrenheit(temp);
}

if (input == "celsius".ToLower())
{
Console.WriteLine("Celsius it is!, Please enter a temperature to convert: ");
string temp = Console.ReadLine();
temp = ToCelsius(temp);
}

if ((input != "celsius".ToLower()) && (input != "fahrenheit".ToLower()))
{
Console.WriteLine("Invalid type, please enter Celsius or Fahrenheit\n");
}
}
}

static string ToFahrenheit(string fTemp)
{
float fahrenheit;
fahrenheit = float.Parse(fTemp);
fahrenheit = 9 / 5 * fahrenheit + 32;
Console.WriteLine(fahrenheit);
return fTemp;
}

static string ToCelsius(string cTemp)
{
float celsius;
celsius = float.Parse(cTemp);
celsius = celsius - 32;
celsius = celsius * 9;
celsius = celsius / 5;
Console.WriteLine(celsius);
return cTemp;
}

}
}
You are doing "integer division". Using integer division, 9 / 5 is 1, any fractional value is truncated. This is despite the fact that you later store the result of the expression as a float. The compiler uses the immediate types of the constants to determine the kind of operation it is.

Use floating point constants, e.g. 9.0f / 5.0f. I would recommend using parentheses or extra variables, because it clarifies your intent.

I would write:

float conversionRatio = 9.0f / 5.0f;
farenheit = (conversionRation * farenheit) + 32;

You are doing "integer division". Using integer division, 9 / 5 is 1, any fractional value is truncated. This is despite the fact that you later store the result of the expression as a float. The compiler uses the immediate types of the constants to determine the kind of operation it is.

Use floating point constants, e.g. 9.0f / 5.0f. I would recommend using parentheses or extra variables, because it clarifies your intent.

I would write:

float conversionRatio = 9.0f / 5.0f;
farenheit = (conversionRation * farenheit) + 32;



Thank you sir. I'm SLOWLY starting to get this. I'll need to work on the whole static returnvalue Method(parameter) parts.

The whole returning a variable thing is still a bit confusion to me, as well as the parameter bits. I'm not 100% on how either of these work.

[quote name='rip-off' timestamp='1302794968' post='4798433']
You are doing "integer division". Using integer division, 9 / 5 is 1, any fractional value is truncated. This is despite the fact that you later store the result of the expression as a float. The compiler uses the immediate types of the constants to determine the kind of operation it is.

Use floating point constants, e.g. 9.0f / 5.0f. I would recommend using parentheses or extra variables, because it clarifies your intent.

I would write:

float conversionRatio = 9.0f / 5.0f;
farenheit = (conversionRation * farenheit) + 32;



Thank you sir. I'm SLOWLY starting to get this. I'll need to work on the whole static returnvalue Method(parameter) parts.

The whole returning a variable thing is still a bit confusion to me, as well as the parameter bits. I'm not 100% on how either of these work.
[/quote]


static string ToFahrenheit(string fTemp)
{
float fahrenheit;
fahrenheit = float.Parse(fTemp);
fahrenheit = 9 / 5 * fahrenheit + 32;
Console.WriteLine(fahrenheit);
return fTemp;
}


For a function like this, if you don't need to do anything with the converted temperature, a return type isn't needed here. If you want to manipulate the temperature back in your main method then you would want to keep the return type. The code like this:

static void ToFahrenheit(string fTemp)
{
float fahrenheit;
fahrenheit = float.Parse(fTemp);
fahrenheit = 9 / 5 * fahrenheit + 32;
Console.WriteLine(fahrenheit);
}

should do exactly the same thing. The only exception is that when you call a function with no return type like this, you won't use "temp = ToFahrenheit(temp)", you'd simply just call it like "ToFarhrenheit(temp)". Additionally, I don't think static is necessary in either of these functions, but I don't do a lot of console programming so not sure if that's different somehow. You could probably just use "void ToFahrenheit(string fTemp)" and be fine.

Hope that makes sense/helps.
When removing static from both methods, I get the following error:

"An object reference is required for the non-static field, method or property "ConsoleApplication1.Program.ToFahrenheit(string);

When removing static from both methods, I get the following error:

"An object reference is required for the non-static field, method or property "ConsoleApplication1.Program.ToFahrenheit(string);


My fault, I don't write console applications so ignore what I said there and keep them static. "static void ToFahrenheit(string fTemp)" and the same for Celsius should work though.

[quote name='GraySnakeGenocide' timestamp='1302798010' post='4798448']
When removing static from both methods, I get the following error:

"An object reference is required for the non-static field, method or property "ConsoleApplication1.Program.ToFahrenheit(string);


My fault, I don't write console applications so ignore what I said there and keep them static. "static void ToFahrenheit(string fTemp)" and the same for Celsius should work though.
[/quote]

Its not that its a console app, its that Main() is a static function. Unless the conversion functions are put into a class and that class is created in Main or the functions are declared as static then Main will never be able to find them.

This topic is closed to new replies.

Advertisement