Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

Help with my logic on Temperature conversion console app

$
0
0
I am just starting off learning C#. I am trying to make a simple console application that does temperature conversion. Basically you choose whether you want to convert Fahrenheit to Celsius, Celsius to Fahrenheit, or exit. The problem I am having, is that when the user enters a number, it isn't storing it thus, giving the same conversion over and over again, no matter what you enter. The second problem is that the nested while loops are not looping. It is supposed to ask the user if they want to make a similar conversion, if the user enters y then it should loop through again, however, the prompt comes up, but doesn't allow the user to type y or n. Any advise to fix this would be greatly appreciated.

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

namespace TempConversion
{
    class Program
    {
        static void Main(string[] args)
        {
            // Intro to program
            Console.WriteLine("\t\tWelcome to the Temperature Conversion Program\n\n");

            //Setup Variable for loop
            string choose = "q";
            string contA = "y";
            string contB = "y";

            //Loop
            while (choose != "c")
            {
                //Main Menu
                Console.WriteLine("What would you like to do?");
                Console.WriteLine("A. Convert Fahrenheit to Celsius");
                Console.WriteLine("B. Convert Celsius to Fahrenheit");
                Console.WriteLine("C. Exit");
                choose = Console.ReadLine();

                // If user chooses "A"
                if (choose.Equals("a", StringComparison.OrdinalIgnoreCase))
                {
                    while (contA.Equals("y", StringComparison.OrdinalIgnoreCase))
                    {
                        double farh, celc;
                        Console.Write("Enter the temperature in Fahrenheit: ");
                        farh = Console.Read();
                        celc = (farh - 32) * 5 / 9;
                        Console.WriteLine("\n" + farh.ToString() + " degrees Fahrenheit = " + celc.ToString() + " degrees Celsius\n");
                        Console.WriteLine("Would you like to make another conversion like this?(y or n): ");
                        contA = Console.ReadLine();
                    }
                }
                // If user chooses "B"
                else if (choose.Equals("b", StringComparison.OrdinalIgnoreCase))
                {
                    while (contB.Equals("y", StringComparison.OrdinalIgnoreCase))
                    {
                        double faren, celsius;
                        Console.Write("Enter the temperature in Celsius: ");
                        celsius = Console.Read();
                        faren = (celsius * 9 / 5) + 32;
                        Console.WriteLine("\n" + celsius.ToString() + " degrees Celsius = " + faren.ToString() + " degrees Fahrenheit\n");
                        Console.WriteLine("Would you like to make another conversion like this?(y or n): ");
                        contB = Console.ReadLine();
                    }
                }
                else
                {
                    Console.WriteLine("\n\t***Invalid Input***");
                }
            }
        }
    }
}



Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>