Home > Software design >  Why isn't my foreach loop printing the correct output?
Why isn't my foreach loop printing the correct output?

Time:07-28

I made a foreach loop that would take a user's input and subtract two numbers on the same line, I was hoping it would print out the difference between any two numbers, but it didn't. Instead, when I ran the code and entered the numbers, it printed a different answer which didn't make sense to me. I'm kind of a bit new to c# so I'm just experimenting with things, but if anyone knows what's wrong with my code please help me figure out what's wrong with it. Thanks!

using System;
                
public class Program 
{
    string sequence;
    double answer;
    
    public void Main()
    {
        Console.WriteLine("<<SUBTRACTION CODE>>");
        Console.WriteLine("");
        Console.WriteLine("Enter a sequence of numbers and an operator to get subtracted answer ");
        Console.WriteLine("Eg: 10-5");
        sequence = Console.ReadLine();
    
        foreach (var operation in sequence.Split('-'))
            {
               answer -= double.Parse(operation); 
            }
            Console.WriteLine(answer);
            Console.ReadLine();
    } 
}

CodePudding user response:

your answer will initially be 0. So when you do answer -= you subtract the right-hand-site (e.g. 5) from zero, which will result in a negative outcome. I suppose you need to assign the first operat to your answer:

using System;
                
public class Program 
{
    string sequence;
    double answer;
    
    public void Main()
    {
        Console.WriteLine("<<SUBTRACTION CODE>>");
        Console.WriteLine("");
        Console.WriteLine("Enter a sequence of numbers and an operator to get subtracted answer ");
        Console.WriteLine("Eg: 10-5");
        sequence = Console.ReadLine();
        var parts = sequence.Split('-');

        answer = parts[0];
        for(int i = 1; i < parts.Length; i  )
        {
           answer -= double.Parse(parts[i]); 
        }
        Console.WriteLine(answer);
        Console.ReadLine();
    } 
}

Of course you should check if there are at least two elements within parts.

CodePudding user response:

Assuming you want 5 rather than -15?

You need to initialise the value of answer with the first number rather than subtract everything from a starting point of zero.

bool init;
string sequence;
double answer;

public void Main()
{
    Console.WriteLine("<<SUBTRACTION CODE>>");
    Console.WriteLine("");
    Console.WriteLine("Enter a sequence of numbers and an operator to get subtracted answer ");
    Console.WriteLine("Eg: 10-5");
    sequence = Console.ReadLine();

    foreach (var operation in sequence.Split('-'))
        {
           if (!init) {
               answer = double.Parse(operation); 
               init = true;
           }
           else
               answer -= double.Parse(operation); 
        }
        Console.WriteLine(answer);
        Console.ReadLine();
} 
  • Related