0

I have the below code for a simple console application in C#. Whenever I input the AIR rate for the second input I get "input string was not in correct format" as the error. What is the correct format and how do I incorporate it into the current code I have made? I am not familiar with different variable types.

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

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            int AIR, MIR, PMT, IP, PP, ABorrowed, Term;

            Console.WriteLine("Please enter the amount borrowed on your loan ");
            ABorrowed = int.Parse(Console.ReadLine());
            Console.WriteLine("Please enter the interest rate for your loan ");
            AIR = int.Parse(Console.ReadLine());
            Console.WriteLine("Please enter term of your loan in months ");
            Term = int.Parse(Console.ReadLine());

            MIR = AIR / 1200;

            PMT = ABorrowed * (MIR/1-(1/(1+MIR)^Term));

            IP = ABorrowed * MIR;

            PP = PMT - IP;

            Console.WriteLine("Your total payment for this month is "+PMT);
            Console.WriteLine("Of that payment " + IP + " is interest rate");
            Console.WriteLine("and the Payment Portion is " + PP);
            Console.ReadLine();

        }
    }
}
3
  • Well what exactly are you entering? Commented Feb 21, 2013 at 16:29
  • You'll want to get familiar with integer division sooner than later. Commented Feb 21, 2013 at 16:29
  • This has nothing to do with your question, but you'll want to look at your PMT = line, as well. ^ doesn't mean "to the power of" - it does a "bitwise or". Commented Feb 21, 2013 at 16:33

3 Answers 3

3

What is the correct format and how do I incorporate it into the current code I have made?

The problem is that you haven't entered an integer on the command line.

We can't really tell any more than that, as you haven't specified what you are writing... but if you just write something as simple as "100" it should be fine (as far as that error is concerned).

I'd strongly advise you to use camelCase for your local variables though, declare them at the point of first use (rather than all at the top of the method), and give them more meaningful names - such as monthlyPayment instead of PMT. As IronMan84 says, you might also want to use decimal for any currency values, although I guess at the level of mortgages, the cents are unlikely to make much difference. Using decimal for the interest rate would make a lot of sense though.

Sign up to request clarification or add additional context in comments.

7 Comments

To be honest, I've done 3 separate courses on programming in 3 separate schools, they all told us to declare all variables at the top of the code. I don't do it anymore, but if OP is still learning/taking courses, odds are he is being instructed to do so :-)
@Flater: Anyone importing language habits from one language to another (some laanguages required this, or at least did) should be told off. Anyone doing so in an educational capacity should think very carefully about the impact they can have on others :(
I think it's mostly taught so students don't run wild with the amount of variables they declare. I suspect it also helps with keeping a consistent naming to your variables. Didn't work on me though, I just started every project with int a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z;, then deleted what was never used. But I'm the kind of coder that only uses good naming because my colleagues ask me to ;)
To clarify, I did that in school projects that I knew were just create once, run once, never look at it again. KIDS! Please don't code like I just did!
@Flater: I care because sloppiness is habit-forming. Best to get people out of the habit as early as possible.
|
0

Interest rates are typically done in the decimal or double variable types. Trying to do that in an int variable will cause these problems.

int types are not built to handle decimal values at all. They are meant for storing whole numbers.

5 Comments

So how would I change it to a double format type without screwing up the rest of my code? I feel like if I change it to double it will screw the rest up.
Just declare that variable on a separate line. They don't need to all be on the same line.
Actually, I believe that they all should actually be doubles, if I'm reading what you're trying to do correctly.
Using double would be a terrible choice, IMO. For a mortgage, int is probably fine - for more detailed values, decimal is appropriate. double is almost never the right type to use for currency values.
@JonSkeet, I bow to superior knowledge. decimal it is.
0

If the value for AIR var will be i.e. xx.xx, you should use decimal or double type vars. Also all vars in your application (finantial) should be decimal/double.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.