October 30, 2019
The goal of our program is to convert integers given by the user into the corresponding character. Any numer between 32 and 126 can be converted into a “human-readable”, or printable, character. “Control character” are exist, and correspond to the values between 0 and 31. However, they cannot be displayed at the screen and should be ignored.
Your program should ask again when the user enters a value outside the scope of printable characters, and display the corresponding character once given a proper value. As a bonus, you can try to make your whole program loop, so that the user would be given the possibility of converting multiple numbers without having to re-execute the program.
/*
* C. Aubert
* 10/30/2019
* CSCI 1301 -- Review Session
* This small program asks the user for an
* integer, and converts it into a character
* that is then displayed at the screen.
* Note that invalid input is not processed.
* Once the conversion is done, the user is asked
* whenever they want to convert another integer.
*/
using System;
class Program
{
public static void Main(string[] args)
{
string uistring; // To store the string entered by the user.
int uiint; // To store user input once converted into an int.
do // This first loop to allow the user to enter multiple values.
{
do // This loop to perform input validation.
{
Console.WriteLine("Enter a number between 32 and 126 to convert into a character.");
/*
* Any numer between 32 and 126 can be converted into a
* "human-readable"',or printable, character.
* cf. https://en.wikipedia.org/wiki/ASCII#Printable_characters
* "Control character" are also present, and correpond
* to the values between 0 and 31. But they can not be
* displayed at the screen.
* Cf. https://en.wikipedia.org/wiki/ASCII#Control_characters
*/
uistring = Console.ReadLine();
int.TryParse(uistring, out uiint);
}
while (uiint < 32 || uiint > 126);
/*
* This previous test actually takes care of the case where
* the user enter something that is not an int.
* Indeed, if the user enters, say, "No", then
* the TryParse method assign 0 to uiint, and
* 0 makes this test true, hence asking for
* another value.
*/
/*
* Outside the inner loop, we display the result of the
* conversion and ask the user if they want to perform another
* conversion.
*/
Console.WriteLine($"The character corresponding to {uistring} is {(char)uiint}.");
Console.WriteLine("Do you want to convert another integer?");
Console.WriteLine("Enter \"Y\" for yes, anything else to exit.");
} while (Console.ReadLine() == "Y");
}
}You can also download this solution as a project.