2.4. Input and Output#
2.4.1. Input: Reading from the Keyboard#
At console or terminal, input is done in two parts: Prompt and read. Because the lack of better visual cues, we use a prompt to instruct the user how to respond. For example:
Console.Write("Enter your name: ");
The user input from keyboard is automatically shown
in the terminal or console window. For a program to take in the characters typed,
another method in the Console class is used:
Console.ReadLine.
The read method reads the data from a line typed at the keyboard by the user. When the user presses the Enter (Return) key, the sequence of characters, form the string value of the read method.
With any method producing a value, the value is lost unless it is immediately used. We therefore assign the value from the read method to a variable like in
string name; // declare variable
Console.WriteLine("Enter your name: "); // prompt for input
name = Console.ReadLine(); // save value to variable
or read in one line
Console.WriteLine("Enter your name: ");
string name = Console.ReadLine();
Console.WriteLine(name); // print to test
Now we have a variable containing the value for us to operate on.
2.4.2. User Input: The UI class#
Andrew N. Harrington and George Thiruvathukal (2021) have prepared some useful C# utility methods for convenient user input and the code is available on github.
To use the code, go to the github repository, and do the following:
Make sure the path is
introcs-csharp-examples/ui/ui.csand the file name isui.cs.Make sure the Code tab is selected and you are seeing the source code.
Use
copy raw filesicon or simply highlight the whole code.In your project folder, create a new file and name it
ui.cs.Paste the copied code to the file.
Change the namespace of the file from IntroCS to
IntroCSCS.
Alternatively, you may just download ui.cs as a raw file from github and place it in the
project folder with the Program.cs file and change the file’s namespace from IntroCS
to IntroCSCS.
Now you can use the input methods in your code. For example, you may use its
PromptInt method to prompting a user input of integer type:
Note
Notice that ui.cs is added alongside Program.cs in the same project folder — this works because a project can contain many .cs files. Remember: one entry point (the Main() method) per project, but a project can have many .cs files.
a = UI.PromptInt("Enter integer leg: ");
2.4.3. Casting (Type Conversion) in Input#
The default value from Console.ReadLine() is of string type and
when we want the user to supply numbers, we need to explicitly convert the
strings to the proper kind of number. We cannot convert the string type to
int implicitly because strings are immutable in C#. Instead, you need to parse
the string and assign to the intended data type.
There are C# built-in methods to do that. int.Parse takes a string parameter
that should be the characters in an int, like “123” or “-25”, and
produces the corresponding int value, like 123 or -25:
Console.WriteLine("Enter your age: "); // prompt
var ageInput = Console.ReadLine(); // save input to variable
Console.WriteLine(ageInput.GetType()); // check type: string
Console.WriteLine(ageInput);
int age = int.Parse(ageInput); // casting type to int
Console.WriteLine(age.GetType());
Console.WriteLine(age);
The double type works similarly as the int type:
string s = "34.5";
double d = double.Parse(s);
d
or, in VS Code:
string s = "34.5";
double d = double.Parse(s);
Console.WriteLine(d);
2.4.4. Console.WriteLine()#
The method Console.Write() does not create a new line when executing.
We can make use of this in cases such as reading user input
string firstName;
Console.Write("Enter you first name: "); // input will start here
firstName = Console.ReadLine();
Console.WriteLine("Hiya, " + firstName + "!");
You have seen output (“print” or “echo”) from the very beginning when creating
the console app project (dotnet console new) and see the executing line of:
Console.WriteLine("Hello, World");
Naturally, we can print variables in addition to the string literal. For example, extending our input code:
Console.WriteLine("Enter your name: ");
string name = Console.ReadLine();
Console.WriteLine("Hello," + name); // use + for concatenation
2.4.5. Composite formatting#
Instead of using the + operator, composite format (the
“fill-in-the-braces”) gives us better control over output using Console.WriteLine
by using positional string format index to separate the string and the data variables.
In composite formatting, you use two arguments: 1) a composite format string with
parameter specifier (format item index) and 2) an object list. When printing,
the objects (variables or literal values) substitute the parameter specifiers one by one.
Observe the last two statements:
string firstName;
Console.Write("Enter you first name: "); // input will start here
firstName = Console.ReadLine();
Console.WriteLine("Hiya, " + firstName + "!");
Console.WriteLine("Hiya, {0}!", firstName);
You can imagine that there would be {1}, {2}… like this:
Console.WriteLine("Hiya, {0} {1}!", firstName, lastName);
With composite formatting, we have the flexibility of writing the output string and place the variables anywhere we prefer in the string.
A more elaborate silly examples that you could test in csharp would be:
string first = "Peter";
string last = "Piper";
string what = "pick";
Console.WriteLine("{0} {1}, {0} {1}, {2}.", first, last, what);
It would print:
Peter Piper, Peter Piper, pick.
where parameter 0 is first (value "Peter"),
parameter 1 is last ( value "Piper"), and
parameter 2 is what (value "pick").
As an example, try the following in csharprepl:
int x = 7;
int y = 5;
Console.WriteLine("{0} plus {1} is {2}; {0} times {1} is {3}.", x, y, x+y, x*y);
to see it prints:
7 plus 5 is 12; 7 times 5 is 35.
Note the following features of the parameters after the first string:
These parameters can be any expression, and the expressions get evaluated before printing.
These parameters to be substituted can be of any type.
These parameters are automatically converted to a string form, just as in the use of the string
+operation.
In fact the simple use of format strings shown so far can be completed replaced by
long expressions with +, if that is your taste.
2.4.5.1. Format Specifiers#
In addition to printing out strings using composite formatting as seen above, a common way to format strings is using the string.Format() method to save the value of the variable into another string. For example, we may save a format string to a variable:
var msg = string.Format("There are {0} balls", 3);
Also, C# has format specifiers [1] for different data types when
formatting data.
For example, D for Decimal type:
int value = 6324;
string output = string.Format("{{{0:D}}}", value);
Console.WriteLine(output);
// The example displays the following output:
// {6324}
You may format two strings together. In the example below, the {1,6:D}
format item takes the second item, format it also as a decimal and the
string length will be 6 characters (right-aligned and padded with empty strings;
the 6 here is the alignment component):
string.Format("{0:D} {1,6:D}", 634, 868); // result: 634 868
C for currency:
int myNumber = 100;
Console.WriteLine("{0:C}", myNumber);
// The example displays the following output
// if en-US is the current culture:
// $100.00
2.4.6. String Interpolation#
The $ character identifies a string literal as an interpolated string. An interpolated
string is a string literal that might contain interpolation expressions. When an
interpolated string is resolved to a result string, the compiler replaces items with
interpolation expressions by the string representations of the expression results.
String interpolation provides a more readable, convenient syntax to format strings.
To identify a string literal as an interpolated string, prepend it with the $ symbol.
Compare composite formatting and string interpolation:
var name = "Mark";
var date = DateTime.Now;
// Composite formatting:
Console.WriteLine("Hello, {0}! Today is {1}, it's {2:HH:mm} now.", name, date.DayOfWeek, date);
// String interpolation:
Console.WriteLine($"Hello, {name}! Today is {date.DayOfWeek}, it's {date:HH:mm} now.");
// Both calls produce the same output that is similar to:
// Hello, Mark! Today is Wednesday, it's 19:40 now.
2.4.7. Writing to the Console#
In csharprepl, you can type an expression and immediately see the result of its evaluation. This is good for test out syntax. In a regular C# program run from a file like in VS Code, you must explicitly give instructions to print to a console/terminal.
This printing is accomplished through a method with a long name: Console.WriteLine.
Like with math, you can pass a method a value to work on, by placing it in
parentheses after the name of the method.
Console is a C# class maintained by the system, that
interacts with the terminal or console window where text output
appears for the program. A method defined in that class is WriteLine.
To refer to a method like WriteLine in a different class, you must indicate
the location of the method with the “dot” notation shown:
class name, then ., then the method. This
gives the more elaborate name needed in the program.
The Console.WriteLine method automatically makes the printing
position advance to the next line, as when you press the Enter or Return key.
A variant, Console.Write, prints the parameter exactly, and nothing else.
Footnotes
2.4.8. Table Formatting with Loop Output#
The following examples were moved from iteration worked examples to keep formatting topics centralized in Input/Output.
2.4.9. Format Output: Console.Write()#
Thus far all of our for loops have used a sequence of successive integers.
You can do a quick test in csharprepl like:
for (int i = 1; i < 5; i++)
{
Console.WriteLine(i);
}
Now let us make the output neater by using the format string (Composite formatting) and
the Console.Write() method:
2.4.10. Formatting Tables#
Reports commonly include tables, often with successive lines generated by
a consistent formula and therefore a good example for coding. As a simple first table, we can show the square,
cube, and square root of numbers 1 through 10.
The Math class has a method Sqrt, so we take the square root with the Math.Sqrt
method. The pattern is consistent, so we can loop easily:
for ( int n = 1; n <= 10; n++)
{
Console.WriteLine("{0} {1} {2} {3}", n, n*n, n*n*n, Math.Sqrt(n));
}
The numbers will be there, but the output is not pretty:
1 1 1 1
2 4 8 1.4142135623731
3 9 27 1.73205080756888
4 16 64 2
5 25 125 2.23606797749979
6 36 216 2.44948974278318
7 49 343 2.64575131106459
8 64 512 2.82842712474619
9 81 729 3
10 100 1000 3.16227766016838
First we might not need all those digits in the square root approximations.
We can replace {3} by {3:F4} to just show 4 decimal places.
We can adjust the spacing to make nice columns by using a further formatting option. The longest entries are all in the last row, where they take up, 2, 3, 4, and 6 columns (for 3.1623). Change the format string:
for ( int n = 1; n <= 10; n++) {
Console.WriteLine("{0,2} {1,3} {2,4} {3,6:F4}",
n, n*n, n*n*n, Math.Sqrt(n));
}
and we generate the neater output:
1 1 1 1.0000
2 4 8 1.4142
3 9 27 1.7321
4 16 64 2.0000
5 25 125 2.2361
6 36 216 2.4495
7 49 343 2.6458
8 64 512 2.8284
9 81 729 3.0000
10 100 1000 3.1623
We are using two new formatting forms:
{index
,fieldWidth
}and
{index
,fieldWidth
:F
}
where index, fieldWidth, and # are replaces by specific literal integers. The new part with the comma (not colon) and fieldWidth, sets the minimum number of columns used for the substituted string, padding with blanks as needed.
If the string to be inserted is wider than the fieldWidth, then the whole string is inserted, ignoring the fieldWidth. For example:
string s = "stuff";
Console.WriteLine("123456789");
Console.WriteLine("{0,9}\n{0,7}\n{0,5}\n{0,3}", s);
generates:
123456789
stuff
stuff
stuff
stuff
filling 9, 7, and then 5 columns, by padding with 4, 2, and 0 blanks. The last line sticks out past the proposed 3-column fieldWidth.
One more thing to add to our power table is a heading. We might want:
n square cube root
To make the data line up with the heading titles, we can expand the columns, with code in example:
Console.WriteLine("{0,2}{1,7}{2,5}{3,7}",
"n", "square", "cube", "root");
for ( int n = 1; n <= 10; n++) {
Console.WriteLine("{0,2}{1,7}{2,5}{3,7:F4}",
n, n*n, n*n*n, Math.Sqrt(n));
}
generating the output:
n square cube root
1 1 1 1.0000
2 4 8 1.4142
3 9 27 1.7321
4 16 64 2.0000
5 25 125 2.2361
6 36 216 2.4495
7 49 343 2.6458
8 64 512 2.8284
9 81 729 3.0000
10 100 1000 3.1623
Note how we make sure the columns are consistent in the heading and further rows: We used a format string for the headings with the same field widths as in the body of the table. A separate variation: We also reduced the length of the format string by putting all the substitution expressions in braces right beside each other, and generate the space between columns with a larger field width.