Search code examples
c#loopsexceptionvisual-studio-2022do-while

How do make this C# program keep looping until the user has entered an integer?


I want the program to keep asking the user to put in a number until they finally use the correct data type, instead of just exiting the program. This is what I have written.


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace test1
{
    class Program
    {
        static void Main(string[] args)
        {
            //declare and initialize variables
            int a = 0;
            int b = 0;
            int area = 0;
            string read;
            
            do { 
                try
                {
                    //input data solicited
                    Console.WriteLine("Rectangle height: ");
                    read = Console.ReadLine();
                    a = int.Parse(line);

                    Console.WriteLine("Rectangle width: ");
                    read = Console.ReadLine();
                    b = int.Parse(line);

                    //operation
                    area = a * b;

                    //print result
                    Console.WriteLine("The area of the rectangle is: " + area + " cm²");
                    
                }
                catch (Exception e)
                {
                    Console.WriteLine("ERROR. Wrong data type.");
                }
            }while (e);
        }
    }
} 

I tried using a boolean as a random guess to make the do-while loop keep going until the user successfully entered a whole number but that didn't work either. The while (e); you see at the bottom was also a wild guess.

                    Console.WriteLine("The area of the rectangle is: " + area + " cm²");
                    cont = false; 
                    
                }
                catch (Exception e)
                {
                    Console.WriteLine("ERROR. Ingresa un numero entero.");
                    cont = false; 
                }
            }while (cont);

I can't use if statements for this.


Solution

  • There's two ways to make it loop until number.

    One way is to create a bool starting with false and then loop until the bool is true, so if succeed, then doesn't continue the loop.

    And another way is to create a do {} while (true) but with break on end of try stack.

    Since that to the code reach at the end (being break or isValid = true), it would need the code to work correctly.

    If you prefer, you can replace try {} with a variable type check, if it's a number, then proceed, if anything else, continue to loop (with negate condition).

    Using variable

    ...
    static void Main(string[] args)
    {
        // declare and initialize variables
        int a = 0;
        int b = 0;
        int area = 0;
        string read;
        bool isValid = false;
    
        while (!isValid) { 
            try
            {
                // Input data solicited
                Console.WriteLine("Rectangle height: ");
                read = Console.ReadLine();
                a = int.Parse(read);
    
                Console.WriteLine("Rectangle width: ");
                read = Console.ReadLine();
                b = int.Parse(read);
    
                //operation
                area = a * b;
    
                //print result
                Console.WriteLine("The area of the rectangle is: " + area + " cm²");
                isValid = true;
                
            }
            catch (Exception e)
            {
                Console.WriteLine("ERROR. Wrong data type, expected number.");
            }
        };
    }
    ...
    

    Using break

    ...
    static void Main(string[] args)
    {
        // declare and initialize variables
        int a = 0;
        int b = 0;
        int area = 0;
        string read;
    
        do { 
            try
            {
                ...
                break
                
            }
            catch (Exception e)
            {
                ...
            }
        } while (true);
    }
    ...
    

    Also, there was one error in a = int.Parse(line), there's no line var. Same with variable b.