double UserInput;
string y, z;
double OunceToGram, PoundToOunce, PoundToKilogram, PintToLitre, InchToCenti, MilesToInch;
OunceToGram = UserInput * 28.0; //(error is here, it cannot find UserInput)
PoundToOunce = UserInput * 16.0;
PoundToKilogram = UserInput * 0.454;
PintToLitre = UserInput * 0.568;
InchToCenti = UserInput * 2.5;
MilesToInch = UserInput * 63360.0;
int i = 0;
while (i < UserInput)
{
Console.WriteLine("");
Console.WriteLine("Please enter a unit to convert, type a num <1 to cancel");
UserInput = Convert.ToDouble(Console.ReadLine());
You could resolve your problem not converting immediately the user input to your variable UserInput and checking if the user types a conventional letter to stop the input
double UserInput = 0.0; // <- You need to initialize before using it ...
.....
string stopInput = "N";
while (stopInput != "Q"))
{
Console.WriteLine("");
Console.WriteLine("Please enter a unit to convert, type 'Q' to cancel");
stopInput = Console.ReadLine();
if(stopInput == "Q")
break;
UserInput = Convert.ToDouble(stopInput);
....
}
Related
so basically whenever i try to change the variable "restart" inside of the while loop it gives me an error saying "a local or parameter named 'restart' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter". if anyone knows how to fix this please tell me
public class Program
{
public static void Main()
{
bool restart = true;
while (restart == true)
{
Console.WriteLine("please enter Multiply, Divide, Add Or Subtract");
string Method = Console.ReadLine();
if (Method == "Add")
{
Console.WriteLine("Please Enter First Number");
double numberOne = Convert.ToDouble(Console.ReadLine());
Console.WriteLine("Please Enter The Second Number");
double numberTwo = Convert.ToDouble(Console.ReadLine());
Console.WriteLine(numberOne + numberTwo);
Console.WriteLine("Would You Like To Use The Calculator Again? (true = yes, false = no)");
bool restart = Convert.ToBoolean(Console.ReadLine());
}
else if (Method == "Subtract")
{
Console.WriteLine("Please Enter First Number");
double numberOne = Convert.ToDouble(Console.ReadLine());
Console.WriteLine("Please Enter The Second Number");
double numberTwo = Convert.ToDouble(Console.ReadLine());
Console.WriteLine(numberOne - numberTwo);
Console.WriteLine("Would You Like To Use The Calculator Again? (true = yes, false = no)");
bool restart = Convert.ToBoolean(Console.ReadLine());
}
else if (Method == "Multiply")
{
Console.WriteLine("Please Enter First Number");
double numberOne = Convert.ToDouble(Console.ReadLine());
Console.WriteLine("Please Enter The Second Number");
double numberTwo = Convert.ToDouble(Console.ReadLine());
Console.WriteLine(numberOne * numberTwo);
Console.WriteLine("Would You Like To Use The Calculator Again? (true = yes, false = no)");
bool restart = Convert.ToBoolean(Console.ReadLine());
}
else if (Method == "Divide")
{
Console.WriteLine("Please Enter First Number");
double numberOne = Convert.ToDouble(Console.ReadLine());
Console.WriteLine("Please Enter The Second Number");
double numberTwo = Convert.ToDouble(Console.ReadLine());
Console.WriteLine(numberOne / numberTwo);
Console.WriteLine("Would You Like To Use The Calculator Again? (true = yes, false = no)");
bool restart = Convert.ToBoolean(Console.ReadLine());
}
else
{
Console.WriteLine("error found");
Console.WriteLine("Would You Like To Use The Calculator Again? (true = yes, false = no)");
bool restart = Convert.ToBoolean(Console.ReadLine());
}
}
}
}```
Let's look at this part of your code:
bool restart = true;
while (restart == true)
{
// Some stuff omitted...
bool restart = Convert.ToBoolean(Console.ReadLine());
}
You redeclare restart. Instead, you should just assign it like this (leave bool off when you use the variable after first declaring it):
restart = Convert.ToBoolean(Console.ReadLine());
You need something like this:
bool restart = true;
while (restart)
{
Console.WriteLine("please enter Multiply, Divide, Add Or Subtract");
string method = Console.ReadLine();
if (new[] { "Multiply", "Divide", "Add", "Subtract" }.Contains(method))
{
Console.WriteLine("Please Enter First Number");
double numberOne = Convert.ToDouble(Console.ReadLine());
Console.WriteLine("Please Enter The Second Number");
double numberTwo = Convert.ToDouble(Console.ReadLine());
if (method == "Add")
{
Console.WriteLine(numberOne + numberTwo);
}
else if (method == "Subtract")
{
Console.WriteLine(numberOne - numberTwo);
}
else if (method == "Multiply")
{
Console.WriteLine(numberOne * numberTwo);
}
else if (method == "Divide")
{
Console.WriteLine(numberOne / numberTwo);
}
Console.WriteLine("Would You Like To Use The Calculator Again? (true = yes, false = no)");
restart = Convert.ToBoolean(Console.ReadLine());
}
else
{
Console.WriteLine("error found");
Console.WriteLine("Would You Like To Use The Calculator Again? (true = yes, false = no)");
restart = Convert.ToBoolean(Console.ReadLine());
}
}
Or this:
Dictionary<string, Func<double, double, double>> operations =
new Dictionary<string, Func<double, double, double>>()
{
{ "Add", (x, y) => x + y },
{ "Subtract", (x, y) => x - y },
{ "Multiply", (x, y) => x * y },
{ "Divide", (x, y) => x / y },
};
bool restart = true;
while (restart)
{
Console.WriteLine($"Please enter one of: {String.Join(", ", operations.Keys)}");
string method = Console.ReadLine();
if (operations.ContainsKey(method))
{
Console.WriteLine("Please Enter First Number");
double numberOne = Convert.ToDouble(Console.ReadLine());
Console.WriteLine("Please Enter The Second Number");
double numberTwo = Convert.ToDouble(Console.ReadLine());
Console.WriteLine(operations[method](numberOne, numberTwo));
}
else
{
Console.WriteLine("error found");
}
Console.WriteLine("Would You Like To Use The Calculator Again? (true = yes, false = no)");
restart = Convert.ToBoolean(Console.ReadLine());
}
I'm relatively new to programming and I'm working on various projects to get better at it. One is a calculator that I want to be able to solve more than just basic addition and subtraction. Currently, all I have is the basics with the quadratic formula. But my design weighs on user input to decide what to do. It prompts the user with "What would you like to do? Add: Sub: Div: Mul: Quad Equation:" After typic in "Add" the code operates normally but if I type in anything else like "Sub" or "Div". it does nothing. doesn't even spit back an error. As far as I can tell, my code is fine (well it's terrible code but it should work nonetheless) But I just do not know how to proceed.
using System;
namespace Better_Calculator
{
class Program
{
static void Main(string[] args)
{
double num01;
double num02;
double a;
double b;
double c;
System.Console.WriteLine("Welcome to how you are going to cheat through math lol");
System.Console.WriteLine("What would you like to do? \nAdd: \nSub: \nDiv: \nMul: \nQuad Equation: ");
if (Console.ReadLine() == "Add")
{
System.Console.WriteLine("what is the first number?");
num01 = Convert.ToInt32(Console.ReadLine());
System.Console.WriteLine("what is the second number?");
num02 = Convert.ToInt32(Console.ReadLine());
Add(num01, num02);
}
else if (System.Console.ReadLine() == "Sub")
{
System.Console.WriteLine("what is the first number?");
num01 = Convert.ToInt32(Console.ReadLine());
System.Console.WriteLine("what is the second number?");
num02 = Convert.ToInt32(Console.ReadLine());
Sub(num01, num02);
}
else if (System.Console.ReadLine() == "Mul")
{
System.Console.WriteLine("what is the first number?");
num01 = Convert.ToInt32(Console.ReadLine());
System.Console.WriteLine("what is the second number?");
num02 = Convert.ToInt32(Console.ReadLine());
Mul(num01, num02);
}
else if (System.Console.ReadLine() == "Div")
{
System.Console.WriteLine("what is the first number?");
num01 = Convert.ToInt32(Console.ReadLine());
System.Console.WriteLine("what is the second number?");
num02 = Convert.ToInt32(Console.ReadLine());
Div(num01, num02);
}
else if (System.Console.ReadLine() == "Quad Equation")
{
System.Console.WriteLine("what is a?");
a = Convert.ToInt32(Console.ReadLine());
System.Console.WriteLine("what is b");
b = Convert.ToInt32(Console.ReadLine());
System.Console.WriteLine("what is c");
c = Convert.ToInt32(Console.ReadLine());
Quad(a, b, c);
}
static void Add(double num01, double num02)
{
string answer = Convert.ToString(num01 + num02);
System.Console.WriteLine(answer);
}
static void Sub(double num01, double num02)
{
string answer = Convert.ToString(num01 - num02);
System.Console.WriteLine(answer);
}
static void Mul(double num01, double num02)
{
string answer = Convert.ToString(num01 * num02);
System.Console.WriteLine(answer);
}
static void Div(double num01, double num02)
{
string answer = Convert.ToString(num01 / num02);
System.Console.WriteLine(answer);
}
static void Quad(double a, double b, double c)
{
double bNeg = b * -1;
double bSqr = b * b;
double SqR = Math.Sqrt(bSqr - (4 * a * c));
double solvedAdd = (bNeg + SqR) / (2 * a);
double solvedSub = (bNeg - SqR) / (2 * a);
string answer = Convert.ToString(solvedAdd) + " or " + Convert.ToString(solvedSub);
System.Console.WriteLine(answer);
}
}
}
}
With every ´if´, you call Console.ReadLine() again, so if you enter "Quad Equation", your code has passed 5 Readlines until you reach the code that does Quad Equation.
Solution: Do only one Console.ReadLine() and put its result into a variable:
var userInput = Console.ReadLine();
and then, test against userInput in your if statements (ex.)e:
else if (userInput == "Sub")
You only need to do a ReadLine once and store the value, then use that to compare. For example:
var operation = Console.ReadLine();
if(operation == "Add")
{
// Do add stuff
}
But you should consider using a switch statement instead:
switch(operation)
{
case "Add":
// do Add Stuff
break;
case "Sub":
// do Add Stuff
break;
case "Mul":
// do Add Stuff
break;
// etc...
}
When we get input from console application from user may be they given as
CAPS, Small or combination of both
. In that situation you can use the following approach,
string value=Console.ReadLine();
if(string.Equals(value, "Add", StringComparison.CurrentCultureIgnoreCase))
{
// Do add stuff
}
else if(string.Equals(value, "Sub", StringComparison.CurrentCultureIgnoreCase))
{
// Do add stuff
}
I've tested a simple calculator functions as a part of my C# studies and I have a problem, it won't exit the while loop even if I type the right choise.
Here is the code:
static void Main(string[] args)
{
string calc, inputX, inputY, inputZ;
double x, y, z;
Console.Write("Welcome to the cool calculator. Please choose between sumCalc or multiCalc: ");
calc = Console.ReadLine();
while (calc != "sumCalc" || calc != "sumCalc")
{
Console.Write("Please type in the right calculator again: ");
calc = Console.ReadLine();
}
if (calc == "sumCalc")
{
Console.WriteLine("You are now working with the sumCalc.");
//Getting user input for the variable 'x'
Console.WriteLine("Please enter a value for the first number:");
inputX = Console.ReadLine();
x = Convert.ToDouble(inputX);
//Getting user input for the variable 'y'
Console.WriteLine("Please enter a value for the second number:");
inputY = Console.ReadLine();
y = Convert.ToDouble(inputY);
//Getting user input for the variable 'z'
Console.WriteLine("Please enter a value for the third number:");
inputZ = Console.ReadLine();
z = Convert.ToDouble(inputZ);
Console.WriteLine("The result is:" + sumCalc(x, y, z));
Console.ReadKey();
}
else if (calc == "multiCalc")
{
Console.WriteLine("You are now working with the multiCalc.");
//Getting user input for the variable 'x'
Console.WriteLine("Please enter a value for the first number:");
inputX = Console.ReadLine();
x = Convert.ToDouble(inputX);
//Getting user input for the variable 'y'
Console.WriteLine("Please enter a value for the second number:");
inputY = Console.ReadLine();
y = Convert.ToDouble(inputY);
//Getting user input for the variable 'z'
Console.WriteLine("Please enter a value for the third number:");
inputZ = Console.ReadLine();
z = Convert.ToDouble(inputZ);
Console.WriteLine("The result is:" + multiCalc(x, y, z));
Console.ReadKey();
}
}
//This is the multiply calculator function
static double sumCalc(double x, double y, double z)
{
double res = x + y + z;
return res;
}
//This is the multiply calculator function
static double multiCalc(double x, double y, double z)
{
double res = x * y * z;
return res;
}
I don't know why it does that, it should work just fine.
Please help me, thanks! :)
You have sumCalc twice in while condition. Also change || to &&, if you use OR as the condition, even if you enter sumCalc OR multiCalc, the loop will be true and keep asking you to enter again.
while (calc != "sumCalc" && calc != "multiCalc")
Try to change your code :
while (calc != "sumCalc" && calc != "multiCalc")
instead of :
while (calc != "sumCalc" || calc != "sumCalc")
I need some help making sure that when the user enters nothing, or not a number, it doesn't crash. I have it down so that it will tell them to enter a number if they don't, but I don't know how to make it go back to the original question and give them a chance to input correctly. Thanks for the help.
Console.WriteLine("How much is rent: ");
string strRent = Console.ReadLine();
double dblRent = 0.0;
if (double.TryParse(strRent, out dblRent))
{
Console.WriteLine("How much is the car payment: ");
string strCarPayment = Console.ReadLine();
double dblCarPayment = Convert.ToDouble(strCarPayment);
}
else
{
Console.WriteLine("Enter a number");
}
Console.WriteLine("How much is student loan payment: ");
string strStudentLoan = Console.ReadLine();
Console.WriteLine("How much is phone bill: ");
string strPhoneBill = Console.ReadLine();
Console.WriteLine("How much is electric bill: ");
string strElectricBill = Console.ReadLine();
Console.WriteLine("Fraction deposited: ");
string strFractionDeposited = Console.ReadLine();
Console.WriteLine("Amount leftover: ");
string strAmountLeft = Console.ReadLine();
double dblStudentLoan = Convert.ToDouble(strStudentLoan);
double dblPhoneBill = Convert.ToDouble(strPhoneBill);
double dblElectricBill = Convert.ToDouble(strElectricBill);
double dblFractionDeposited = Convert.ToDouble(strFractionDeposited);
double dblAmountLeft = Convert.ToDouble(strAmountLeft);
double dblBillSum = dblRent + dblCarPayment + dblStudentLoan + dblPhoneBill + dblElectricBill;
double afterBills = dblAmountLeft / (1 - dblFractionDeposited);
double totalPaycheck = afterBills + dblBillSum;
Console.WriteLine("Total Paycheck: " + totalPaycheck.ToString("C"));
Console.WriteLine("Enter wage $/hr: ");
string strWage = Console.ReadLine();
Console.WriteLine("Enter hours worked overtime: ");
string strHoursOT = Console.ReadLine();
Console.WriteLine("Enter overtime multiplier: ");
string strOTWage = Console.ReadLine();
double dblWage = Convert.ToDouble(strWage);
double dblHoursOT = Convert.ToDouble(strHoursOT);
double dblOTWage = Convert.ToDouble(strOTWage);
double OTPay = (dblWage * dblOTWage) * dblHoursOT;
Console.WriteLine("Overtime Pay: " + OTPay.ToString("C"));
Console.ReadLine();
}
}
}
You need a loop, something like this
Console.WriteLine("How much is rent: ");
string strRent = Console.ReadLine();
double dblRent = 0.0;
while (!double.TryParse(strRent, out dblRent))
{
Console.WriteLine("Enter a number");
strRent = Console.ReadLine();
}
Console.WriteLine("How much is the car payment: ");
string strCarPayment = Console.ReadLine();
double dblCarPayment = Convert.ToDouble(strCarPayment);
Try this
double dblRent = 0.0;
Boolean valid = false;
while (!valid)
{
if (double.TryParse(strRent, out dblRent))
{
Console.WriteLine("How much is the car payment: ");
string strCarPayment = Console.ReadLine();
double dblCarPayment = Convert.ToDouble(strCarPayment);
valid = true;
}
else
{
Console.WriteLine("Enter a number");
}
}
Since you are reading many doubles, you could create a method encapsulating reading and processing the number
private static double? ReadLineDouble()
{
while(true) {
string s = Console.ReadLine();
if (String.IsNullOrWhitespace(s)) {
return null; // The user wants to abort
}
double d;
if (Double.TryParse(s, out d)) {
return d;
}
Console.WriteLine("Please enter a valid number");
}
}
If the user just hits Enter probably he wants to abort. In this case the method returns null. This is possible, because the return type is a Nullable<double>. The shorthand notation for it is double?.
Now you can read a number like this:
double? rent = ReadLineDouble();
if (double == null) return; // Abort the program.
// Otherwise continue.
I am trying to create a program for a hotel where the user is to enter a character (either S, D, or L) and that is supposed to correspond with a code further down the line. I need help converting the user input (no matter what way they enter it) to be converted to uppercase so I can then use an if statement to do what I need to do.
My code so far is the following:
public static void Main()
{
int numdays;
double total = 0.0;
char roomtype, Continue;
Console.WriteLine("Welcome to checkout. We hope you enjoyed your stay!");
do
{
Console.Write("Please enter the number of days you stayed: ");
numdays = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("S = Single, D = Double, L = Luxery");
Console.Write("Please enter the type of room you stayed in: ");
roomtype = Convert.ToChar(Console.ReadLine());
**^Right Her is Where I Want To Convert To Uppercase^**
total = RoomCharge(numdays,roomtype);
Console.WriteLine("Thank you for staying at our motel. Your total is: {0}", total);
Console.Write("Do you want to process another payment? Y/N? : ");
Continue = Convert.ToChar(Console.ReadLine());
} while (Continue != 'N');
Console.WriteLine("Press any key to end");
Console.ReadKey();
}
public static double RoomCharge(int NumDays, char RoomType)
{
double Charge = 0;
if (RoomType =='S')
Charge = NumDays * 80.00;
if (RoomType =='D')
Charge= NumDays * 125.00;
if (RoomType =='L')
Charge = NumDays * 160.00;
Charge = Charge * (double)NumDays;
Charge = Charge * 1.13;
return Charge;
}
Try default ToUpper method.
roomtype = Char.ToUpper(roomtype);
Go through this http://msdn.microsoft.com/en-us/library/7d723h14%28v=vs.110%29.aspx
roomtype = Char.ToUpper(roomtype);
public static void Main()
{
int numdays;
double total = 0.0;
char roomtype, Continue;
Console.WriteLine("Welcome to checkout. We hope you enjoyed your stay!");
do
{
Console.Write("Please enter the number of days you stayed: ");
numdays = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("S = Single, D = Double, L = Luxery");
Console.Write("Please enter the type of room you stayed in: ");
roomtype = Convert.ToChar(Console.ReadLine());
roomtype = Char.ToUpper(roomtype);
total = RoomCharge(numdays,roomtype);
Console.WriteLine("Thank you for staying at our motel. Your total is: {0}", total);
Console.Write("Do you want to process another payment? Y/N? : ");
Continue = Convert.ToChar(Console.ReadLine());
} while (Continue != 'N');
Console.WriteLine("Press any key to end");
Console.ReadKey();
}