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
}
Related
This question already has answers here:
What's the main difference between int.Parse() and Convert.ToInt32
(13 answers)
Closed 4 years ago.
This is the program. Can someone tell me what I did wrong? Every time I input a value for the string SolvingFor, the program crashes. I'm new to coding so if I made a stupid mistake please tell me.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace PercentageCalculator
{
class Program
{
static void Main(string[] args)
{
string SolvingFor;
int part;
int whole;
int percent;
Console.WriteLine("please only use numbers and lowercase letters.");
Console.WriteLine("Are you trying to solve for percent, part, or whole?");
SolvingFor = Convert.ToString(Console.Read());
if (SolvingFor == "part") {
Console.WriteLine("Please Enter Value of the Whole");
whole = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Please Enter Value of the Percent");
percent = Convert.ToInt32(Console.ReadLine()); ;
Console.WriteLine("Your answer is" + (whole * percent) / 100); }
else if (SolvingFor == "whole")
{
Console.WriteLine("Please Enter Value of the part");
part = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Please Enter Value of the Percent");
percent = Convert.ToInt32(Console.ReadLine()); ;
Console.WriteLine("Your answer is" + (part * 100) / percent);
}
else if (SolvingFor == "percent")
{
Console.WriteLine("Please Enter Value of the part");
part = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Please Enter Value of the whole");
whole = Convert.ToInt32(Console.ReadLine()); ;
Console.WriteLine("Your answer is" + (part * 100) / whole);
}
else
{
Console.WriteLine("Please only input valid lowercase letters and numbers. ");
};
Console.Read();
}
}
}
//SolvingFor = Convert.ToString(Console.Read());
SolvingFor = Convert.ToString(Console.ReadLine());
if you put(hover) the mouse over ReadLine
you will see that it returns a string so Convert.ToString is not needed
SolvingFor = Console.ReadLine();
First Issue:
Below line from your code is reading just one character.
Instead of Console.Read(), use Console.ReadLine() to enable you to input multiple characters.
SolvingFor = Convert.ToString(Console.Read());
Second Issue:
You are using Convert.ToInt32(Console.Readline());
Please note that Convert.ToInt32 will throw an exception in anything apart from number is provided as input.
Better use int.TryParse which returns if the conversion is successful or not.
I'm making a where the users enters as many numbers as they want,so long as they're between 1 and 10, then once they're done the program lists the number they entered and how many times. Like this
Now my question is how would you make C# read and check which numbers they entered and print them out and how many times they entered it like in the bottom of the picture? Sorry for the noob question i'm new to this language.
Here is what I have so far:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Assignment_3
{
class Program
{
static void Exercise1()
{
//int min;
//int max;
//Console.WriteLine("Enter minimum integer: ");
//min = int.Parse(Console.ReadLine());
//Console.WriteLine("Enter maximum integer: ");
//max = int.Parse(Console.ReadLine());
//int start = min;
}
static void Exercise2()
{
string entry;
Console.Write("Enter an integer or 'q' to quit ");
entry = Console.ReadLine();
while (entry.ToLower() != "q")
{
int number;
if (int.TryParse(entry, out number))
{
if (number >= 1 && number <= 10)
{
}
else
{
Console.WriteLine("Your number must be between 1 and 10");
}
}
Console.Write("Enter an integer or 'q' to quit ");
entry = Console.ReadLine();
}
}
static void Main(string[] args)
{
Exercise1();
Exercise2();
Console.WriteLine("Press enter to end");
Console.ReadLine();
}
}
}
Well, we do not do your homework, so I will just point you around in the documentation with enough information you are not lost.
Use a Dictionary to keep the nnumber and counter. This is easiest - if a number is entered, get the counter, add one, put back into dictionary. If not - set 1 as value for the number.
At the end you go throuch all the keys (that are the numbers) and print the corresponding values (the number of times).
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace TipCalculator
{
class Program
{
static void Main(string[] args)
{
string answer;
float totalPrice;
Console.WriteLine("Would you like to calculate a tip?");
answer = Console.ReadLine();
if (answer == "yes")
{
Console.WriteLine("Please enter the total price of your meal");
totalPrice = float.Parse(Console.ReadLine());
if (totalPrice >= 20.00)
TipCalculator.over20(totalPrice);
else if (totalPrice < 20)
TipCalculator.under20(totalPrice);
else Console.WriteLine("Error. Please type in the value of the bill again");
}
else if (answer == "no")
Console.WriteLine("Please run this again when you wish to calculate a tip.");
else
Console.WriteLine("Error. Please type in \"yes\" or \"no\"");
Console.ReadLine();
}
}
public class TipCalculator
{
public static string over20(float bill)
{
float tip, totalBill;
tip = (bill * (float)1.2);
totalBill = (tip + bill);
return "Very good. The total tip to be paid is " + tip + " and this will cost " + totalBill + "in total.";
}
public static string under20(float bill)
{
float tip, totalBill;
tip = (bill * (float)1.1);
totalBill = (tip + bill);
return "Very good. The total tip to be paid is " + tip + " and this will cost " + totalBill + "in total.";
}
}
}
When I try to run this code no error is displayed however the message starting with "Very good" does not display in the over20 or under20 message. Thanks in advance!
The issue is that you are returning string from methods over20 and under20, but you are not using them anywhere. Update the part of your code to print the output using Console.WriteLine:
if (totalPrice >= 20.00)
Console.WriteLine(TipCalculator.over20(totalPrice));
else if (totalPrice < 20)
Console.WriteLine(TipCalculator.under20(totalPrice));
else Console.WriteLine("Error. Please type in the value of the bill again");
Working example
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();
}
}
}
}
I've been working on a C# console application, and have successfully instituted a palindromic check based on the user inputing an int. I'm now having trouble with the condition involving addition of the user input and the reverse of said input. I want to continue checking for a palindromic number via this addition and then stop execution of the program when one is reached. My program appears to have difficulty saving the state of the reversed number, any help would be much appreciated:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MathPalindromeConsoleApplication
{
class Program
{
static void Main(string[] args)
{
int num, temp, remainder, reverse = 0;
Console.WriteLine("Enter an integer \n");
num = int.Parse(Console.ReadLine());
bool palindromic = true;
temp = num;
while (num > 0)
{
remainder = num % 10;
reverse = reverse * 10 + remainder;
num /= 10;
}
Console.WriteLine("Given a number is = {0}", temp);
Console.WriteLine("Its reverse is = {0}", reverse);
while (palindromic)
{
if (temp == reverse)
{
Console.WriteLine("Number is a palindrome \n");
palindromic = false;
Console.ReadLine();
}
if (temp != reverse)
{
Console.WriteLine("Number is not a palindrome \n");
Console.WriteLine(temp += reverse);
Console.ReadLine();
}
}
}
}
}
I was going to say the same thing as L.B. He put it in a comment, so I'll put it here. Why not use:
bool pal = num.ToString() == new string(num.ToString().Reverse().ToArray());