locked
Input String was not in correct format. RRS feed

  • Question

  • Iam just a beginer and learning C# Language.  Iam getting the "Input String was not in the  correct format". The code was given below.

    namespace WhileDemo
    {
        class Program
        {
            static void Main(string[] args)
            {
                int count = 1;
                int principal,roi ,noy;
                float si;

                while (count <= 3)
                {
                    Console.WriteLine("Enter the values of principal,rate of interest,years");
                    principal = Convert.ToInt32(Console.ReadLine());
                    roi = Convert.ToInt32(Console.ReadLine());
                    noy = Convert.ToInt32(Console.ReadLine());

                    si = (principal * roi * noy) / 100;

                    Console.WriteLine("The simple interest is :\t" +si);

                    count = count + 1;
                }

            }
        }
    }

    Please help me to fix the above problem.


    Jayakumar

    Saturday, May 12, 2012 3:36 PM

Answers

  • try these changes.  I would think if you are enter currency with two decimal palces you would need to convert to a float, not a Int32 (which is an integer with no decimal place).

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    namespace WhileDemo
    {
        class Program
        {
            static void Main(string[] args)
            {
                int count = 1;
                int principal, roi, noy;
                float si;
                string response;
                while (count <= 3)
                {
                    Console.WriteLine("Enter the values of principal,rate of interest,years");
                    response = Console.ReadLine();
                    if (Int32.TryParse(response, out principal) == true)
                    {
                        response = Console.ReadLine();
                        if (Int32.TryParse(response, out roi) == true)
                        {
                            response = Console.ReadLine();
                            if (Int32.TryParse(response, out noy) == true)
                            {
                                si = (principal * roi * noy) / 100;
                                Console.WriteLine("The simple interest is :\t" + si);
                                count = count + 1;
                            }
                        }
                    }
     
                }
            }
        }
    }


    jdweng

    • Proposed as answer by Mike Feng Monday, May 14, 2012 10:50 AM
    • Marked as answer by Mike Feng Tuesday, May 22, 2012 10:14 AM
    Saturday, May 12, 2012 5:01 PM