Reputation: 25
I'm currently learning C# and I wanted to do simple math problems using C# to sharpen my skills. Can someone tell me what am I missing in this code? I want the user to enter a specific length, width, and height and then the code will multiply it and then divide by 1728 and then display however it does not function properly. Can someone please assist me, thank you
Console.Write("Enter your Length: ");
int length = new int();
Console.ReadLine();
Console.Write("Enter your Width: ");
int width = new int();
Console.ReadLine();
Console.Write("Enter your Height: ");
int height = new int();
Console.ReadLine();
int totalDims = new int();
totalDims = length * width * height;
int cubicFeet = new int();
cubicFeet = totalDims/1728;
Console.WriteLine("Your total cubic feet is " + cubicFeet);
Console.ReadLine();
Upvotes: 1
Views: 1606
Reputation: 540
1.) You have to set your numbers. Console.ReadLine() returns the string value of the input you put in the console so you have to convert that to an int.
2.) The operation at the end will likely return a decimal so have totalDims and cubicFeet be decimal.
Console.Write("Enter your Length: ");
int length = new int();
length = int.Parse(Console.ReadLine());
Console.Write("Enter your Width: ");
int width = new int();
width = int.Parse(Console.ReadLine());
Console.Write("Enter your Height: ");
int height = new int();
height = int.Parse(Console.ReadLine());
int totalDims = new int();
totalDims = length * width * height;
//isnt cubic feet just length * width * height?
//decimal cubicFeet = new decimal();
//cubicFeet = totalDims;
Console.WriteLine("Your total cubic feet is " + totalDims);
Console.ReadLine();
Upvotes: 0
Reputation: 76557
You are never currently setting the values that you are reading from the command line in your Console.ReadLine()
calls.
Console.ReadLine()
will actually return a string representing the value that was entered, so you'll need to convert that to the proper numeric type so you can use it.
Since you are dealing with integers, you can use the Convert.ToInt32()
or Int32.Parse()
methods as seen below :
Console.Write("Enter your Length: ");
// Read and parse your length
int length = Convert.ToInt32(Console.ReadLine());
Console.Write("Enter your Width: ");
// Read and parse your width
int width = Convert.ToInt32(Console.ReadLine());
Console.Write("Enter your Height: ");
// Read and parse your height
int height = Convert.ToInt32(Console.ReadLine());
// Now you have all of your dimensions so calculate
int totalDims = length * width * height;
// Since you are performing division, you could have a fractional
// value here, so you might want to use another type like decimal
decimal cubicFeet = totalDims / 1728m;
// Output your result
Console.WriteLine("Your total cubic feet is " + cubicFeet);
If you want to imlpement an even safer solution, you should consider the Int32.TryParse()
method which will let you know if the conversion is good or not and will allow you to prompt again :
int length, width, height, totalDims;
Console.Write("Enter your Length: ");
// Read and parse your length
while(!Int32.TryParse(Console.ReadLine(), out length)){
Console.Write("Enter your Length again: ");
}
Console.Write("Enter your Width: ");
// Read and parse your width
while(!Int32.TryParse(Console.ReadLine(), out width)){
Console.Write("Enter your Width again: ");
}
Console.Write("Enter your Height: ");
// Read and parse your height
while(!Int32.TryParse(Console.ReadLine(), out height)){
Console.Write("Enter your Height again: ");
}
// Now you have all of your dimensions so calculate
totalDims = length * width * height;
// Since you are performing division, you could have a fractional
// value here, so you might want to use another type like decimal
decimal cubicFeet = totalDims / 1728m;
// Output your result
Console.WriteLine("Your total cubic feet is " + cubicFeet);
You can see a complete working example of this in action here.
Upvotes: 1
Reputation: 15982
You have to parse to int
the lines you read from the console:
int length = 0;
int.TryParse(Console.ReadLine(), out length);
...
int width = 0;
int.TryParse(Console.ReadLine(), out width);
...
int height = 0;
int.TryParse(Console.ReadLine(), out height);
Also, if you want cubicFeet
as a floating number:
double cubicFeet = totalDims / 1728.0;
Upvotes: 1