Gungsun
Gungsun

Reputation: 13

Subtraction between multiple random input numbers

i want to make user input random number example : 5-3-10-50 , system will split " - " and then the result 5 3 10 50 , how to make subtraction from first number minus second number and so on, like this 5 - 3 = 2 , 2 - 10 = -8 , -8 - 50 = -58 and then system will print the final answer -58

my code :

bool Subtraction = true;
int AskSubtraction = 0;

while (Subtraction)
{
    Console.Write("\n" + "input number ( example : 1 - 2 - 3 - 4 ) : ");
    var InputNumber = Console.ReadLine();
    double Answer = 0;

        foreach (var result in InputNumber.Split('-'))
        {
            if (double.TryParse(result, out _))
            {
                double NumberResult = Convert.ToDouble(result);
                Answer -= NumberResult;
            }
            else
            {
                Console.WriteLine("\n" + "Wrong input !");
                AskSubtraction++;
            }
        }

    Console.WriteLine("\n" + "subtraction result : " + Answer);
}

i know my code is wrong, im beginner i already try to fix this but i cant fix it until now, i hope someone tell me what's wrong with my code and fix it too, thank you.

Upvotes: 1

Views: 442

Answers (2)

Roxy reid
Roxy reid

Reputation: 18

You would need a condition inside the foreach loop to check for the first parsed double before you begin subtraction. Also there is no need to call Convert.ToDouble() since the double.TryParse() function already returns the parsed double value, All you would need is a variable to contain the out value of the double.TryParse() function, See example below

bool Subtraction = true;
int AskSubtraction = 0;

while (Subtraction)
{
    Console.Write("\n" + "input number ( example : 1 - 2 - 3 - 4 ) : ");
    var InputNumber = Console.ReadLine();
    double Answer = 0;
    double numResult;

    foreach (var result in InputNumber.Split('-'))
    {
        if (double.TryParse(result, out numResult))
        {
            if(Math.Abs(Answer)>0){
                Answer -= numResult;
            }
            else{
                Answer=numResult;
            }
        }
        else
        {
            Console.WriteLine("\n" + "Wrong input !");
            AskSubtraction++;
        }
    }

    Console.WriteLine("\n" + "subtraction result : " + Answer);
}

Upvotes: 0

JC_
JC_

Reputation: 26

The reason yours doesn't work is because you set Answer = 0. And you used foreach. On the first iteration of the loop, the first number is subtracted from Answer which results in -5.

Use for (int i=1; i<arr.Length; i++) instead of foreach

Start from index 1, and then subtract the values.

Example:

var arr = InputNumber.Split('-');
double Answer = 0;
if (double.TryParse(arr[0], out _))
{
    // We set Answer to the first number, since nothing is subtracted yet
    Answer = Convert.ToDouble(arr[0]);
}

// We start index from 1, since subtraction starts from 2nd number on the String array
for (int i=1; i<arr.Length; i++)
{
    if (double.TryParse(arr[i], out _))
    {
        double NumberResult = Convert.ToDouble(arr[i]);
        Answer -= NumberResult;
    }
}

Tested on Online C# Compiler

Upvotes: 1

Related Questions