1.5 Getting Input

In order for our programs to do something different each time we run them, we need to have some way of interacting with a user. We can allow the user to enter data with the keyboard by using the get statement. Here is a program that asks for the user's age, name and address and then prints back what the user typed in a nice neat format.

var name : string
var address : string
var age : int

put "Please enter your age"
get age
put "Please enter your name"
get name
put "Please enter your address"
get address

put "Hi, ", name, ". I hope you are well."
put "I notice that you are ", age, " years old."
put "You live at ", address, "." 

In the output that follows, the user's input will be bolded and slightly larger.

Please enter your age
16
Please enter your name
Jane Doe
Please enter your address
Hi, Jane. I hope you are well.
I notice that you are 16 years old.
You live at Doe.

Notice how only Jane was read into name. Turing stops reading strings as soon as it encounters a space. When it tries to read the address it picks up where it stopped reading before. That is why Doe gets read in as the address. So even though the prompt for the address (Please enter your address) is printed you never get a chance to type it in. You need to keep this in mind if you try to read any numbers after strings. If the computer tries to read an int but gets characters instead, the program will crash.

var name : string
var age : int

put "Please enter your name"
get name
put "Please enter your age"
get age

put "Hi, ", name, ". I hope you are well."
put "I notice that you are ", age, " years old." 

If you type Jane Doe in response to the first question only Jane gets read into name as we mentioned before. Now when the computer tries to read age it will read the leftover Doe instead. This isn't a valid int so the program can't store it in age and it will crash.

There are two ways to get the computer to read more than one word into a string. When the user enters the string they can put quotes around the person's name. Here is the output from the first example using this approach:

Please enter your age
16
Please enter your name
"Jane Doe"
Please enter your address
"17 Broadway Avenue"
Hi, Jane Doe. I hope you are well.
I notice that you are 16 years old.
You live at 17 Broadway Avenue.

This is probably not the best solution however. We don't want the user to have to type in quotes. To make a get read in an entire line you just have to add :* after the variable. In the first program change get name to get name:* and change get address to get address:*. Then the user can type strings in the way they would expect to. Here is another example:

var status : string
var ranking : int

put "How do you feel about Turing so far?"
get status:*
put "On a scale of 1 to 10, how confident are you?"
get ranking

put "*****************************************************"
put "Feelings: ", status
put "Ranking:  ", ranking
put "*****************************************************" 

Here is a sample run of the program:

How do you feel about Turing so far?
So far I think I'm getting the hang of it
On a scale of 1 to 10, how confident are you?
9
*****************************************************
Feelings: So far I think I'm getting the hang of it
Ranking:  9
*****************************************************


Exercise 1.5

  1. Write an interactive program that prompts the user for their given and family names. It should then print a message welcoming them.
  2. Write an interactive program that prompts the user for some personal data. The program should prompt the user for their name, their age in years, their height in centimetres (to the nearest centimetre), their weight in kilograms (to the nearest tenth of a kilogram), and their favourite letter. The program should then print the personal data in a neat, easily read form.