Results 1 to 3 of 3

Thread: Problem with writing out contens of a generic list

  1. #1
    .NET Framework
    .NET 4.5
    Join Date
    Jun 2018
    Posts
    21
    Rep Power
    9

    Problem with writing out contens of a generic list

    I have written a small console application - I want to read in some strings which I parse as doubles. After the user pushed "r", the first double I add to a generic List<Double> is printed out.


    My question is the following one - even though I make a typecast, for example if I enter 2.2 as the first number - the result which will finally be printed out is 22 in spite of the fact that I make a typecast. What am I doing wrong?


    Code:
    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    
    namespace Average
    {
    
    
    
    
    class Program
        {
    
    
            private static List<Double> al = new List<Double>();
            private static int counter = 1;
    
    
    
    
            static void Main(string[] args)
            {
                compute();
            }
    
    
            private static void compute()
            {
                Console.WriteLine("Enter numbers like this: 2.3");
    
    
                Console.WriteLine("If you want to have the result, enter \"r\".");
    
    
                string sum = Console.ReadLine();
    
    
                try
                {
    
    
                    if (sum.Equals("r"))
                    {
                        Console.WriteLine((double)al[0]);
                        Console.ReadLine();
                    }
                    else
                    { 
                        double d = Double.Parse(sum);
                        al.Add(d);
                        counter++;
                        compute();
                    }
                }
                catch(Exception e) {
                    Console.WriteLine("Something went wrong.");
                }
    
    
            }
        }
    }

  2. #2
    .NET Framework
    .NET 4.5
    Join Date
    Apr 2011
    Posts
    1,979
    Rep Power
    154
    I just tested your code and it worked for me. I entered 1.2 and then 5.7 and then r and it output 1.2.

    Your code is rather dodgy in that you are recursively calling 'compute'. You should rather be prompting the user for input in a loop. Recursion is great when it makes sense but it doesn't make sense here.

  3. #3
    .NET Framework
    .NET 4.5
    Join Date
    Apr 2011
    Posts
    1,979
    Rep Power
    154
    The only reason I can think of that your code wouldn't work as expected is if the culture settings are such that the dot is a group separator rather than a decimal separator.

Similar Threads

  1. List problem
    By Xyozz in forum Console Application
    Replies: 1
    Last Post: 11-23-2017, 7:25 PM
  2. Question How to send generic list<T> in tabular form in body of mail via outlook
    By raushanaj5 in forum C# General Discussion
    Replies: 0
    Last Post: 04-15-2017, 7:22 AM
  3. Question How can I send generic list<T> in the body of outlook mail
    By raushanaj5 in forum VS.NET General Discussion
    Replies: 0
    Last Post: 04-15-2017, 2:08 AM
  4. I been looking at writing to csv file
    By old_man in forum C# General Discussion
    Replies: 5
    Last Post: 11-11-2016, 9:36 AM
  5. Question start writing my first program
    By BrendanDixon in forum General Learning Resources Discussion
    Replies: 0
    Last Post: 03-06-2015, 4:06 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •