3

I am having trouble with types in c#. I have an int input on a console application and I would like for a (non-numeral characters) string input to be put back into my existing but not pictured do while loop like an error. I don't want to convert nonnumeral string values to int, but rather want to protect my object from crashing and return it to the beginning of my loop when an unacceptable value (i.e. a alphabetic string value) is presented. For example:

Console.WriteLine("Please input an odd number:");
int input1 = int.Parse(Console.ReadLine());
if (input1 % 2 == 0)
{
    Console.WriteLine("Please input an odd number.");
    //is even
}
else
{
    TotalOdd = TotalOdd + input1;
    TotalNumeral++;
    Console.WriteLine("Accepted.");
    //is odd
}

I would like for a string input to be treated like an "even" number. I know this seems like an amateurish mistake, but I am actually at a loss... Any help is appreciated.

ganchito55
  • 3,559
  • 4
  • 25
  • 46
Xenopod
  • 37
  • 6
  • I don't know if I got your question right. But you can convert a String to an Integer. Convert.ToInt32(). Since the user is able to provide any input, you can check if its an Integer first. int.TryParse(input1, out value) – Bin4ry Jun 19 '17 at 22:46
  • are you asking if the user inputs an even number, how would you go back and ask for more input until they put in an odd number? – Steve Jun 19 '17 at 22:47
  • I'm not sure you quite grasp what a string 'is' - all the inputs are strings, till you do your int.parse - but anyway, perhaps you should be looking at int.tryparse. Sorry if I'm hopelessly missing your point here. – peterG Jun 19 '17 at 23:06
  • Use `TryParse`. – RJM Jun 20 '17 at 02:34

3 Answers3

2

I usually use int.TryParse() in these situations, just to be sure the number entered is a valid integer. This method returns true if the conversion succeeds, and takes an int out parameter, which will be set to the integer value if it succeeded. Then you can add the check that Steve has in his answer above:

int input;

do
{
    Console.Write("Please input an odd number: ");
} while (!int.TryParse(Console.ReadLine(), out input)
         || input % 2 == 0);
Rufus L
  • 36,127
  • 5
  • 30
  • 43
1

User inputs are always received as String, you can't avoid that because user can type any thing keyboard can write. So you have to parse it as you are doing but you miss catching two errors:

  1. String may not be in the correct form (may not be a number, i.e includes alphabetical characters)
  2. String may be a number greater than the max for Int32

so you just have to add the following to your code:

     try {
        int input1 = Int32.Parse(Console.ReadLine());
     }
     catch (FormatException) {
        //not a number input
        continue;//iterate to the next
     }   
     catch (OverflowException) {
        //inform the user about the error...
        //if this often happens try parsing Int64.Parse
     } 

you can also use int.TryParse,they give the same result. check this question What is better: int.TryParse or try { int.Parse() } catch

A. Harkous
  • 252
  • 1
  • 3
  • 14
0

The following code will loop until an odd number is entered, and then input1 will hold the odd value;

  int input1 = 0;
    do {
        Console.WriteLine("Please input an odd number:");
        input1 = int.Parse(Console.ReadLine());
    } while (input1 % 2 == 0);
Steve
  • 502
  • 3
  • 12