Julian
Julian

Reputation: 21

My calculator application is not working as intended

I'm a beginner with C# and as my first program I've made a simple calculator with 2 variables and now I've created a calculator v2 in which you can put how many variables you want but it's not working.

It has 2 big problems that makes the calculator not work as intended:

  1. The operations number is the same as the numbers and it should have been less with 1
  2. The " sum " variable resets to 0 or the last number entered every time you exit the switch and I've tried some things to change but it still resets the value.

The program is structured in 2 classes. The calculator class with the calculation functions and the program class with the main function.

Calculator class code:

class Calculator
{
    private double num;
    private double sum = 0;
    
    public Calculator(double number)
    {
        num = number;
    }
 
    public double Add()
    {
        return sum += num;
    }
    public double Divide()
    {
        if(num==0)
        {
            num = 1;
        }
        return sum /= num;
    }

    public double Subtraction()
    {
        return sum -= num;
    }
    public double Multiplication()
    {
        if(sum==0)
        {
            sum = 1;
        }
        return sum *= num;
    }

    public double Sum
    {
        get
        {
            return sum;
        }
    }
}

The program class code:

class Program
{
    static void Main(string[] args)
    {
        Console.ForegroundColor = ConsoleColor.Cyan;
        Console.WriteLine("Choose the number of the operations: 1.Add 2.Divide 3.Subtraction 4.Multiply");
        Console.ResetColor();

        Console.Write("How many numbers do you want to calculate?: ");
        int countNumbers = int.Parse(Console.ReadLine());
        double suma = 0;

        if (countNumbers > 1)
        {
             for (int i = 1; i <= countNumbers; i++)
             {
                Console.Write("Add number: ");
                double number = double.Parse(Console.ReadLine());
                Calculator op = new Calculator(number);

                Console.Write("Operation number: ");
                int operation = int.Parse(Console.ReadLine());

                switch (operation)
                {
                    case 1:
                       op.Add();
                       break;
                    case 2:
                       op.Divide();
                       break;
                    case 3:
                       op.Subtraction();
                       break;
                    case 4:
                       op.Multiplication();
                       break;
                }
                     
                suma = op.Sum;
             }
        
        }
        Console.WriteLine("The sum is {0}", suma);
    }
}

Regards

Upvotes: 0

Views: 157

Answers (1)

Rafalon
Rafalon

Reputation: 4515

I would first initialize a single Calculator instance with the first number, then loop to get each number/operation:

Console.Write("First number: ");
double number = double.Parse(Console.ReadLine());
Calculator op = new Calculator(number);

for (int i = 1; i < countNumbers; i++)
{
    Console.Write("First number: ");
    double number = double.Parse(Console.ReadLine());

    Console.Write("Operation number: ");
    int operation = int.Parse(Console.ReadLine());

    switch (operation)
    {
        case 1:
            op.Add(number);
            break;
        case 2:
            op.Divide(number);
            break;
        case 3:
            op.Subtraction(number);
            break;
        case 4:
            op.Multiplication(number);
            break;
    }
    
    suma = op.Sum;
}

Note that to do this, I would get rid of the num field in Calculator, and use a parameter in each function, like this:

public double Add(double num)
{
    return sum += num;
}

Also note that you apparently do not use the return value, so you could as well write:

public void Add(double num)
{
    sum += num;
}

Upvotes: 2

Related Questions