Click here to Skip to main content
15,889,096 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
(C# Visual Studio 2019) Hello, my professor wants us to do the following code shown in the picture : https://i.stack.imgur.com/ONNSV.png

We are fairly new into the course and have have talked about only a few of conversion methods.

What I have tried:

Here is my attempt, but with an error that I cannot seem to wrap my head around. If someone can help me out or provide a solution, thanks!

C#
Console.Write("Enter degrees Fahrenheit to convert to Celsius OR

\nenter degrees Celsius to convert to Fahrenheit.");

Console.Write("\n\nCurrent temperature scale (C for Celsius; F for Fahrenheit): ");

double tempscale = Convert.ToDouble(Console.ReadLine());

Console.Write("Current degrees: ");

double currentdeg = Convert.ToDouble(Console.ReadLine());

double degreeCelsius = 5 / 9 * (tempscale - 32);

double degreeFahrenheit = 9 / 5 * (degreeCelsius + 32);

if (tempscale == degreeFahrenheit)

Console.Write(degreeFahrenheit + "degrees Fahrenheit is" + degreeCelsius + "degrees Celsius");

if (tempscale == degreeCelsius)

Console.Write(degreeCelsius + "degrees Celsius is" + degreeFahrenheit + "degrees Fahrenheit");

Console.Write("\n\nPress any key to continue...");

Console.ReadKey();
Posted
Updated 30-Oct-20 0:05am
v2

Quote:
with an error that I cannot seem to wrap my head around
Put a breakpoint at the start of your code, and single step through it (F11): observe the values of variables to find unexpected results.

Describe the error, and where it occurs.

These simple methods are the foundation of debugging, and they are as essential for competence as a programmer as mastery of the computer language you use.

CodeProject has many articles on debugging: [^] ... to start with: I recommend [^] and [^]
 
Share this answer
 
v2
Comments
TheRealSteveJudge 30-Oct-20 6:19am    
Debugging and a more detailed description of the symptoms is always a good idea! 5*
As Bill suggested debugging is what every developer must learn in order to know what is happening inside when the programme does not behave as expected.
Please let me give you some more suggestions.

The first problem is
C#
Console.Write("Enter degrees Fahrenheit to convert to Celsius OR

                \nenter degrees Celsius to convert to Fahrenheit.");

will sabotage a successful compilation.
You should replace it by
C#
Console.Write("Enter degrees Fahrenheit to convert to Celsius OR\nenter degrees Celsius to convert to Fahrenheit.");

The next problem is
C#
double tempscale = Convert.ToDouble(Console.ReadLine());

You are expecting the user to enter either 'C' or 'F'.
No matter what the user will enter you convert it to double which is not a suitable data type for characters.
You should replace it by
C#
var tempscale = Console.ReadLine();

The lines to follow then make no sense.
Hint:
You can start with this for conversion from Celsius to Fahrenheit
C#
if (tempscale == "C")
{
    double degreeFahrenheit = 9 / 5 * (currentdeg + 32);
    Console.Write(currentdeg + "degrees Celsius is " + degreeFahrenheit + " degrees Fahrenheit");

Then you must do something similar for the opposite direction.
BTW: Your formula for conversion from Fahrenheit to Celsius is wrong!
Moreover you must take care of error handling to catch invalid inputs e.g. the user entering other letters than 'C' or 'F' in the first Console.Readline.
What will happen if the user enters any other than valid numbers in the second Console.ReadLine?
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900