|
Hey all,
So,
for my final in C# we have to write a few programs. Some of them being
pretty simple. The one I'm doing now asks me to write a weight
conversion program with a minimum of grams, ounces, kilograms and
pounds. If the user enters a negative number or a letter then it's
supposed to throw and handle an exception. Well, everything was going
great until I encountered this problem.
At the moment, I'm
trying to convert grams to kilograms, pounds and ounces. It works great
when I'm converting grams to ounces or kilograms, but whenever I want
to convert the grams to pounds, it handles an exception and says
"Invalid input value.". I have the user select what they want to
convert said grams to by inputting a number. The number for pounds is
3, but whenever the user enters 3 to convert to pounds it handles said
exception.
Here's my code:
WeightConverterTest Class
using System; using System.Collections.Generic; using System.Text;
namespace Final_Question_3 { class WeightConverterTest { static void Main(string[] args) { //declare variables int selection = 0;
while (selection < 5) { try { //prompt user to select a measurement type to be converted Console.WriteLine("Select measurement type:" + "\n1) Grams\t2) Kilograms\t3) Ounces\t4) Pounds\n" + "5) Exit the program.\n"); selection = Convert.ToInt32(Console.ReadLine());
/*if statement which checks if the user wanted to end the program of if they entered a selection between 1 and 5*/ if (selection == 5) Console.WriteLine("Exiting program."); else if (selection < 1 || selection > 5) Console.WriteLine("Please enter a selection between 1 and 5.\n"); else SelectionMethod(selection); }//end try //if input value is not numeric, display message catch { Console.WriteLine("Invalid input value."); }//end catch }//end while
}//end Main method
public static int SelectionMethod(int s) { //declare variables and create reference to WeightConverter class WeightConverter weightConverter = new WeightConverter(); decimal grams = 0; decimal kilograms = 0; decimal ounces = 0; decimal pounds = 0; try { switch (s) { //if user chose to input grams, prompt user to enter how many case 1: Console.WriteLine("Input amount of grams to be converted:\n"); grams = Convert.ToDecimal(Console.ReadLine());
//call Grams method of class WeightConverter weightConverter.Grams(grams);
break;
//if user chose to input kilograms, prompt user to enter how many case 2: Console.WriteLine("Input amount of kilograms to be conveted:\n"); kilograms = Convert.ToDecimal(Console.ReadLine());
//call Kilograms method of class WeightConverter weightConverter.Kilograms(kilograms); break;
//if user chose to input ounces, prompt user to enter how many case 3: Console.WriteLine("Input amount of ounces to be converted:\n"); ounces = Convert.ToDecimal(Console.ReadLine());
//call Ounces method of class WeightConverter weightConverter.Ounces(ounces); break;
//if user chose to input pounds, prompt user to enter how many case 4: Console.WriteLine("Input amount of pounds to be converted:\n"); pounds = Convert.ToDecimal(Console.ReadLine());
//call Pounds method of class WeightConverter weightConverter.Pounds(pounds); break; } }//end try //if input value is not numeric, display message catch { Console.WriteLine("Invalid input value."); }//end catch
return s; }//end Selection method } }
WeightConverter Class
using System; using System.Collections.Generic; using System.Text;
namespace Final_Question_3 { class WeightConverter { public decimal Grams(decimal g) { //declare variables decimal kilograms = 0; decimal ounces = 0; decimal pounds = 0; int selection = 0;
while (selection <= 4) { try { //prompt user to select what they would like to convert grams to Console.WriteLine("Convert grams to?\n1) Kilograms\t2) Ounces \t3) Pounds" + "\n4) Return to first selection."); selection = Convert.ToInt32(Console.ReadLine());
switch (selection) { //convert grams to kilograms case 1: kilograms = g / 1000;
Console.WriteLine("{0} gram(s) is {1} kilogram(s).", g, kilograms); break;
//convert grams to ounces case 2: ounces = g / (int)28.31257;
Console.WriteLine("{0} gram(s) is {1} ounce(s).", g, ounces); break;
//convert grams to pounds case 3: pounds = g / 453;
Console.WriteLine("{0} gram(s} is {1} pound(s).", g, pounds); break;
//return to first selection case 4: selection = 5; break;
} }//end try //if value entered is not numeric, display message catch { Console.WriteLine("Invalid input value."); }//end catch }//end while return g; }
public decimal Kilograms(decimal kg) { decimal grams = 0; decimal ounces = 0; decimal pounds = 0;
return kg; }
public decimal Ounces(decimal o) { decimal grams = 0; decimal kilograms = 0; decimal pounds = 0;
return o; }
public decimal Pounds(decimal p) { decimal grams = 0; decimal kilograms = 0; decimal ounces = 0;
return p; } } }
| |