0

As a Newbie to C#, I have some code in C#.

The Program is a kind of Daily Money Saving Algorithm!

I'm struggling to produce the output in some alignment way as shown in below image:

enter image description here

Code I have written:

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

namespace RemainingDaysCalculation
{
    internal class DailyMoneySavingMultiplier
    {
        public static void Main()
        {
            DateTime currentDate = DateTime.Now;
            int daysInYear = DateTime.IsLeapYear(currentDate.Year) ? 366 : 365;
            int daysLeftInYear = daysInYear - currentDate.DayOfYear; // Result is in range 0-365.
            int finisheddaysCount = daysInYear - daysLeftInYear;
            Console.WriteLine("daysLeftInYear is {0}", daysLeftInYear);
            Console.WriteLine("finishedDaysCount is {0}",finisheddaysCount);
            int savings = (daysInYear * (daysInYear + 1)) - (finisheddaysCount * (finisheddaysCount -1));
            Console.WriteLine(savings);

            //Case 2:
            Console.WriteLine("_________________________________");
            Console.WriteLine(" Day No || Daily Saving || Total Saved");
            for (int i=1; i <= daysInYear; i++)
            {
                
                
                Console.WriteLine("{0,-10} || {1,-10} || {2,5}", i, (i * 2), (i * (i + 1)));
                //Console.Write(i);
                //Console.Write(i * 2);
                //Console.Write(i * (i + 1));
                
            }
            Console.WriteLine("_________________________________");

        }
    }
}

Output:

enter image description here

Update:

I have tried the 2 ways from the given workarounds in this reference, but i'm getting the exception like enter image description here

Lee Taylor
  • 7,761
  • 16
  • 33
  • 49
  • 1
    Does this answer your question? [How To: Best way to draw table in console app (C#)](https://stackoverflow.com/questions/856845/how-to-best-way-to-draw-table-in-console-app-c) – Oleg Apr 30 '22 at 11:51
  • I have tried the 2 ways from that workarounds in the given reference but failed to get the correct alignment as you see here https://i.imgur.com/zUo76Ta.png –  Apr 30 '22 at 12:13
  • 1
    You can try this source: https://github.com/khalidabuhakmeh/ConsoleTables/blob/master/src/ConsoleTables/ConsoleTable.cs – Oleg Apr 30 '22 at 12:17
  • 1
    https://github.com/spectreconsole/spectre.console – Oliver Apr 30 '22 at 12:20
  • @Oleg, it's a very long code given in the above GitHub reference! Could you suggest me short code to align the 3 columns! –  Apr 30 '22 at 12:20
  • 1
    https://stackoverflow.com/questions/10160770/how-to-print-list-as-table-in-console-application – Oleg Apr 30 '22 at 12:25
  • 1
    Github sample:https://github.com/khalidabuhakmeh/ConsoleTables, you can just download the provided class. – Oleg Apr 30 '22 at 12:29
  • How to resolve this errors https://i.imgur.com/mLiXG46.png –  Apr 30 '22 at 12:36
  • Hello @Oleg, Could you take my code given in the question and modify the alignment for better look so that I'll understand! –  Apr 30 '22 at 12:46

1 Answers1

0

Thanks to @Oliver and @Oleg for pointing me in the right direction and giving me the helping references.

Finally, I figured out this issue using the code below:

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

namespace RemainingDaysCalculation
{
    internal class DailyMoneySavingMultiplier
    {
        public static void Main()
        {
            DateTime currentDate = DateTime.Now;
            int daysInYear = DateTime.IsLeapYear(currentDate.Year) ? 366 : 365;
            int daysLeftInYear = daysInYear - currentDate.DayOfYear; // Result is in range 0-365.
            int finisheddaysCount = daysInYear - daysLeftInYear;
            Console.WriteLine("daysLeftInYear is {0}", daysLeftInYear);
            Console.WriteLine("finishedDaysCount is {0}",finisheddaysCount);
            int savings = (daysInYear * (daysInYear + 1)) - (finisheddaysCount * (finisheddaysCount -1));
            Console.WriteLine("Saving Value " +savings);

            //Case 2:
            Console.WriteLine("_________________________________");
            var table = new ConsoleTable("Day No", "Daily Saving", "Total Saved");
            for (int i = 1; i <= daysInYear; i++)
            {
                table.AddRow(i, (i * 2), (i * (i + 1)));
            }

            table.Write();
            Console.ReadKey();


        }
    }
}

Result:

enter image description here