h
Getting Basic User Input
There are four main ways to pause the program and ask the user for a value.
- Getting an int value
- Getting a double value
- Getting a string value
- Getting a point on the screen
Getting an int value
To ask for an int, use the function: win.get_int(string prompt);
Example:
- int age = win.get_int(“How old are you?”);
- int radius = win.get_int(“Enter a value for the radius.”);
In example 1, the program will pause and display the message “How old are you?” at the top of the screen. The user will then type in a numerical value and press enter. That value typed will then be returned and stored in the variable age.
Getting a double value
To ask for a double, use the function: win.get_double(string prompt);
Example:
- double price = win.get_double(“How much does the car cost?”);
- double mass = win.get_double(“How much does the ball weigh?”);
In example 1, the program will pause and display the message “How much does the car cost?” at the top of the screen. The user will then type in a numerical double value and press enter. That value typed will then be returned and stored in the variable price.
Getting a string value
To ask for a string, use this function: win.get_string(string prompt);
Example:
- string name = win.get_string(“What is your name?”);
- string part = win.get_string(“Which part would you like to buy?”);
In example 1, the program will pause and display the message “What is your name?” at the top of the screen. The user will then type in a string value and press enter. That value typed will then be returned and stored in the variable name.
Getting a Point on the screen
To ask for a point on the window, use this function: win.get_click(string prompt);
Example:
- Point location = win.get_click(“Click where you want to start.”);
- Point end_point = win.get_click(“Click on the screen for the end point of the line to make.”);
In example 1, the program will pause and display the message “Click where you want to start.” at the top of the screen. The user will then click a location on the screen. That Point location on the screen will be returned and stored in the variable location.
Changing the color of the displayed message
Similar to drawing objects, you can also specify a color for all the prompts. The default color is BLACK. To set the color of the prompts, just add a color to the end of the function.
Example:
- win.get_int(“How old are you?”, BLUE);
- win.get_double(“What is the weight of the ball?”, GREEN);
- win.get_click(“Click where you want to start.”, WHITE);
So for example 1, the message “How old are you?” will be displayed at the top of the screen in the color BLUE instead of BLACK.
Those are all the basics for getting user input. Soon it will be Instinct to you!