Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Comparison between double values (Visual Studio 2005)

Status
Not open for further replies.

rogerzebra

Technical User
May 19, 2004
216
SE
Hi All,
I get an error with this code even though is a sample straight out from the book "Start with C#". This should be a fairly simple error but i'm new to C# programming. The issue I have is that it does executing when I'm using an integer for var1 and var2, so a whole number works fine but the purpose of converting the var1 & var2 to double,is to use decimals and when I does it trows me the error "Input string was not in a correct format." Could anyone see why this is not working, that would be much appreciated. Thanks so much
/R

Code:
using System;
using System.Collections.Generic;
using System.Text;

namespace Ch04Ex02
{
    class Program
    {
        static void Main(string[] args)
        {
            string comparison;
            Console.WriteLine("Enter a number:");
            double var1 = Convert.ToDouble(Console.ReadLine());
            Console.WriteLine("Enter another number:");
            double var2 = Convert.ToDouble(Console.ReadLine());
            if (var1 < var2)
                    comparison = "less than";
            else 
            {
                if (var1 == var2)
                    comparison = "equal to ";
                else
                    comparison = "greater than";
            }
            Console.WriteLine("The first number is {0} the second number.",
                comparison);
        }
    }
}
 
I can't reproduce your error. I copied your code directly into a new project and tried it. It works exactly like I would expect it to.

I can create the error if I input a text value into the input instead of a number.

Can you provide some sample numbers you are using?
Which line is reporting the error?

Also, you might want to include these two lines at the end of your code. It will keep the window open until you hit another key...

Code:
            Console.WriteLine("Press any key to close");
            Console.ReadKey();

=======================================
People think it must be fun to be a super genius, but they don't realize how hard it is to put up with all the idiots in the world. (Calvin from Calvin And Hobbs)

Robert L. Johnson III
CCNA, CCDA, MCSA, CNA, Net+, A+, CHDP
C#.NET Programmer
 
SOLVED**
It seems to be working now, I really don't know why it didn't execute in the first place. Thank you mstrmage1768 it is much appreciated.

/R
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top