What am i missing in my code. Because it is not working.
According to visual studio, the istartd and iendd are unassigned local variable.

Console.WriteLine("{0, 40}", "Sphere Surface Area Evaluator");

            Console.Write("\nEvaluates Surface areas of spheres of various diameter");

            do
            {
                try
                {
                    bError = false;
                    Console.Write("\nEnter the starting diameter for the spheres: ");
                    istartd = int.Parse(Console.ReadLine());

                    if (istartd < 0)
                    {
                        Console.WriteLine("The start value must be positive.");
                        bError = true;
                    }
                }
                catch (FormatException)
                {
                    Console.WriteLine("An incorrect diameter was entered.");
                    bError = true;
                }
                finally
                {
                    Console.WriteLine("");
                    bError = true;
                }
                do
                {
                    try
                    {
                        bError = false;
                        Console.Write("\nEnter the end diameter for the spheres: ");
                        iendd = int.Parse(Console.ReadLine());

                        if (iendd < istartd)
                        {
                            Console.WriteLine("The end value must be positive and greater than the start value.");
                            bError = true;
                        }
                    }
                    catch (FormatException)
                    {
                        Console.WriteLine("An incorrect diameter was entered.");
                        bError = true;
                    }
                    finally
                    {
                        Console.WriteLine("");
                        bError = true;
                    }
                }
                while (bError);
            }
            while (bError);
            for (int iX = istartd; iX <= iendd; ++iX)
            {
                dA = 4 * 3.14159 * Math.Sqrt(istartd / 2);
                Console.WriteLine("A = {0} , d = {1}", dA, iX);
            }
            Console.WriteLine("Press any key to continue");
            COnsole.Read();

The program should look like this.
Sphere Surface Area Evaluator

Evaluates Surface areas of spheres of various diameter

Enter the starting diameter for the spheres: cats
An incorrect diameter was entered.
Enter the starting diameter for the spheres: -2
The start value must be positive.
Enter the starting diameter for the spheres: 2

Enter the end diameter for the spheres: -2
The end value must be positive and greater than the start value.
Enter the starting diameter for the spheres: 6

A = 12.6 , d = 2
A = 28.3 , d = 3
A = 50.3 , d = 4
A = 78.5 , d = 5
A = 113.1 , d = 6

Press any key to continue

Recommended Answers

All 12 Replies

Wherever you declare them, since it isn't in this code, just add = 0; to the end.

done that...

my new code is this: this code work: except its keeps looping the end value (line 41).

Console.Title = "Spher Surface Area Calculator";

            int istartd = 0;
            int iendd = 0;
            double dA;
            bool bError;

            Console.WriteLine("{0, 40}", "Sphere Surface Area Evaluator");

            Console.Write("\nEvaluates Surface areas of spheres of various diameter");

            do
            {
                try
                {
                    bError = false;
                    Console.Write("\nEnter the starting diameter for the spheres: ");
                    istartd = int.Parse(Console.ReadLine());

                    if (istartd < 0)
                    {
                        Console.WriteLine("The start value must be positive.");
                        bError = true;
                    }
                }
                catch (FormatException)
                {
                    Console.WriteLine("An incorrect diameter was entered.");
                    bError = true;
                }
                finally
                {
                    Console.WriteLine("");
                    bError = true;
                }
                do
                {
                    try
                    {
                        bError = false;
                        Console.Write("\nEnter the end diameter for the spheres: ");
                        iendd = int.Parse(Console.ReadLine());

                        if (iendd < istartd)
                        {
                            Console.WriteLine("The end value must be positive and greater than the start value.");
                            bError = true;
                        }
                    }
                    catch (FormatException)
                    {
                        Console.WriteLine("An incorrect diameter was entered.");
                        bError = true;
                    }
                    finally
                    {
                        Console.WriteLine("");
                        for (int iX = istartd; iX <= iendd; ++iX)
                        {
                            dA = 4 * 3.14159 * Math.Sqrt(istartd / 2);
                            Console.WriteLine("A = {0} , d = {1}", dA, iX);
                        }
                        bError = true;
                    }
                }
                while (bError);
           }
           while (bError);
           
           Console.WriteLine("Press any key to continue");
           Console.Read();

In line 63 you set bError to true. Since finally blocks always execute, it will always be true when it hits the while statement.

Console.Title = "Spher Surface Area Calculator";

            int istartd = 0;
            int iendd = 0;
            double dA;
            bool bError;

            Console.WriteLine("{0, 40}", "Sphere Surface Area Evaluator");

            Console.Write("\nEvaluates Surface areas of spheres of various diameter");

            do
            {
                try
                {
                    bError = false;
                    Console.Write("\nEnter the starting diameter for the spheres: ");
                    istartd = int.Parse(Console.ReadLine());

                    if (istartd <= 0)
                    {
                        Console.WriteLine("The start value must be positive.");
                        continue;
                    }
                }
                catch (FormatException)
                {
                    Console.WriteLine("An incorrect diameter was entered.");
                    continue;
                }
                finally
                {
                    Console.WriteLine("");
                    bError = true;
                }
                do
                {
                    try
                    {
                        bError = false;
                        Console.Write("\nEnter the end diameter for the spheres: ");
                        iendd = int.Parse(Console.ReadLine());

                        if (iendd < istartd)
                        {
                            Console.WriteLine("The end value must be positive and greater than the start value.");
                            continue;
                        }
                    }
                    catch (FormatException)
                    {
                        Console.WriteLine("An incorrect diameter was entered.");
                        continue;
                    }
                    finally
                    {
                        Console.WriteLine("");
                        bError = true;
                    }
                    for (int iX = istartd; iX <= iendd; ++iX)
                    {
                        dA = 4 * 3.14159 * Math.Sqrt(istartd / 2);
                        Console.WriteLine("A = {0:f1} , d = {1}", dA, iX);
                    }
                    bError = false;
                }
                while (bError);

            }
            while (bError);

            Console.WriteLine("Press any key to continue");
            Console.Read();

there something wrong from line 60 - 64.
because
My program output is this:
A = 12.6 , d = 2
A = 12.6 , d = 3
A = 12.6 , d = 4
A = 12.6 , d = 5
A = 12.6 , d = 6

its supposed to be this.
A = 12.6 , d = 2
A = 28.3 , d = 3
A = 50.3 , d = 4
A = 78.5 , d = 5
A = 113.1 , d = 6

Line 62 'istartd' should be 'iX'

i changed it but still doesn't work.

for d = 2 & 3 is A = 12.6
for d = 4 & 5 is A = 17.8
and for d = 6 is A = 21.8

Inside the Math.Sqrt your are doing iX / 2. Both iX and 2 are integers, so you are doing integer division. This means you get:
d = 2, 2/2 = 1
d = 3, 3/2 = 1
d = 4, 4/2 = 2
d = 5, 5/2 = 2
d = 6, 6/2 = 3

Change the '2' to '2.0' to force a double result. I should have noticed that earlier :)

Oh, and your equation for the surface area of a sphere is wrong.

the instruction says the equation i need to use is A=4πr^2 (3.14159 for PI)

Yep. ^2 means Squared, not Square root.

i think we need remainder because 3/2 = 1.5 or 5/2 = 2.5...
If its true that we a remainder, i don't know how to put a remainder in my code

As I said before, just changing the 2 in the division to 2.0 will force it to be a double, which will give you the fractional part.

thank you...i got it now...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.