1

I just wrote my first C# console application, I am still a beginner. Anyway, I tried the code below and it seems to work, its for solving quadratic equations. I'd like to add code for a situation whereby a user inputs a string instead of an integer and give an error message any ideas as to how to implement this?

namespace Quadratic_equation
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("welcome to seyi's quadratic calculator!!!");
            Console.Write("a:");
            double a = Convert.ToInt32(Console.ReadLine());


            Console.Write("b:");
            double b = Convert.ToInt32(Console.ReadLine());
            Console.Write("c:");
            double c = Convert.ToInt32(Console.ReadLine());
            if ((b * b - 4 * a * c) < 0) {
                Console.WriteLine("There are no real roots!");
            }
            else {
                double x1 = (-b + Math.Sqrt((b*b)-4*a*c)) /2*a;
                double x2 = (-b + Math.Sqrt((b*b)-4*a*c)) /2*a;
                Console.WriteLine("x:{0}",x1);
                Console.WriteLine("y:{0}",x2);
            }

            Console.ReadKey();
        }
    }
}
1
  • Just use TryParse and if it returns false show error and don't continue. Commented Mar 30, 2014 at 13:39

3 Answers 3

2

You can use Int32.TryParse method to check your string is a valid integer or not. This method returns a boolean value for your conversation is succeed or not.

Converts the string representation of a number to its 32-bit signed integer equivalent. A return value indicates whether the conversion succeeded.

And I don't understand why you want to keep as double the return value of Convert.ToInt32 method. These factors (a, b, c) should be integer, not double.

int a;
string s = Console.ReadLine();
if(Int32.TryParse(s, out a))
{
   // Your input string is a valid integer.
}
else
{
  // Your input string is not a valid integer.
}

This Int32.TryParse(string, out int) overload uses NumberStyle.Integer as default. That means your string can have one of these;

  • Trailing white spaces
  • Leading white spaces
  • Leading sign character
Sign up to request clarification or add additional context in comments.

2 Comments

strings is an invalid variable type. Was that a typo?
@jsve Yeah, that was a typo. Fixed.
1

Check out int.TryParse

   int number;
  bool result = Int32.TryParse(value, out number);
  if (result)
  {
     Console.WriteLine("Converted '{0}' to {1}.", value, number);         
  }
  else
  {
     if (value == null) value = ""; 
     Console.WriteLine("Attempted conversion of '{0}' failed.", value);
  }

Comments

1

Use a try-catch block in a do-while loop:

bool goToNextNum = false;
do
{
    try
    {
        double a = Convert.ToInt32(Console.ReadLine());
        goToNextNum = true;
    }
    catch
    {
        Console.WriteLine("Invalid Number");
    }
} while (goToNextNum == false);

This will loop until a is a valid number.

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.