static void Main(string[] args)
{
int choice;
choice = 0;
double length;
length = 0.00;
double height;
height = 0.00; //initiating variables
double base1;
base1 = 0.00;
double width;
width = 0.00;
double radius;
radius = 0.00;
double total;
total = 0.00;
Console.WriteLine("What shape would you like to make?");
Console.WriteLine("Please select one option (1, 2, 3, or 4)");
Console.WriteLine("1. Square.");
Console.WriteLine("2. Triangle."); //menu on the console
Console.WriteLine("3. Rectangle.");
Console.WriteLine("4. Circle.");
choice = int.Parse(Console.ReadLine());
if (choice == 1)
{
Console.WriteLine("Great! You chose Square.");
Console.WriteLine("Let's help you calculate the area of a square.");
Console.WriteLine("But first, we need to know the length of this square");
Console.WriteLine("Enter the length(Integer, or Decimal value is just fine:");
length = double.Parse(Console.ReadLine());
}
else if (choice == 2)
{
Console.WriteLine("Great! You chose Triangle.");
Console.WriteLine("Let's help you calculate the area of a triangle.");
Console.WriteLine("But first, we need to know the base and height of this triangle.");
Console.WriteLine("Enter the base(Integer, or Decimal value is just fine):");
base1 = double.Parse(Console.ReadLine()); //writing to the console depending on what choice
Console.WriteLine("Enter the height(Integer, or Decimal value is just fine):");
height = double.Parse(Console.ReadLine());
}
else if (choice == 3)
{
Console.WriteLine("Great! You chose Rectangle.");
Console.WriteLine("Let's help you calculate the area of a rectangle.");
Console.WriteLine("But first, we need to know the width and height of this rectangle.");
Console.WriteLine("Enter the width(Integer, or Decimal value is just fine):");
width = double.Parse(Console.ReadLine());
Console.WriteLine("Enter the height(Integer, or Decimal value is just fine):");
height = double.Parse(Console.ReadLine());
}
else if (choice == 4)
{
Console.WriteLine("Great! You chose Circle.");
Console.WriteLine("Let's help you calculate the area of a circle.");
Console.WriteLine("But first, we need to know the radius of this circle.");
Console.WriteLine("Enter the radius(Integer, or Decimal value is just fine:");
radius = double.Parse(Console.ReadLine());
}
Main_Calculations(args, total, base1, height, width, choice); //Sending Paramters to the next method
}
static void Main_Calculations(string[] args, double base1, double height, double width, double total, int choice) //bringing in parameters
{
if(choice == 2)
{
total = (base1 * height) * 0.5; //Calculations for triangle
Console.WriteLine("The area of a triangle is: " + total);
}
else if(choice == 3)
{
total = width * height; //calculations for rectangle
Console.WriteLine("The area of a rectangle is: " + total);
}
Main_Calculations(choice, total); //Sending Paramters to the next method
}
static void Main_Calculations(double length, double radius, double total, double choice)
{
if(choice == 1)
{
total = length * length; //Calculations for square
Console.WriteLine("The area of a square is: " + total);
}
else if(choice == 4)
{
total = (radius * radius) * 3.14; //calculations for circle
Console.WriteLine("The area of a circle is " + total); //prints the answer to the console
}
}
}
}
My 2nd sender for (Math_Calculations) is giving me an error for no overload takes 2 arguments, I don't understand why it won't work the way I have it. Seems possibly I am just missing one phrase, I have stepped in and out of the code looking to now avail.
When you call the 2nd method Math_Calculations, you only give 2 arguments to the method when you ask in the method for 4 arguments
So, What you need to do is or, you give 2 arguments extra to the 2dn Math_Calculations call, OR you remove 2 arguments from the method.
This would be easier to answer with line numbers, but you call
Main_Calculations(choice,total);
when there is no method for Main_Calculations that takes two arguments.Just as Habib has pointed out.
Related
I've been working on a two class BMI Calculator for my class. I finally have managed to debug on get the program to execute. However I noticed that it wont let me enter height or weight as a decimal.
Example
putting 6.4 for height causes it to crash
Putting 6 height and 220 weight always seems to declare underweight and show incorrect bmi reading
example:
Height 6
Weight 220
BMI: score $0.09
It's calculating the BMI wrong. Looking at the code I can't seem to identify why its calculating this wrong.
-Edit- Managed to get it to take decimals! (Thank you everyone!) However it still seems to be messing up calculating the BMI
-Edit- Code Works!
(Code Part 1) Updated Mk2
using System;
namespace Calculator
{
public class BMICalc
{
private const
decimal REQ = 703;
private decimal weightPerson;
private decimal heightPerson;
public BMICalc()
{
}
public BMICalc(decimal weiP, decimal heiP)
{
this.weightPerson = weiP;
this.heightPerson = heiP;
}
public decimal SetBmi()
{
decimal bmi;
bmi = Convert.ToDecimal((this.weightPerson * 0.45m) / ((this.heightPerson * 12 * 0.025m) * (this.heightPerson * 12 * 0.025m)));
if (bmi < 18.5m)
{
Console.WriteLine("UnderWeight");
Console.ReadLine();
}
if (bmi > 18.5m && bmi < 25.0m)
{
Console.WriteLine("Normal");
Console.ReadLine();
}
if (bmi > 25.0m && bmi < 29.9m)
{
Console.WriteLine("OverWeight");
Console.ReadLine();
}
if (bmi > 29.9m && bmi < 40.0m)
{
Console.WriteLine("Obese");
Console.ReadLine();
}
return bmi;
}
public override string ToString()
{
return "\tCalculator" +
"\n\n Weight:" + this.weightPerson +
"\n\n Height:" + this.heightPerson +
"\n\n BMI Score:" + SetBmi().ToString();
}
}
}
Code (Part 2) Updated Mk 2
namespace Calculator
{
public class App
{
public static void Main() //
{
decimal heiP, weiP;
heiP = InputHeight();
weiP = InputWeight();
BMICalc bmiCal = new BMICalc(weiP, heiP);
Console.Clear();
Console.WriteLine(bmiCal.ToString());
Console.ReadKey();
}
public static decimal InputHeight()
{
decimal hNumber;
Console.Write("Please enter your height: ");
hNumber = Convert.ToDecimal(Console.ReadLine());
return hNumber;
}
public static decimal InputWeight()
{
Decimal wNumber;
Console.Write("Please enter your weight: ");
wNumber = Convert.ToDecimal(Console.ReadLine());
return wNumber;
}
}
}
You're input parameters into the constructor expects weight (weiP), height (heiP) (in that order) while you're passing height, weight from BMICalc bmiCal = new BMICalc(heiP, weiP);
That's the reason the value being output is incorrect. Interchange the parameters and it should be fine.
EDIT
Since you're entering the weight in Lbs and height in feet, you would need to convert feet into inches ( multiplying by 12) and then also multiplying the height and weight by the conversion metric.
So the formula would be:
bmi = Convert.ToDecimal((this.weightPerson * 0.45) / ((this.heightPerson * 12 * 0.025) * (this.heightPerson * 12 * 0.025)));
The currency comes in when it uses the ToString() overridden method. The format specified there is for currency.
I would like to make a program that calculates circumference. Though, would much rather have a rounded answer (two decimal places) as a pose to seven decimal places. But when I use the Math.Round method, it doesn't seem to round. What am I doing wrong?
Console.Write("Circumference Calculator! Enter the radius of the circle: ");
int inputRadius = Convert.ToInt32(Console.ReadLine());
double ans = Math.Sqrt(inputRadius) * Math.PI;
Math.Round(ans, 2);
Console.WriteLine("Your answer is: " + ans + " squared.");
Math.Round does not modify provided argument - it returns a new value. So you have to assigned it back to a variable to make it work:
ans = Math.Round(ans, 2);
You have to use the return value of Math.Round, it doesn't take the variable by reference.
//Greet user & give instructions
#region
Console.WriteLine("Circumference Calculator");
Console.WriteLine("");
Console.Write("Welcome to the Circumference Calculator! Please enter the radius of the circle: ");
#endregion
//Get input from user and calculate answer
#region
Console.ForegroundColor = oldColour;
int inputRadius = Convert.ToInt32(Console.ReadLine());
double ans = Math.Sqrt(inputRadius) * Math.PI;
Console.ForegroundColor = ConsoleColor.DarkYellow;
double roundedAnswer = Math.Round(ans, 2);
Console.WriteLine("Your answer is: " + roundedAnswer + " squared.");
#endregion
In my if statement (LengthCalculatorOption == 1), I want to convert, for example, 187.96cm to feet and inches, such as 6feet 2ins. How do I do that? In my current code, it shows 6.17feet and always 0ins. I have no idea why.
static void Main(string[] args) {
double Centimetres = 0.0, Feet = 0.0, Inches = 0.0;
string AnotherConversion = null;
string LengthCalculatorMenu;
int LengthCalculatorOption;
do {
LengthCalculatorMenu = ("Enter 1) Convert centimetres to feet and inches:"
+ "\nEnter 2) Convert feet and inches to centimetres:");
Console.Write(LengthCalculatorMenu);
LengthCalculatorOption = int.Parse(Console.ReadLine());
if (LengthCalculatorOption == 1) {
Console.WriteLine("Please Enter the Centimetres(cm) that you wish to convert to feet and inches");
Centimetres = double.Parse(Console.ReadLine());
Feet = (Centimetres / 2.54) / 12;
Inches = (Centimetres / 2.54) - (Feet * 12);
Centimetres = ((Feet * 12) + Inches) * 2.54;
Console.WriteLine("\nThe equivalent in feet and inches is {0:C} ft {1:G} ins", Feet, Inches);
Console.Write("\nWould you like to make an another conversion? \n\n(Enter Y to make an another conversion/Enter any other key to exit):");
AnotherConversion = Console.ReadLine();
} else if (LengthCalculatorOption == 2) {
Console.WriteLine("Please Enter the Feet");
Feet = double.Parse(Console.ReadLine());
Console.WriteLine("Please Enter the Inches");
Inches = double.Parse(Console.ReadLine());
Centimetres = ((Feet * 12) + Inches) * 2.54;
Console.WriteLine("\nThe equivalent in centimetres is {0:G}cm", Centimetres);
Console.Write("\nWould you like to make an another conversion? \n\n(Enter Y to make an another conversion/Enter any other key to exit):");
AnotherConversion = Console.ReadLine();
} else {
Console.WriteLine("\n\a\t Invalid Option!Option Must be 1 or 2");
}
} while (AnotherConversion == "y" || AnotherConversion == "Y");
Try this:
Feet = (Centimetres / 2.54) / 12;
int iFeet = (int)Feet;
inches = (Feet - (double)iFeet) * 12;
To elaborate a bit:
You are defining feet as a double, which means that it will be a decimal value. So since you're dividing by 12, it can become a decimal representation.
What my code does is it converts Feet to integer (which will round it to 6 in this situation). We then subtract the double version of Feet (6.17 in this situation) which equals .17 (The remainder). We multiply that by 12 to convert from .17 feet to inches
Edit
To expand based on Scott's comment, this would be a different way to go
int totalInches = (Centimetres / 2.54); // This will take a floor function of Centimetres/2.54
int Feet = (totalInches - totalInches % 12) / 12; // This will make it divisible by 12
int inches = totalInches % 12; // This will give you the remainder after you divide by 12
To calculate a value in centimeters into feet and inches, you'd likely want to do this:
double centimeters = 187.96;
double inches = centimeters/2.54;
double feet = Math.Floor(inches / 12);
inches -= (feet*12);
Generally-speaking, one should convert down to the most basic level, then calculate your way up. This way, you only do the work of converting one time, instead of having to repeat the conversion calculation. In this insntance, I do the simple conversion of centimeters to inches, and then after that, count the number of feet in inches, then subtract that much from the final inches value.
So, if I had, say, 38 inches, I would have Math.Floor(38 / 12) feet, or 3. Then inches would be set to 38 - (3*12), which is 2, giving a final result of 3 feet, 2 inches.
Keeping it as double, use:
double inp = 12.75; // e.g.
double feet = Math.Floor(inp);
double inches = (inp - feet) * 12.0;
Try this:
double F = Math.Floor(Centimetres * 0.0328084);
Feet = Centimetres * 0.0328084;
Inches = (Feet - F) * 12;
1 ft = 0.30480m
Some general-purpose methods:
public static class Converter
{
public static (int Feet, double Inches) CentimetresToFeetInches(double centimetres)
{
var feet = centimetres / 2.54 / 12;
var iFeet = (int)feet;
var inches = (feet - iFeet) * 12;
return (iFeet, inches);
}
public static string CentimetresToFeetInchesString(double centimetres, string footSymbol = " foot", string inchesSymbol = " inches")
{
(var feet, var inches) = CentimetresToFeetInches(centimetres);
return $"{feet:N0}{footSymbol}, {inches:N0}{inchesSymbol}";
}
}
Usage:
(var feet, var inches) = Converter.CentimetresToFeetInches(178);
//feet == 5
//inches == 10.078740157480315
var feetInchesString = Converter.CentimetresToFeetInchesString(178);
//5 foot, 10 inches
I have no idea why my BMI value is always = to 0. I am programming noob what am i missing? other than that is my if statement alright? what am i missing?
static void Main(string[] args) {
double WeightKg = 0.0, HeightCm = 0.0, Weightlbs = 0.0, WeightOz = 0.0, BMI = 0.0, Feet = 0.0, Inches = 0.0;
int BMIOption;
string AnotherConversion;
string BMIMenu = ("Which Measurement You Want to use to enter the weight and height?"
+ "\n1)Enter 1 for Metric"
+ "\n2)Enter 2 for British Imperial:");
Console.Write(BMIMenu);
BMIOption = int.Parse(Console.ReadLine());
if (BMIOption == 1) {
Console.Write("\nPlease Enter your Weight in Kilogram (kg):");
WeightKg = int.Parse(Console.ReadLine());
Console.Write("\nPlease Enter your Height in in centimetres (cm):");
HeightCm = int.Parse(Console.ReadLine());
BMI = WeightKg / (HeightCm * HeightCm);
if (BMI >= 35) {
Console.WriteLine("\nYour BMI is {0:C},Severe Obesity", BMI);
} else if (BMI >= 30) {
Console.WriteLine("\nYour BMI is {0:C},Obese", BMI);
} else if (BMI >= 25) {
Console.WriteLine("\nYour BMI is {0:C},OverWeight", BMI);
} else if (BMI >= 18.5) {
Console.WriteLine("\nYour BMI is {0:C},Healthy BodyWeight", BMI);
} else if (BMI <= 18.5) {
Console.WriteLine("\nYour BMI is {0:C},UnderWeight", BMI);
}//End if
Console.Write("\nWould you like to make an another conversion? \n\n(Enter Y to make an another conversion/Enter any other key to exit):");
Console.ReadKey();
BMI is calculated with meters, not centimeters. So you need to convert the HeightCm to HeightM. If you don't do this, you get really small number, that is then printed as 0.
double HeightM = HeightCm / 100.0;
BMI = WeightKg / (HeightM * HeightM);
Also, when parsing, use double.Parse instead of int.Parse. The way it is right now, you will only parse the number without the decimal part.
In this line:
BMI = WeightKg / (HeightCm * HeightCm);
The result ends up being very small (less than 1). Take an example where WeightKg is 55 and HeightCm is 165. The result is around 0.002.
When you display it as {0:C}, it's being displayed as zero. Use {0:G} to see the actual value.
even so your formula is wrong, as Martin says u must replace the int.Parse with double AND
BMI = WeightKg / ((HeightCm/100) * (HeightCm/100));
use this for calculation it is HEIGHT IN METERS :)
I am trying to enter a loop to stop the number of areas being below 0 or above 5, I've tried entering a do while loop, but it still executes the next block of code even when incorrect input has been entered. I've commented the section with the loop. Help would be much appreciated, thank you.
const int MaxItems = 5; // max size of array needed
double[] itemCosts = new double[MaxItems];
int jobType; // valid values are 1=normal, 2=waterproofing, 3=skim
int nAreas;
int i;
string customer;
double totalCost = 0.0;
double averageCost;
const double discountPrice = 800; // price at which discount available
const double discountRate = 0.1; // discount rate
const double setupCostPerArea = 30.00;
// cost of moving furniture, carpets etc.
double discount, discountedTotal; // discount amount and discounted total
double width, height; // width and height of plaster area
double[] basePrices = { 0, 35.0, 30.0, 20.0 }; // added 0 as index placeholder, so 35 can be selected using 1, and so forth.
// prices per square metre for standard, plasterboard and skim, and skim only
Console.Write("enter name of customer: ");
customer = Console.ReadLine();
Console.Write("enter number of plaster areas to quote for: ");
nAreas = Convert.ToInt32(Console.ReadLine());
do
{
Console.WriteLine("Please Enter numbers of rooms between 1 and 5 only!!!!");
} while (nAreas < 1 && nAreas > 5); // loop
for (i = 0; i < nAreas; i++)
{
Console.WriteLine("Data entry for area {0}", i + 1);
Console.Write("enter 1 (standard), 2 (plasterboard and skim) or 3 (skim only): ");
jobType = Convert.ToInt32(Console.ReadLine());
Console.Write("enter width of area {0} in metres: ", i + 1);
width = Convert.ToDouble(Console.ReadLine());
Console.Write("enter height of area {0} in metres: ", i + 1);
height = Convert.ToDouble(Console.ReadLine());
// add base area cost to price based on area and type of plaster
itemCosts[i] = setupCostPerArea + (basePrices[jobType] * (width * height));
totalCost += itemCosts[i];
}
averageCost = totalCost / nAreas;
// main report heading
Console.WriteLine("\n");
Console.WriteLine("Plasterers R US Job Costing Report");
Console.WriteLine("===================================");
Console.WriteLine("\nName of customer: {0}\n", customer);
Console.WriteLine("No. of plaster areas: {0}", nAreas);
Console.WriteLine("Average Cost per area £{0:0.00}", averageCost);
// output subheadings
Console.WriteLine("Area\tCost\tDifference From Average");
for (i = 0; i < nAreas; i++)
{
Console.Write("{0}\t", i + 1);
Console.Write("£{0:0.00}\t", itemCosts[i]);
Console.WriteLine("£{0:0.00}", itemCosts[i] - averageCost);
}
Console.WriteLine("Total Cost £{0:0.00}", totalCost);
if (totalCost > discountPrice)
{
discount = totalCost * discountRate;
discountedTotal = totalCost - discount;
Console.WriteLine(" - Discount of £{0:0.00}", discount);
Console.WriteLine(" Discounted Total £{0:0.00}", discountedTotal);
}
Console.WriteLine("press enter to continue");
Console.ReadLine();
do
{
Console.Write("enter number of plaster areas to quote for: ");
nAreas = Convert.ToInt32(Console.ReadLine());
if(nAreas < 1 || nAreas > 5)
Console.WriteLine("Please Enter numbers of rooms between 1 and 5 only!!!!");
} while (nAreas < 1 || nAreas > 5); // loop
Two problems:
you are not asking for new input within your loop
you have the wrong exit condition for your loop - an illegal are number would be less than 1 or larger than 5.
Adjusting for both:
do
{
Console.Write("enter number of plaster areas to quote for: ");
nAreas = Convert.ToInt32(Console.ReadLine());
if(nAreas < 1 || nAreas > 5)
Console.WriteLine("Please Enter numbers of rooms between 1 and 5 only!!!!");
} while (nAreas < 1 || nAreas > 5); // loop