-6

I have a list containing user entered values. I would like all integers entered to sum together at the end and the total to be displayed. Is this possible?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BubbleSort1
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.ForegroundColor = ConsoleColor.Green;
            List<int> price= new List<int>();
        enterprice:
            Console.WriteLine("Please enter price");
            Console.WriteLine("When Finished please press '/'");
            string pricex = Console.ReadLine();

            if (pricex == "/")
            {
                goto receipt;
            }
            else
            {               
                int pricey = Convert.ToInt16(pricex);
                price.Add(pricey);
                Console.Clear();
                goto enterprice;
            }

        receipt:
            Console.Clear();
            Console.WriteLine("Your Outflows:");
            Console.WriteLine(string.Join("\n", price));
            Console.ReadKey();
        }
    }
}

Before anyone mentions I understand there are better methods than goto and I could probably save user entered data straight as integers and I will learn how but have just started so want to take my time with it. Many thanks ;)

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • 2
    Loop through the collection and add them up? – James Cooke Feb 24 '20 at 18:05
  • 2
    @Clutchjam007 Try LINQ Sum. – yW0K5o Feb 24 '20 at 18:07
  • 2
    I know you addressed it already, but there isnt a reason to use `goto` ever. Get rid of it now, don't wait – maccettura Feb 24 '20 at 18:08
  • 1
    I'm assuming `goto` is used here taken verbatim from the textbook or wherever OP pulled this algorithm from. Its typical to see `goto` in algorithm pseudo. But in practice `goto` is the bad kind of four letter word, you should never use it. Instead, make a function and call that instead. – TheBatman Feb 24 '20 at 18:12

2 Answers2

3

There is a sum function on the List object.

var total = price.Sum();

It is an extension method on System.Linq. So, you will also need.

using System.Linq;
Jason Dimmick
  • 400
  • 4
  • 12
  • Hi thanks for the help. Just wondering what var saves something as. I grasp it is kind of similar to an integer? – Clutchjam007 Feb 24 '20 at 18:15
  • @Clutchjam007 What `var` ends up as is determined by the expression. In this case, it resolves to an integer. If the right hand side returned a string it would be a string. I personally only use var for anonymous types and prefer to be explicit about my typing. If you're just learning I would recommend to do the same. Using `var` all throughout your code tends to cause some confusion in my experience. – TheBatman Feb 24 '20 at 18:21
  • Like @TheBatman said its just a variable declaration for current scope. I could have used `code int total = price.Sum();` var keeps it a little more dynamic. – Jason Dimmick Feb 24 '20 at 18:31
0

The other answer recommends LINQ, and that is definitely the less verbose way to do it. But I have a feeling that you're just learning, so may want to have an idea of how this actually works. Below is the an abridged version of the Linq.Sum source code so you can see what is happening.

public static int Sum(this IEnumerable<int> source)
{
    int sum = 0;
    foreach (int v in source)
    {
        sum += v;
    }
    return sum;
}

It loops through each item in the IEnumerable, adds them to another variable, and then returns that result.

TheBatman
  • 770
  • 4
  • 10