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();
}
Related
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.
How can I get the random number generator method to loop and produce new random numbers? Do I need a separate class before Main that handles the random number generation? If so, how do I get the scope of those variables to Main? The console app is supposed to repeat integer math problems to practice with a loop that conditions on correct or incorrect answer. Also, what syntax am I overlooking to loop back to a new instance of randomly generated integers? TIA.
public class Multiplication
{
public static void Main(string[] args)
{
Random randomNumbers = new Random(); // random number generator
int num01 = randomNumbers.Next(1, 11);
int num02 = randomNumbers.Next(1, 11);
int value = 0;
while (true)
{
if (value != -1)
{
Console.Write("Please enter the answer to {0} x {1} = ?", num01, num02);
int product = num01 * num02;
Console.WriteLine();
int answer = Convert.ToInt32(Console.ReadLine());
while (product != answer)
{
Console.WriteLine("Incorrect, enter another guess: ");
answer = Convert.ToInt32(Console.ReadLine());
}
Console.WriteLine("Correct. Your answer was {0} \n {1} x {2} = {3} Very good!",
answer, num01, num02, product);
//keep console open
Console.WriteLine();
Console.WriteLine("Press - 1 to exit");
value = Convert.ToInt32(Console.ReadLine());
}
else
{
break;
}
}
}
}
Put the random number generation inside the loop, not before it.
int value = 0;
while (true)
{
if (value != -1)
{
int num01 = randomNumbers.Next(1, 11);
int num02 = randomNumbers.Next(1, 11);
...
The random number generator statement had to be in the loop. And I needed to tweak the console.writeline to include "type 1 to continue". So the final code is:
public class Multiplication
{
public static void Main(string[] args)
{
int value = 1;
while (true)
{
if (value == -1)
{
break;
}
else
{
Random randomNumbers = new Random(); // random number generator
int num01 = randomNumbers.Next(1, 11);
int num02 = randomNumbers.Next(1, 11);
Console.Write("Please enter the answer to {0} x {1} = ?", num01, num02);
int product = num01 * num02;
Console.WriteLine();
int answer = Convert.ToInt32(Console.ReadLine());
while (product != answer)
{
Console.WriteLine("Incorrect, enter another guess: ");
answer = Convert.ToInt32(Console.ReadLine());
}
Console.WriteLine("Correct. Your answer was {0} \n {1} x {2} = {3} Very good!",
answer, num01, num02, product);
//keep console open
Console.WriteLine();
Console.WriteLine("Press - 1 to exit or 1 to continue");
value = Convert.ToInt32(Console.ReadLine());
}
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConTempConversion_LeeMichelle
{
class Program
{
static void Main(string[] args)
{
double fTemp;
double cTemp;
double convertC;
double convertF;
Console.WriteLine("Welcome to the temperature conversion application");
Console.WriteLine("_________________________________________________");
Console.WriteLine("1. Fahrenheit to Celsius");
Console.WriteLine("2. Celsius to Fahrenheit");
Console.WriteLine("3. Exit");
Console.Write("Enter choice: ");
Console.ReadKey();
int ichoice = 0;
do
{
if (ichoice == 1)
{
Console.WriteLine("Enter Fahrenheit temperature: ");
fTemp = int.Parse(Console.ReadLine());
convertC = ConvertCelcius(fTemp);
Console.WriteLine(fTemp + "Fahrenheit is " + convertC + "Celsius");
Console.WriteLine("Welcome to the temperature conversion application");
Console.WriteLine("_________________________________________________");
Console.ReadKey();
}
if (ichoice == 2)
{
Console.WriteLine("Enter Celsius temperature: ");
cTemp = int.Parse(Console.ReadLine());
convertF = ConvertFahrenheit(cTemp);
Console.WriteLine(cTemp + "Celsius is " + convertF + "Fahrenheit");
Console.WriteLine("Welcome to the temperature conversion application");
Console.WriteLine("____________________________________________________");
Console.ReadKey();
}
if (ichoice == 3)
{
Console.WriteLine("Thank you for using the temperature conversion application. Please come again.");
}
else
{
Console.WriteLine("Invalid choice. Please choose again!");
}
}
while (ichoice > 3);
Console.ReadKey();
}
static double ConvertCelcius(double c){
double f;
return f= 9.0 / 5.0 * c + 32;
}
static double ConvertFahrenheit(double f) {
double c;
return c = 5.0 / 9.0 * (f - 32);
}
}
}
so suppose the user has three options from 1 - 3. After option 3, there will be an error alert. my problem is why this code gives me an infinity loop? what did I do wrong? can I put break after each if statment?
Please help, thank you!
You're creating a do...while loop without anything to ever exit it. When the loop begins, ichoice is already set in stone. So you'll always be stuck in that loop!
You are never assigning a value to ichoice If I were you, I would try something like this:
int ichoice = 0;
int.TryParse(Console.ReadKey().ToString(), out ichoice );
do
{
if (ichoice == 1)
{
Console.WriteLine("Enter Fahrenheit temperature: ");
fTemp = int.Parse(Console.ReadLine());
convertC = ConvertCelcius(fTemp);
Console.WriteLine(fTemp + "Fahrenheit is " + convertC + "Celsius");
Console.WriteLine("Welcome to the temperature conversion application");
Console.WriteLine("_________________________________________________");
int.TryParse(Console.ReadKey().ToString(), out ichoice );
Add the int.TryParse(Console.ReadKey().ToString(), out ichoice ); everywhere you have Console.Readline(). What this is doing is getting the user input and converting it to an integer (if it's not an integer, then the value is 0).
Also add this:
if (ichoice == 3)
{
Console.WriteLine("Thank you for using the temperature conversion application. Please come again.");
break;
}
else
{
Console.WriteLine("Invalid choice. Please choose again!");
int.TryParse(Console.ReadKey().ToString(), out ichoice );
}
Finally, remove this (I'm not quite sure what it was for...)
while (ichoice > 3);
Console.ReadKey();
When you need to do some prep work before your condition, the most straightforward way involves combining your loop with a separate structure.
This is the format to use when your loop condition is at the beginning of the loop:
prep work
while (condition) // if false then loop body will never run
{
loop body
}
This is the format to use when the loop condition is at the end of the loop:
prep work
do
{
loop body
}
while (condition); // loop body will always execute at least once
In this case, you have some prep work to do before each iteration--you want to get the key press from the user each time. For this, I recommend the following from Code Complete:
while (true) // loop termination condition is inside the loop
{
prep work
if (condition)
{
break;
}
loop body
}
In your case, it would look something like:
Console.Write("Enter choice: ");
while (true) // loop termination condition is inside the loop
{
Console.ReadKey();
if (ichoice == 3)
{
Console.WriteLine(...);
break;
}
...
}
Also, consider changing most of your ifs into else ifs; otherwise, you're repeatedly checking the condition even when you've gone through one case.
you should assign a value to iChoice. and set your while to read a terminating character. like:
namespace YourNameSpace
{
static void Main(string[] args)
{
double fTemp;
double cTemp;
double convertC;
double convertF;
int iChoice;
Console.WriteLine("Welcome to the temperature conversion application");
Console.WriteLine("_________________________________________________");
Console.WriteLine("1. Fahrenheit to Celsius");
Console.WriteLine("2. Celsius to Fahrenheit");
Console.Write("Enter choice(0 to exit): ");
iChoice = Console.Read();
do{
switch(iChoice)
{
case 1:
Console.WriteLine("Enter Fahrenheit temperature: ");
fTemp = int.Parse(Console.ReadLine());
convertC = ConvertCelcius(fTemp);
Console.WriteLine(fTemp + "Fahrenheit is " + convertC + "Celsius");
case 2:
Console.WriteLine("Enter Celsius temperature: ");
cTemp = int.Parse(Console.ReadLine());
convertF = ConvertFahrenheit(cTemp);
Console.WriteLine(cTemp + "Celsius is " + convertF + "Fahrenheit");
}
Console.WriteLine("Welcome to the temperature conversion application");
Console.WriteLine("_________________________________________________");
Console.WriteLine("1. Fahrenheit to Celsius");
Console.WriteLine("2. Celsius to Fahrenheit");
Console.Write("Enter choice(0 to exit): ");
iChoice = Console.Read();
}while(iChoice != 0);
}
static double ConvertCelcius(double c){
double f;
return f= 9.0 / 5.0 * c + 32;
}
static double ConvertFahrenheit(double f) {
double c;
return c = 5.0 / 9.0 * (f - 32);
}
}
Each time you call Console.ReadKey you should store returning value.You are receiving input from user but you didn't store it anywhere.
First you can create a function to get ichoice, and force user to enter the correct value:
static void GetChoice(ref int ichoice)
{
string input = Console.ReadLine();
Console.Write("Enter choice: ");
bool result = int.TryParse(input, out ichoice);
if (!result)
{
while (!result && ichoice > 3)
{
Console.WriteLine("Invalid value.Try again:");
input = Console.ReadLine();
result = int.TryParse(input, out ichoice);
}
}
}
Then use this function whenever you need to make a choice.
Here is the working version or your program:
double fTemp;
double cTemp;
double convertC;
double convertF;
Console.WriteLine("Welcome to the temperature conversion application");
Console.WriteLine("_________________________________________________");
Console.WriteLine("1. Fahrenheit to Celsius");
Console.WriteLine("2. Celsius to Fahrenheit");
Console.WriteLine("3. Exit");
int ichoice = 0;
GetChoice(ref ichoice);
do
{
if (ichoice == 1)
{
Console.WriteLine("Enter Fahrenheit temperature: ");
fTemp = int.Parse(Console.ReadLine());
convertC = ConvertCelcius(fTemp);
Console.WriteLine(fTemp + "Fahrenheit is " + convertC + "Celsius");
Console.WriteLine("Welcome to the temperature conversion application");
Console.WriteLine("_________________________________________________");
GetChoice(ref ichoice);
}
if (ichoice == 2)
{
Console.WriteLine("Enter Celsius temperature: ");
cTemp = int.Parse(Console.ReadLine());
Console.WriteLine(cTemp + "Celsius is " + cTemp + "Fahrenheit");
Console.WriteLine("Welcome to the temperature conversion application");
Console.WriteLine("____________________________________________________");
GetChoice(ref ichoice);
}
if (ichoice == 3)
{
Console.WriteLine("Thank you for using the temperature conversion application. Please come again.");
}
else
{
Console.WriteLine("Invalid choice. Please choose again!");
}
}
while (ichoice < 3);
}
static double ConvertCelcius(double c)
{
double f;
return f = 9.0 / 5.0 * c + 32;
}
static double ConvertFahrenheit(double f)
{
double c;
return c = 5.0 / 9.0 * (f - 32);
}
And here is the full code: http://pastebin.com/bkegSAFL
I'm using C#. I am trying to allow the user to input a numerical value (IE: how many do you want?) and then take that value and figure tax and total. I can not figure out how to do this and wondered if anyone could show me? My current script is below:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Welcome to Infinate Happiness Ranch.\nPlease enter your order information bellow. ");
Console.WriteLine();
Console.WriteLine("Please enter your first and last name:");
string FirstName = Console.ReadLine();
Console.WriteLine("Please enter your street address:");
string Address = Console.ReadLine();
Console.WriteLine("Please enter your city:");
string City = Console.ReadLine();
Console.WriteLine("Please enter your two letter state abbreviation:");
string StateCode = Console.ReadLine();
Console.WriteLine("Please enter your zip code:");
string ZipCode = Console.ReadLine();
Console.WriteLine("Please enter the number of Tribbles \nyou wish to purchase for $29.99 plus tax");
string NumberOrdered = Console.ReadLine();
Console.WriteLine("Invoice \nName {0}", FirstName);
Console.WriteLine("Address {0}", Address);
Console.WriteLine("City {0}", City);
Console.WriteLine("StateCode {0}", StateCode);
Console.WriteLine("ZipCode {0}", ZipCode);
Console.WriteLine("NumberOrdered {0}", NumberOrdered);
// PROGRAM WORKS UNTIL HERE.
NumberOrdered = m;
TotalBeforeTax = m * 29.99; //'n' is total b4 tax
o = n * 0.9;// 'o' is total tax due
p = o + n; // 'p' is total due
Console.WriteLine("Your total is {0} {1}", n);
Console.WriteLine("Your tax is {0}", o);
Console.WriteLine("Your total charge is {0}", p);
Console.WriteLine("Thank you for your order");
Console.WriteLine();
//Console.WriteLine("Name:" + FirstName);
Console.Read();
}
}
}
You should parse the string with
int ordered = int.Parse(NumberOrdered);
And continue to calculate with this integer.
Because the user is entering information as strings, you should convert the number ordered to an integer. Also, to preserve your decimals, you need to store numbers as doubles for things such amounts.
int numOrdered = Convert.ToInt32(NumberOrdered);
double TotalBeforeTax = numOrdered * 29.99;
double beforeTax = TotalBeforeTax * 0.9;
double afterTax = beforeTax + TotalBeforeTax;
Console.WriteLine("Your total is {0}", TotalBeforeTax);
Console.WriteLine("Your tax is {0}", beforeTax);
Console.WriteLine("Your total charge is {0}", afterTax);
Console.WriteLine("Thank you for your order");
You forgot to declare some variables and assign some values.
Try this:
static float m;
static float n;
static float o;
static float p;
static float TotalBeforeTax;
static void Main(string[] args)
{
Console.WriteLine("Welcome to Infinate Happiness Ranch.\nPlease enter your order information bellow. ");
Console.WriteLine();
Console.WriteLine("Please enter your first and last name:");
string FirstName = Console.ReadLine();
Console.WriteLine("Please enter your street address:");
string Address = Console.ReadLine();
Console.WriteLine("Please enter your city:");
string City = Console.ReadLine();
Console.WriteLine("Please enter your two letter state abreviation:");
string StateCode = Console.ReadLine();
Console.WriteLine("Please enter your zip code:");
string ZipCode = Console.ReadLine();
Console.WriteLine("Please enter the number of Tribbles \nyou wish to purchase for $29.99 plus tax");
string NumberOrdered = Console.ReadLine();
Console.WriteLine("Invoice \nName {0}", FirstName);
Console.WriteLine("Address {0}", Address);
Console.WriteLine("City {0}", City);
Console.WriteLine("StateCode {0}", StateCode);
Console.WriteLine("ZipCode {0}", ZipCode);
Console.WriteLine("NumberOrdered {0}", NumberOrdered);
//PROGRAM WORKS UNTIL HERE ? HELP ? ? ? ? ?
//NumberOrdered = m;
m = float.Parse(NumberOrdered);
TotalBeforeTax = m * 29.99f; //'n' is total b4 tax
n = TotalBeforeTax;
o = n * 0.9f;//'o' is total tax due
p = o + n; //'p' is total due
Console.WriteLine("Your total is {0}", n);
Console.WriteLine("Your tax is {0}", o);
Console.WriteLine("Your total charge is {0}", p);
Console.WriteLine("Thank you for your order");
Console.WriteLine();
Console.Read();
}
Hope this helps !
Just a bit of advice, you can include non-string variables in Console.WriteLine() like:
Console.WriteLine("Your tax is " + o);
This is what most professional developers do. No need for complicated C/C++ style of parsing.
Also, you don't seem to be declaring the variables o and p. Try this:
double o = Convert.toDouble(n * 0.9);
or
double o = (double)(n * 0.9);
So I'm trying to make a console application that calculates interest rates per year. But somehow its crashing down when i assign the deposit year.
The source code is below:
using System;
namespace week0502
{
class Interest
{
static void Main(string[] args)
{
decimal p = 0; // principle
decimal r = 1; // interest rate
decimal t = 2; // time/years entered
decimal i = 3; // interest
decimal a = 4; // new amount
// prompt for values
Console.Write("Enter original deposit amount: ");
p = Convert.ToDecimal(Console.ReadLine());
Console.Write("Enter annual interest rate (10% as 10): ");
r = Convert.ToDecimal(Console.ReadLine());
Console.Write("Enter years to save this deposit amount: ");
t = Convert.ToDecimal(Console.ReadLine());
Console.WriteLine();
// display headers
Console.WriteLine("Year Rate Amount Interest New Amount");
// calculate new amount on deposit after t amount of years
for (int n = 1; n <= t; n++)
{
// calculations
i = p * r;
a = i + p;
// display contents
Console.WriteLine("{0, 4}{1, 4}{2, 10:C}{3, 5}{4, 10:C}", n, r, p, i, a);
} // end for
} // end main
} // end class
} // end name
If you mean it closes after showing the result thats normal if you forget to add Console.ReadLine() (wait for user to press enter before closing the application)
using System;
namespace week0502
{
class Interest
{
static void Main(string[] args)
{
decimal p = 0; // principle
decimal r = 1; // interest rate
decimal t = 2; // time/years entered
decimal i = 3; // interest
decimal a = 4; // new amount
// prompt for values
Console.Write("Enter original deposit amount: ");
p = Convert.ToDecimal(Console.ReadLine());
Console.Write("Enter annual interest rate (10% as 10): ");
r = Convert.ToDecimal(Console.ReadLine());
Console.Write("Enter years to save this deposit amount: ");
t = Convert.ToDecimal(Console.ReadLine());
Console.WriteLine();
// display headers
Console.WriteLine("Year Rate Amount Interest New Amount");
// calculate new amount on deposit after t amount of years
for (int n = 1; n <= t; n++)
{
// calculations
i = p * r;
a = i + p;
// display contents
Console.WriteLine("{0, 4}{1, 4}{2, 10:C}{3, 5}{4, 10:C}", n, r, p, i, a);
} // end for
Console.ReadLine(); //Add this to prevent application from closing
} // end main
} // end class
}