x++ / Tutorials / 2. User Input

Table of Contents

Lesson

In the previous lesson, you learned how to write your first x++ project, define and use a variable, and output a value into the terminal. In this lesson, you will learn how to get a user input and how to manipulate strings and numbers.

So far, you should've gotten this in your main.xpp file:

:: main.xpp
var name "Bob"
var age 20
prt "My name is $(name) and I am $(age) years old."

What if you want to ask what the user's name is instead of yours? You can use the read operator to get user input from the terminal. The read operator takes in two arguments, the prompt and the output. The prompt is what the user will see when you get a user input. You can think of it as asking a question. The output is the answer from the user input.

Let's see it:

read "What is your favorite color? " ?favoriteColor

You can replace your var operators and use the read operators instead:

:: main.xpp
read "What is your name? " ?name
read "What is your age? " ?age
prt "Your name is $(name) and you are $(age) years old."

You can also make the name more standout from the rest of the string by making it all capitalized. You can uppercase all the letters in a string by using the upr operator:

var myString "Hello, world!"
upr myString
prt myString  :: Outputs: "HELLO, WORLD!"

Let's try it:

:: main.xpp
read "What is your name? " ?name
upr name
read "What is your age? " ?age
prt "Your name is $(name) and you are $(age) years old."

You can also use mathematical operators to calculate the user's birth year. By subtracting the user's age from the current year, you get their birth year. You can use the sub operator for this purpose:

sub 5 10 ?output
prt output  :: Outputs: -5

However, because the user input always gives us a string, you need a way to convert it into a number. You can use the int operator for this:

var myInteger "5"
int myInteger
prt myInteger  :: Outputs: 5

Let's give it a shot:

:: main.xpp
read "What is your name? " ?name
upr name
read "What is your age? " ?age
int age
sub 2024 age ?birthYear
prt "Your name is $(name) and you were born in $(birthYear)."

Now it will ask the user their name and age and output their name and birth year. Incredible isn't it?

In the next lesson, you will learn how to make branches using the if operator.


Last Updated: March 9th, 2024 by iiPython

↑ Go To Top