This question already has answers here:
Difference between Console.Read() and Console.ReadLine()?
(12 answers)
Closed 1 year ago.
Hello I am practicing with C$ specifically with decimal numeric values. I have this simple program just to check if the inputted data is correct. I input 12.9 but get 49. Any help would be appreciated.
static void Main(string[] args)
{
Console.Write("Enter the first number 1: ");
double num1 = Console.Read();
Console.Write(num1);
//Console.WriteLine(num1+" + "+num2+" + "+num3 + " = "+total);
}
You need to use Console.ReadLine() and not Console.Read().
Read method - Reads the next character from the standard input stream.
ReadLine method - Reads the next line of characters from the standard input stream.
Try this:
static void Main(string[] args)
{
Console.Write("Enter the first number 1: ");
// It might cause an error if the user doesn't write a number
// which is parsable to double
double num1 = double.Parse(Console.ReadLine());
Console.Write(num1);
}
Or a safe way:
static void Main(string[] args)
{
Console.Write("Enter the first number 1: ");
// You may also ask the user to write again an input of number.
if (double.TryParse(Console.ReadLine(), out double num1))
{
Console.Write(num1);
}
}
Related
Having some problems with my Console Calculator
I want to prohibit user inputing a words instead of numbers.
I wrote this code and its converting words to zero but i want user to type a number instead of words. And if user still typing words program should ask user to type number.
internal class Program
{
static void Main(string[] args)
{
Console.WriteLine("----------------------");
Console.WriteLine("------Calculator------");
Console.WriteLine("----------------------");
do
{
double num1 = 0;
double num2 = 0;
double result = 0;
bool result0;
Console.WriteLine("Enter number one: ");
try
{
double.TryParse(Console.ReadLine(), out num1);
if ()
{
Console.WriteLine("Incorrect number!\nType another number:");
num1 = Convert.ToDouble(Console.ReadLine());
}
}
catch (Exception e)
{
}
Console.WriteLine("Enter number two: ");
num2 = Convert.ToDouble(Console.ReadLine());
You have to use double.TryParse() inside a if condition with negation operator.
If you want to force user to show "Incorrect number!\nType another number:" message till the user enters correct integer, use while() loop instead of if() condition.
while (!double.TryParse(Console.ReadLine(), out num1))
Console.WriteLine("Incorrect number!\nType another number:");
Why we should use double.TryParse() inside if condition?
double.TryParse(), parse string value to double and return true if conversion succeeded or not.
In your case, if user enters word instead of number then conversion will fail and use of !(negation) will iterate the loop again.
This question already has an answer here:
Clear Console Buffer
(1 answer)
Closed 1 year ago.
I'm a beginner in C# so please be patient with me. :) I'm practicing to write a simple C# code to understand how this language works. I have tried to read characters, integers and strings from console but between different tries/methods the buffer never empties. I would like to clear the buffer of console between each methods, for example:
using System;
namespace ElsoKonzolAppom
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Write something!");
string text1 = Console.ReadLine();
Console.WriteLine("Your text:" + text1);
//here, I would like to clear the buffer
int number1 = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Your first number:" + number1);
//here, I would like to clear the buffer
int number2 = Console.Read();
Console.WriteLine("Your second number:" + number2);
}
}
}
Probably this is something very trivial that I haven’t mastered yet.
As far i understood you want to clear input buffer? There is no method in console class for it. Try this:
while(Console.KeyAvailable)
Console.ReadKey(false); // skips previous inputs
Console.ReadKey(); // reads a new char
Use Console.ReadKey(true) if you don't want to print skipped chars.
If you want to clear the screen use:
Console.Clear()
Regards
you can use Console.ReadKey(false); // true = hide input
This question already has answers here:
Difference between Console.Read() and Console.ReadLine()?
(12 answers)
Closed 1 year ago.
I don't get why my integer isn't coming out correctly, Console.Read() method says it's returning an integer, why isn't WriteLine displaying it correctly?
int dimension;
dimension = Console.Read();
Console.WriteLine(""+ dimension);
Console.Read() only returns the first character of what was typed. You should be using Console.ReadLine():
Example:
int suppliedInt;
Console.WriteLine("Please enter a number greater than zero");
Int32.TryParse(Console.ReadLine(), out suppliedInt);
if (suppliedInt > 0) {
Console.WriteLine("You entered: " + suppliedInt);
}
else {
Console.WriteLine("You entered an invalid number. Press any key to exit");
}
Console.ReadLine();
Additional Resources:
MSDN - Console.Read()
MSDN - Console.ReadLine()
From the MSDN:
Return Value
Type: System.Int32 The next character from the input stream, or
negative one (-1) if there are currently no more characters to be
read.
Your program is returning but you're not seeing, would you please see below code block:
You are not be able to see the output if the output window doesn't stay.
int dimension;
dimension = Console.Read();
Console.WriteLine("" + dimension);
Console.ReadLine();
Console.Read() returns ASCII code of first symbol in input. You can do
int dimension;
dimension = Console.Read();
Console.WriteLine(""+ (char)dimension);
and you'll see right first symbol in input, as
(char)dimension
will give you symbol by it's ASCII code.
int a = 0;
if(Int32.TryParse(Console.ReadLine(), out a))
{
// Do your calculations with 'a'
}
else
{
// Some warnings
}
The Console.Read method returns only a single character wrapped in an int, so that is only applicable if you are reading a number that is only one digit long, otherwise you'll always get only the first digit.
Since the return value of Read is actually a character, you cannot use it directly as an integer, you would need to parse it from character to integer.
But assuming that you want a number that is longer than one digit, then you really need to use Console.ReadLine instead and convert the input to an integer using int.TryParse. If int.TryParse returns false you can warn the user that he provided an invalid input and ask for the dimension again.
Sample code:
int dimension;
bool isValidDimension;
do
{
Console.Write("Dimension: ");
string input = Console.ReadLine();
isValidDimension = int.TryParse(input, out dimension);
if (!isValidDimension)
{
Console.WriteLine("Invalid dimension... please try again.");
Console.WriteLine();
}
} while (!isValidDimension);
you should it as follow
static void Main()
{
int Number;
string strNumber;
strNumber = Console.ReadLine();
Number = int.Parse(strNumber);
Console.WriteLine("" + dimension);
}
As you can probably tell from my question, I am very new to coding. I am trying to make a calculator that computes some formulas that are used in physics. However, the code runs the formula before the user has time to enter a value for A, in this example at least. Here is the example:
case "f = ma":
Console.WriteLine("Type the value for M in KG:");
var FM = Console.Read();
Console.WriteLine("Type the value for A in M/S:");
var FA = Console.Read();
var FMARes = FM * FA;
Console.WriteLine("Your answer (in Newtowns) is " + FMARes);
break;
How am I able to check whether a value has been assigned to the variable A, and only run the formula after the variable has an assigned value? Thanks.
You need to use ReadLine instead of Read. You also need to do another ReadLine at the bottom so the user can see the result. And...you should validate that the user entered a valid number. This could be refactored a bit to avoid duplicate code - etc. - but see if this works for you! Good luck!!
static void Main(string[] args)
{
double fm;
double fa;
// Use ReadLine instead of Read
Console.WriteLine("Type the value for M in KG:");
var input = Console.ReadLine();
// Now you need to cast it to a double -
// -- but only if the user entered a valid number
if (!double.TryParse(input, out fm))
{
Console.WriteLine("Please enter a valid number for M");
Console.ReadLine();
return;
}
Console.WriteLine("Type the value for A in M/S:");
input = Console.ReadLine();
if (!double.TryParse(input, out fa))
{
Console.WriteLine("Please enter a valid number for A");
Console.ReadLine();
return;
}
// Now we have valid values for fa and fm
// It's a better programming practice to use the string format
// intead of + here...
Console.WriteLine($"Your answer (in Newtowns) is {fm * fa}");
// You need another read here or the program will just exit
Console.WriteLine("Press Enter to end the program");
Console.ReadLine();
}
This question already has answers here:
Difference between Console.Read() and Console.ReadLine()?
(12 answers)
Closed 1 year ago.
I don't get why my integer isn't coming out correctly, Console.Read() method says it's returning an integer, why isn't WriteLine displaying it correctly?
int dimension;
dimension = Console.Read();
Console.WriteLine(""+ dimension);
Console.Read() only returns the first character of what was typed. You should be using Console.ReadLine():
Example:
int suppliedInt;
Console.WriteLine("Please enter a number greater than zero");
Int32.TryParse(Console.ReadLine(), out suppliedInt);
if (suppliedInt > 0) {
Console.WriteLine("You entered: " + suppliedInt);
}
else {
Console.WriteLine("You entered an invalid number. Press any key to exit");
}
Console.ReadLine();
Additional Resources:
MSDN - Console.Read()
MSDN - Console.ReadLine()
From the MSDN:
Return Value
Type: System.Int32 The next character from the input stream, or
negative one (-1) if there are currently no more characters to be
read.
Your program is returning but you're not seeing, would you please see below code block:
You are not be able to see the output if the output window doesn't stay.
int dimension;
dimension = Console.Read();
Console.WriteLine("" + dimension);
Console.ReadLine();
Console.Read() returns ASCII code of first symbol in input. You can do
int dimension;
dimension = Console.Read();
Console.WriteLine(""+ (char)dimension);
and you'll see right first symbol in input, as
(char)dimension
will give you symbol by it's ASCII code.
int a = 0;
if(Int32.TryParse(Console.ReadLine(), out a))
{
// Do your calculations with 'a'
}
else
{
// Some warnings
}
The Console.Read method returns only a single character wrapped in an int, so that is only applicable if you are reading a number that is only one digit long, otherwise you'll always get only the first digit.
Since the return value of Read is actually a character, you cannot use it directly as an integer, you would need to parse it from character to integer.
But assuming that you want a number that is longer than one digit, then you really need to use Console.ReadLine instead and convert the input to an integer using int.TryParse. If int.TryParse returns false you can warn the user that he provided an invalid input and ask for the dimension again.
Sample code:
int dimension;
bool isValidDimension;
do
{
Console.Write("Dimension: ");
string input = Console.ReadLine();
isValidDimension = int.TryParse(input, out dimension);
if (!isValidDimension)
{
Console.WriteLine("Invalid dimension... please try again.");
Console.WriteLine();
}
} while (!isValidDimension);
you should it as follow
static void Main()
{
int Number;
string strNumber;
strNumber = Console.ReadLine();
Number = int.Parse(strNumber);
Console.WriteLine("" + dimension);
}