×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Comparison between double values (Visual Studio 2005)

Comparison between double values (Visual Studio 2005)

Comparison between double values (Visual Studio 2005)

(OP)
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);
        }
    }
}

RE: Comparison between double values (Visual Studio 2005)

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
 

RE: Comparison between double values (Visual Studio 2005)

(OP)
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

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login


Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close