Jump to content
Xtreme .Net Talk

Recommended Posts

Posted (edited)

In my simple program (console application) below, I am requesting two integer values and then simply using the AddNumbers() function to return the result.

 

However, the lines of code "Num1 = Console.ReadLine();" and ""Num2 = Console.ReadLine();" produce a build error reporting that it cannot implicitly convert type 'string' to 'int'.

 

using System;

namespace Calculator
{
class CCalculator
{
	[sTAThread]
	static void Main(string[] args)
	{
		//local variables
		int Num1;
		int Num2;

		//welcome message
		Console.WriteLine("Welcome to Calculator 1.0");
		
		//request first number
		Console.Write("Number 1: ");
		Num1 = Console.ReadLine();
		
		//request second number
		Console.Write("Number 2: ");
		Num2 = Console.ReadLine();

		//display result
		Console.WriteLine("Total: " + AddNumbers(Num1, Num2));
		
		//finish
		Console.WriteLine("Press any key to continue...");
		Console.Read();
	}

	static int AddNumbers(int Number1, int Number2)
	{
		//add numbers and return result
		return (Number1 + Number2);
	}
}
}

Thanks in advance...

Shuaib

Edited by VisualDeveloper
Windows XP Professional, Visual Studio .NET Enterprise Architect, Dell Dimension 8100, 15" TFT Monitor, Intel Pentium 4 CPU 1700MHz, 40GB Hard Disk, nVidia GeForce 3, NEC CD-RW 16x, Samsung DVD 16x.
  • *Experts*
Posted

This would be a good time to learn what try...catch does as well (take a look at the help).

 

-nerseus

"I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut
Posted (edited)

I've been looking into the so-called "Try...Catch" statement this afternoon!

 

Is there any way my program below could be improved or optimized?

using System;

namespace Calculator
{
class CCalculator
{
	[sTAThread]
	//application entry point
	static void Main(string[] args)
	{
		//local variables
		int Num1 = 0;
		int Num2 = 0;

		bool Num1OK = false;
		bool Num2OK = false;

		//welcome message
		Console.WriteLine("Welcome to Calculator 1.0");
		
		while (Num1OK == false)
		{
			try
			{
				//request first number
				Console.Write("Number 1: ");
				Num1 = int.Parse(Console.ReadLine());

				//input for first number is valid
				Num1OK = true;
			}
			catch (Exception E)
			{
				//display error information
				Console.WriteLine("Error: " + E.Message);
			}
		}

		while (Num2OK == false)
		{
			try
			{
				//request first number
				Console.Write("Number 2: ");
				Num2 = int.Parse(Console.ReadLine());

				//input for first number is valid
				Num2OK = true;
			}
			catch (Exception E)
			{
				//display error information
				Console.WriteLine("Error: " + E.Message);
			}
		}

		//display result
		Console.WriteLine("Total: " + AddNumbers(Num1, Num2));
		
		//finish
		Console.WriteLine("Press any key to continue...");
		Console.Read();
	}
	
	//function for adding two numbers
	static int AddNumbers(int Number1, int Number2)
	{
		//add numbers and return result
		return (Number1 + Number2);
	}
}
}

Cheers,

Shuaib

Edited by VisualDeveloper
Windows XP Professional, Visual Studio .NET Enterprise Architect, Dell Dimension 8100, 15" TFT Monitor, Intel Pentium 4 CPU 1700MHz, 40GB Hard Disk, nVidia GeForce 3, NEC CD-RW 16x, Samsung DVD 16x.
  • 3 weeks later...
  • *Experts*
Posted

Try something like this:

private bool IsInt(string test)
{
   try
   {
       int dummy = Int32.Parse(test);
       return true;
   }
   catch
   {
       return false;
   }
}

 

There is no built in "IsNumeric" function in .NET. VB.NET supports the old IsNumeric function (I think) but only for backwards compatability. I wouldn't recommend using it. Also, using the Int32.Parse() method, you can specify extra options like if you want to allow commas as part of a valid string or not.

 

-Nerseus

"I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...