Home > OS >  C# console app finishing with code 0 instead of providing the output it's supposed to calculate
C# console app finishing with code 0 instead of providing the output it's supposed to calculate

Time:11-12

The program is supposed to calculate compound interest by getting several inputs from the user and then applying those with the compound interest formula, however while gramatically correct, the program does everything correctly except for ouputting the calculated value. Any ideas why this might be happening?

   using System;

   namespace compoundCalc
  {
    class Program
    {
        static void Main(string[] args)
        {
            Console.Write("Enter investment sum:");
            int investment = Convert.ToInt32(Console.ReadLine());
            Console.Write("Enter annual interest rate:");
            double interestRate = Convert.ToDouble(Console.ReadLine());
            Console.Write("Enter the number of times per year that interest is compounded per period:");
            int compoundNumber = Convert.ToInt32(Console.ReadLine());
            Console.Write("Enter the number of periods the money is invested for:");
            int investmentPeriod = Convert.ToInt32(Console.ReadLine());
            double nt = Math.Pow((compoundNumber * investmentPeriod),(1 interestRate / compoundNumber ));
            double futureCapital = investment * nt;
            Console.WriteLine("The future value of your investment is:",  Convert.ToString(futureCapital));

        }
    }
}

CodePudding user response:

Console.WriteLine(String, Object) method signature requires the string include a format character, which you don't have.

If you want to use String interpolation, then that would look like

Console.WriteLine($"The future value of your investment is: {futureCapital}");

CodePudding user response:

You need to tell the Console where to display the futureCapital add this {0} to last of your string

Console.WriteLine("The future value of your investment is: {0}",Convert.ToString(futureCapital));

or you can use string concatenation

Console.WriteLine("The future value of your investment is: "   futureCapital);

or more convenient use string interpolation $

Console.WriteLine($"The future value of your investment is:{futureCapital}");
  •  Tags:  
  • c#
  • Related