Beginner's question - Input String was not in correct format

M

Mahmood Ahmad

Hi,

I am getting the error 'Input String was not in correct format' in the
following C# program (indicated line):

using System;
namespace CF7
{
class CalcArea
{
[STAThread]
static void Main(string[] args)
{
const decimal PI = 3.14159M;
int ch2;
float r;
do
{
Console.Write("Enter Radius: ");
r = Single.Parse(Console.ReadLine());
Console.WriteLine("The Area of circle is: {0}", (float)PI*r*r);
Console.Write("Calculate again (y/n): ");
ch2 = Int32.Parser(Console.ReadLine()); // error
} while((ch2 == 'y') || (ch2 == 'Y'));

}
}
}
[END]

Could please guide me to a solution and an understanding why I
encounter an exception?

Grateful
Mahmood Ahmad
 
T

Tom Porterfield

Mahmood said:
Hi,

I am getting the error 'Input String was not in correct format' in the
following C# program (indicated line):

using System;
namespace CF7
{
class CalcArea
{
[STAThread]
static void Main(string[] args)
{
const decimal PI = 3.14159M;
int ch2;
float r;
do
{
Console.Write("Enter Radius: ");
r = Single.Parse(Console.ReadLine());
Console.WriteLine("The Area of circle is: {0}", (float)PI*r*r);
Console.Write("Calculate again (y/n): ");
ch2 = Int32.Parser(Console.ReadLine()); // error
} while((ch2 == 'y') || (ch2 == 'Y'));

}
}
}
[END]

Could please guide me to a solution and an understanding why I
encounter an exception?

Because Int32.Parse doesn't know what to do with the string "y" or "Y". A
quick, but hardly robust and free from pitfalls, fix would be:

using System;
namespace CF7
{
class CalcArea
{
[STAThread]
static void Main(string[] args)
{
const decimal PI = 3.14159M;
int ch2;
float r;
do
{
Console.Write("Enter Radius: ");
r = Single.Parse(Console.ReadLine());
Console.WriteLine("The Area of circle is: {0}", (float)PI*r*r);
Console.Write("Calculate again (y/n): ");
ch2 = Console.ReadLine()[0]; // Read just the first char entered and
behave accordingly
} while((ch2 == 'y') || (ch2 == 'Y'));
}
}
}

--
Tom Porterfield
MS-MVP MCE
http://support.telop.org

Please post all follow-ups to the newsgroup only.
 
C

Christopher Kimbell

The problem is that the Int32.Parse() method expects a string containing
numbers, not characters.
Try the following code instead.

Chris

static void Main(string[] args)
{
const decimal PI = 3.14159M;
string doAgain; // new line
float r;
do
{
Console.Write("Enter Radius: ");
r = Single.Parse(Console.ReadLine());
Console.WriteLine("The Area of circle is: {0}", (float)PI*r*r);
Console.Write("Calculate again (y/n): ");
doAgain = Console.ReadLine().ToLower(); // new line
} while(doAgain == "y") // modified



Mahmood Ahmad said:
Hi,

I am getting the error 'Input String was not in correct format' in the
following C# program (indicated line):

using System;
namespace CF7
{
class CalcArea
{
[STAThread]
static void Main(string[] args)
{
const decimal PI = 3.14159M;
int ch2;
float r;
do
{
Console.Write("Enter Radius: ");
r = Single.Parse(Console.ReadLine());
Console.WriteLine("The Area of circle is: {0}", (float)PI*r*r);
Console.Write("Calculate again (y/n): ");
ch2 = Int32.Parser(Console.ReadLine()); // error
} while((ch2 == 'y') || (ch2 == 'Y'));

}
}
}
[END]

Could please guide me to a solution and an understanding why I
encounter an exception?

Grateful
Mahmood Ahmad
 
M

Mahmood Ahmad

Thank you for your replies. Both the solutions worked.

However, Chris's solution produced a more desirable check on ch2. This
is because (as I understood it) the do-while loop repeats itself only if
the string ch2 is a one-character string "y". It won't repeat if ch2 is
composed of more than one character.

Thanx again,
Mahmood
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top