Function Getint - C# Programming Exercise

In this exercise of C#, you will learn how to create a function named GetInt, which displays on screen the text received as a parameter, asks the user for an integer number, and repeats the request if the entered number is not within the specified minimum and maximum range parameters. Finally, the function returns the entered number. This exercise is perfect for practicing how to validate user inputs and how to repeat a request until a valid value is entered in C#. For example, if the program asks for age with the text "Enter your age", and the user enters values outside the range (such as 180 or -2), the program will repeat the request until a valid number is entered. This type of exercise is great for learning how to handle user inputs and validations in C#.

With this exercise, you will enhance your C# skills and learn how to interact with users, controlling allowed value ranges.

 Category

Functions

 Exercise

Function Getint

 Objective

Write a C# function named "GetInt", which displays on screen the text received as a parameter, asks the user for an integer number, repeats if the number is not between the minimum value and the maximum value which are indicated as parameters, and finally returns the entered number:

age = GetInt("Enter your age", 0, 150);

would become:

Enter your age: 180
Not a valid answer. Must be no more than 150.
Enter your age: -2
Not a valid answer. Must be no less than 0.
Enter your age: 20

(the value for the variable "age" would be 20)

 Write Your C# Exercise

// Importing the System namespace to access basic system functions
using System;

class Program
{
    // Main method where the program starts
    public static void Main()
    {
        // Call the GetInt function and store the returned value in the age variable
        int age = GetInt("Enter your age", 0, 150); // Get the user's age within a valid range
        Console.WriteLine("Your age is: " + age); // Output the valid age entered by the user
    }

    // Function GetInt to request an integer from the user within a specified range
    public static int GetInt(string prompt, int min, int max)
    {
        int result; // Variable to store the user's input
        while (true) // Loop indefinitely until a valid input is given
        {
            Console.Write(prompt + ": "); // Display the prompt to the user
            string input = Console.ReadLine(); // Read the user's input

            // Check if the input can be converted to an integer
            if (int.TryParse(input, out result))
            {
                // Check if the result is within the specified range
                if (result < min)
                {
                    Console.WriteLine($"Not a valid answer. Must be no less than {min}."); // Invalid if less than min
                }
                else if (result > max)
                {
                    Console.WriteLine($"Not a valid answer. Must be no more than {max}."); // Invalid if greater than max
                }
                else
                {
                    return result; // Return the valid result
                }
            }
            else
            {
                Console.WriteLine("Not a valid integer. Please enter a valid number."); // Invalid if not an integer
            }
        }
    }
}

 Share this C# exercise

 More C# Programming Exercises of Functions

Explore our set of C# programming exercises! Specifically designed for beginners, these exercises will help you develop a solid understanding of the basics of C#. From variables and data types to control structures and simple functions, each exercise is crafted to challenge you incrementally as you build confidence in coding in C#.

  •  Function tasks database

    In this exercise of C#, you will improve a "tasks database" program by splitting it into multiple functions. This type of exercise is great for l...

  •  Function Greatest value in a array

    In this exercise of C#, you will learn how to create a function that returns the greatest value stored in an array of real numbers, which is specified as a par...

  •  Function factorial (iterative)

    In this exercise of C#, you will learn how to create an iterative (non-recursive) function to calculate the factorial of the number specified as a parameter. T...

  •  Function WriteTitle

    In this exercise of C#, you will learn how to create a function named "WriteTitle" that writes a text centered on the screen, in uppercase, with extra spaces, ...

  •  Function return value for Main

    In this exercise of C#, you will learn how to create a program in which you can use the WriteTitle function to write a title that the user will specify ...

  •  Function CountDV

    In this exercise of C#, you will learn how to create a function that calculates the amount of numeric digits and vowels a text string contains. The function...