Reputation: 41
Here's my block of code, and I can't figure out how to make girth and volume perform the calculation. For example, when I input 1,1,1,1 for the first 4 parameters, I get 0, 0 for volume and girth.
if (packages == 1)
{
int width = 0, length = 0, height = 0, weight = 0;
int volume = 0, girth = 0;
int[] packageInfo = new int[6] { width, length, height, weight ,volume, girth };
packageInfo[4] = height * width * length;
packageInfo[5] = (2 * length + 2 * width);
double packageSum = 0;
for (int k = 0; k < 4; k++)
{
string line = Console.ReadLine();
if (!int.TryParse(line, out packageInfo[k]))
{
Console.WriteLine("Couldn't parse {0} - please enter integers", line);
k--;
}
}
if(packageInfo[3] > 25)
{
packageSum = 0;
Console.WriteLine("Package couldn't be shipped because of its size.");
}
if (volume > 4800)
{
packageSum = packageSum + 5.95;
}
if (volume > 9600)
{
packageSum = 0;
Console.WriteLine("Package couldn't be shipped because of its size.");
}
foreach (var item in packageInfo)
Console.WriteLine(item.ToString());
}
Upvotes: 0
Views: 125
Reputation: 30837
You should calculate them after getting user input. Rearrange you code this way:
for (int k = 0; k < 4; k++)
{
string line = Console.ReadLine();
if (!int.TryParse(line, out packageInfo[k]))
{
Console.WriteLine("Couldn't parse {0} - please enter integers", line);
k--;
}
}
packageInfo[4] = packageInfo[0] * packageInfo[1] * packageInfo[2];
packageInfo[5] = (2 * packageInfo[0] + 2 * packageInfo[1]);
Upvotes: 1
Reputation: 1148
Variables in C# aren't like variables in math. From what I can tell, you expect packageInfo[4]
to be updated whenever packageInfo[0], packageInfo[1], packageInfo[2]
are modified. Because, in math, if you define volume to be height*width*length and you modify any of those variables, then the volume changes. Unfortunately, a "standard variable" in C# is just a blob of bits. You read/write from it. I'm not being very precise (I am not that proficient with languages), but there is no notion of variables being defined in terms of others' (they can only be initialized with some value, which might be the value of some other variable). To create such relationships requires language constructs to represent them.
So what your code currently does is:
Console
, packageInfo[0], packageInfo[1], packageInfo[2], packageInfo[3]
are modified.packageInfo[0]
it does not modify any other int
in packageInfo[]
You can emulate the behavior to be more similar to math with something like:
private int _height;
private int _width;
private int _length;
private int Volume =
{
get { return _height * _width * _length };
}
Upvotes: 1
Reputation: 58990
Look closely at your code.
First, you do this:
int width = 0, length = 0, height = 0, weight = 0;
int volume = 0, girth = 0;
int[] packageInfo = new int[6] { width, length, height, weight ,volume, girth };
packageInfo[4] = height * width * length;
packageInfo[5] = (2 * length + 2 * width);
double packageSum = 0;
0 * 0 * 0 = 0.
2 * 0 + 2 * 0 = 0
Then, after you've calculated the volume and girth, you actually query the user for input:
for (int k = 0; k < 4; k++)
{
string line = Console.ReadLine();
if (!int.TryParse(line, out packageInfo[k]))
{
Console.WriteLine("Couldn't parse {0} - please enter integers", line);
k--;
}
}
Upvotes: 0