C# repeat or close program based on input - Temperature Conversion - c#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("What is the Temperature in Fahrenheit?")
string input = Console.ReadLine ();
double tt = double.Parse (input);
double cc = (tt-32)*5/9;
Console.WriteLine(cc + " is the temperature in Celsius");
}
}
}
My problem is, I need to receive input and close out if I don't. If their input is (""), the program needs to close, but if they input a number - e.g. 13, I need to have the program loop back to the beginning until I get an input of (""), at which point the program closes. I'm fairly new to programming, but I've tried everything I can think of to try/figure out, so any help you can give would be greatly appreciated.

Try:
Console.WriteLine("What is the Temperature in Fahrenheit?")
string input = Console.ReadLine ();
while (input != "")
{
double tt = double.Parse (input);
double cc = (tt-32)*5/9;
Console.WriteLine(cc + " is the temperature in Celsius");
Console.WriteLine("What is the Temperature in Fahrenheit?")
input = Console.ReadLine ();
}

if instead you use a specific character to specify ending the program instead of an empty line, it is relatively easy:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
string exit = "";
while(exit.ToLower != "no")
{
Console.WriteLine("What is the Temperature in Fahrenheit?");
string input = Console.ReadLine();
double tt = double.Parse (input);
double cc = (tt-32)*5/9;
Console.WriteLine(cc + " is the temperature in Celsius");
Console.WriteLine("if you want to do another conversion, enter \"yes\" otherwise, enter \"no\"");
exit = Console.ReadLine();
}
}
}
}

Related

Program that writes numbers until I write a string to stop it

I am required to write a program that prints out numbers until I write in the console window a simple string "stop". The numbers are supposed to go on infinitely until the condition is met.
I tried:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp3
{
internal class Program
{
static void Main(string[] args)
{
for (int i = 0; i < 1000000000;)
{
Console.WriteLine(i);
string a = Console.ReadLine();
if (a == "stop")
{
break;
}
i++;
}
}
}
}
But, there is a delay waiting for my input every time, it's not constant.
You are asking for a non blocking console read. THats not simple.
One way is to peek to see if a key is available to read
int number = 0;
while (true)
{
Console.WriteLine(number);
number++;
if (Console.KeyAvailable)
{
var s = Console.ReadLine();
if (s == "stop")
break;
}
}

Strange behaviour from Console.ReadKey()

The output looks something like:
] [ Your input is: ffffffwwqqqwffasfffffffw
>
when you use BACKSPACE which shouldn't be possible to begin with,
why is this happening
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
namespace ConsoleApp7
{
class Program
{
public static string input;
static Program()
{
input = string.Empty;
}
static void Main(string[] args)
{
while (true)
{
ConsoleKeyInfo consoleKeyInfo;
Console.Clear();
Console.Out.Write($"\r\n\t[ Your input is: {input} ]\r\n\r\n\t>");
consoleKeyInfo = Console.ReadKey();
if (consoleKeyInfo.Modifiers == default(ConsoleModifiers))
input += consoleKeyInfo.KeyChar;
Thread.Sleep(250);
}
}
}
}
Backspace is a valid char for ReadKey to process, and when you concatenate a backspace char to the string, it will remove the last character from the string in the output. You can check whether the key that was pressed was a backspace and ignore it.
if (consoleKeyInfo.Modifiers == default(ConsoleModifiers) && consoleKeyInfo.Key != ConsoleKey.Backspace)
input += consoleKeyInfo.KeyChar;

How could i replace a number with it's matching month name in c#? [duplicate]

This question already has answers here:
Get Month name from month number
(9 answers)
Closed 5 years ago.
So i have a program that asks for your birth data and i want to replace the number of the month to its matching name. For ex. if someone writes 1989.11.10 then the result would be 1989. november 10.
My code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Text.RegularExpressions;
namespace hf
{
class Program
{
static void Main(string[] args)
{
Console.Write("Bith data: ");
string adat = Console.ReadLine();
string szido = Regex.Replace(adat, "[^0-9.]", "");
Console.WriteLine("Birthday: {0}", szido);
string[] szidotomb = new string[3];
szidotomb = szido.Split('.');
for (int i = 0; i < 3; i++)
Console.WriteLine(szidotomb[i]);
Console.ReadKey();
}
}
}
No need to, you can use DateTime.ToLongDateString or .ToString
Read your user's input from the console, then use DateTime.tryParse the string, and then print it with the aforementioned method of the class.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Text.RegularExpressions;
using System.Globalization;
public class Program
{
public static void Main()
{
DateTime dt = new DateTime();
string userInput;
do
{
Console.Write("Please enter your birth date in YYYY.MM.DD format: ");
userInput = Console.ReadLine();
}
while (!DateTime.TryParseExact(userInput, "yyyy.MM.dd",
CultureInfo.InvariantCulture,
DateTimeStyles.None,
out dt));
Console.WriteLine("You were born on: \"{0}\"\n", dt.ToString("yyyy.MMMM.dd"));
Console.ReadLine();
}
}
You can run an example here

C# accepting only numbers

I'm doing an exercise of converting fahrenheits to celsius , my question is , how can i say to the program to not accept letters when the user inputs anything? (which is supposed to be only numbers).
My code is this
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace FirstCsharpProgram
{
class Program
{
static void Main(string[] args)
{
//declaring the first temperature needed
float originalFahrenheit;
float cels;
//Input fehrenheit degrees from the user
Console.Write("Enter temperature (Fahrenheit): ");
originalFahrenheit = float.Parse(Console.ReadLine());
cels = (((originalFahrenheit - 32) /9) * 5);
Console.Write(originalFahrenheit + " fahrenheit = " + cels);
Console.Write(" celsius");
Console.Write("");
}
}
}
I would like to have the next piece of code in my program as the exercise intends to start with it
Console.Write("Enter temperature (Fahrenheit): ");
originalFahrenheit = float.Parse(Console.ReadLine());
If you could help me proceeding with that piece of code i would be grateful
Thank you
I would do it this way:
static float ReadFloatFromConsole()
{
float number;
while (!float.TryParse(Console.ReadLine(), out number))
{
Console.WriteLine("Invalid number, please try again");
}
return number;
}
if(!float.TryParse(Console.ReadLine(), out originalFahrenheit)) //Parse fail
{
//Your error message
return; //Exit program
}

method to return largest integer c#

I'm having trouble with this method return of the largest integer, compiler says no errors but it won't let me run it
I have no clear idea what you are asking for.. I think at one time you had code but it is now gone?
Anyway, here is a console example for making an array and displaying its max value.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication3
{
class Program
{
static void Main(string[] args)
{
//Declare i
int i;
Console.WriteLine("Please enter 5 random numbers");
//Make some array
string[] numbers = new string[5];
for (i = 0; i < 5; i++)
{
Console.Write("\nEnter your number:\t");
//Storing value in an array
numbers[i] = Console.ReadLine();
}
//ta da your array is now completed.. lets see what is the largest..
var converted = numbers.Select(int.Parse);
int largest = converted.Max();
//ta da
Console.WriteLine("The largest number is..." + (largest));
}
}
}

Categories