hello guys,

write a program which calculates the volume of a cube or of a sfere. the user gives the side of the cube or the radius of the sfere

• the user gives the shape as string
• the shape will be sfere or cube

otherwise will print "wrong shape"

here is my code, i write something wrong... it doesn't show what i want

using System;


class Shape
{


    string shape;
    string sfere= null, cube= null;



    public string GetShape()
    {

        do{        
            Console.Write("Give the shape: ");
            shape= Console.ReadLine();


            if (shape== sfere|| shape== cube)
            {
                Console.WriteLine("Wrong shape");
            }



        } while (shape== sfere|| shape== cube);
        return shape;
    }


    public void calculation (out double Volume)
    {
        double radius, side;
       const double p = 3.14;

        GetShape();

        if (shape== sfere)
        {
            Console.Write("Give the radius of the sfere");
            radius= Double.Parse(Console.ReadLine());
            Volume= (4 / 3) * p * (Math.Pow(radius, 3));
        }


        else
        {
            Console.Write("Give the side of the cube");
            side= Double.Parse(Console.ReadLine());
            Volume= (Math.Pow(side, 3));
        }
       
        
    }

}

class Program
{
    static void Main()
    {
        Shape sh1 = new Shape();
       
blah blah

        Console.ReadKey();
    }
}

Recommended Answers

All 6 Replies

Line 9 is wrong.
Because you have to test that a string is equal to "sfere" or "cube" make them constants. Like this:

const string aSfere = "sfere";

i changed it but i have problem with the loop

how i will take only sfere and cube and not other strings?

This is called input validation.
Your program has to take care that it only accepts correct input.
Example: your program might only accept "sfere" and "cube" as correct input and reject all other input like "Danny" or "CUBE".

how can i succeed it?

Changed the code inside your GetShape method a bit:

string shape = string.Empty;
            do 
            {
                Console.Write("Give the shape:(type sfere or cube)"); 
                shape = Console.ReadLine();
                if (shape != aSfere && shape != aCube) 
                { 
                    Console.WriteLine("Wrong shape, try again"); 
                }
            } while (shape != aSfere && shape != aCube); 
            return shape;

ok, thank you for your time!!!!

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.