This lab serves multiple goals:
- To reinforce your understanding of
TryParse
statements, - To help you understand the value returned by
TryParse
, - To help you understand the difference between
int.TryParse
anddouble.TryParse
, - To familiarize you with the tools to validate user input,
- To have you validate different kinds of inputs from the user, and
- (Optional) To manipulate user-input validation with classes.
Discovering TryParse’s Behaviour
In your IDE, copy and paste the following:
For each input in the table below:
- in the returns column write whether the TryParse operation succeeded
(
true
orfalse
). - in the value column write the obtained numeric value after the
TryParse
operation.
The first few lines are given as examples; your task is to complete the
rest of the table. You will need to update the program by replacing all
the occurrences of int
with double
to test if your answers were
correct in the second half of the table.
int.TryParse | double.TryParse | |||
---|---|---|---|---|
Input | returns | value | returns | value |
"160519" | true | 160519 | true | 160519 |
"9432.0" | false | 0 | true | 9432.0 |
"nope" | false | 0 | false | 0 |
"12,804" | ||||
"+5102" | ||||
"2+2" | ||||
" -322 " | ||||
"(72);" | ||||
"000" | ||||
"78 095" |
Question: : After completing the table, can you detect a pattern between “returns” and “value”?
Validating Inputs {#validating-inputs-1}
For the following problems, perform this series of steps:
- ask the user for input,
- check that the input is valid according to the specific problem, and
- perform the subsequent action.
If the provided input is not valid, request new input from the user until the user provides valid input. The beginning of the first and second problems are given to get you started.
-
Write a loop that displays:
Enter yes to quit:
and then checks the user’s input. Consider any of these variations to mean yes: “yes”, “YES”, “y”, “Y”. Once the user enters yes, exit the loop.Solution (sketch)
-
Ask the user to enter a positive integer between (and including) 2 and 100. Validate the input, compute the sum of integers starting from 1 up to the integer the user entered, and display that sum. Here are examples:
- if the user enters 5, compute: 1 + 2 + 3 + 4 + 5, then display 15 on the screen
- if the user enters 8, compute: 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8, then display 36 on the screen
Solution (sketch)
-
Do the following problem using the
decimal
type. Ask the user to enter any numbers which can be positive, negative, or zero. Ignore all non-numeric inputs usingTryParse
. Choose an appropriate sentinel value to enable the user to indicate when they are done. Compute and display the average of all the numbers that the user entered. If the user didn’t enter any numbers, display “You did not enter any numbers”.Here is an example of execution, where the user input is u͟n͟d͟e͟r͟l͟i͟n͟e͟d͟, and hitting “enter” is represented by “⏎͟”:
Pushing Further (Optional)
This part is focused on input validation with classes. It requires reading a lengthy (but not very complicated) class implementation and then, improving it. It is difficult and is designed to offer an interesting challenge. However, you should be able to complete such exercises by the end of the semester without too many difficulties.
Start by downloading the LoanCalculator solution which mixes classes and decision structures. Spend some time studying the implementation to understand what the program is doing and how it is doing it.
Next edit the Program.cs
file of the LoanCalculator
solution to add
the following validation features:
- Users that enter a value other than
A
,a
,H
,h
,O
, oro
for the loan type will be asked again; they will be asked until they give a valid answer. - Users that enter a credit score that is not between 300 and 850, or one that is not an integer, will be asked again; they will be asked until they give a valid answer.
- Users that enter an amount needed or a down payment that is not a decimal, or is a negative decimal, will be asked again; they will be asked until they give a valid answer.
- (Optional) Use the
ToLower()
or
ToUpper()
methods of the
char
class to make the program more readable — you will be able to greatly simplify theif
statement that checks the loan type. - (Optional, hard) Write a method for the Loan class that takes a
character as an argument, and returns the string describing the type
of loan designated by that character. Then, use this method in the
ToString
method and in the application program instead of doing it manually.
Solution (sketch)
You can find a possible solution in this archive.