User Input
Getting feedback from the user, modifying it, and producing a new result
Have you ever wondered how online websites take in and verify the usernames and passwords that you enter? Well, these websites ask the user for input and process the information that the user sends in, typically storing this information in variables.
This process used by those programs is called User input, and it is any information that is sent by the user to a computer using an input device. In almost every program, user input is utilized in some form. For example, console applications, desktop applications, databases, and website pages all use user input.
Below is an example of prompting the user for a password, taking in the user input, storing the input into a variable, and printing the password back out.
Code:
password = input("Enter a password: ") # prompting user and reading input
print("Password is " + password) # outputting user input
Output:
> Enter a password:
User types in "Curry30"
> Password is Curry30
Code:
import java.util.Scanner;
public class Main {
  public static void main(String[] args) {
    Scanner myObj = new Scanner(System.in);  // creating a Scanner object
    System.out.println("Enter a password: "); // prompting user

    String password = myObj.nextLine();  // reading user input
    System.out.println("Password is " + password);  // outputting user input
    
  }
}
Output:
> Enter a password:
User types in "Lebron23"
> Password is Lebron23
Code:
#include <bits/stdc++.h>

using namespace std;

int main() {
    cout << "Enter a password: " << endl; // prompting user
    string password; cin>>password; // reading user input
    cout << "Password is " << password << endl; // outputting user input

}
                                
Output:
> Enter a password:
User types in "Harden13"
> Password is Harden13
If you input into the same variable multiple times, only the last info is kept.
User input works for a variety of data types, including numerical values. The syntax is different for numerical values, but not significantly.
Taking in user input and storing it in variables also allows for the manipulation of input data before producing a final output value. In other words, calculations could be performed on the input data to create a different output value, similar to how a function in math produces an output f(x) from an input x. For example, we could take in a number x, add 10 to x, and then print out x.
Below is an example of prompting the user for their favorite number, taking in the user input, multiplying the number by 2, and then outputting the result.
Note: It is extremely important that the variables you are storing the inputs in are the correct variables or the ones you want. For example, if a user inputs 0.55 when a program expects an integer, it will return an integer(The input is usually rounded to the nearest one). If a user inputs the string "hello!" when the program expects an integer, it will fail entirely as the program expects an integer, and has received something which is not one.
Code:
favNumber = int(input("Enter your favorite number: ")) # prompting user and reading input
favNumber*=2

print("Favorite Number X 2 = " + str(favNumber)) # outputting user input
Output:
> Enter your favorite number:
User types in "30"
> Favorite Number X 2 = 60
Code:
import java.util.Scanner;
public class Main {
  public static void main(String[] args) {
    Scanner myObj = new Scanner(System.in);  // creating a Scanner object
    System.out.println("Enter your favorite number: "); // prompting user

    int favNumber = myObj.nextInt();  // reading user input
    favNumber*=2;
    System.out.println("Favorite Number X 2 = " + favNumber);  // outputting user input
    
  }
}
Output:
> Enter your favorite number:
User types in "23"
> Favorite Number X 2 = 46
Code:
#include <bits/stdc++.h>

using namespace std;

int main() {
  cout << "Enter your favorite number: " << endl; // prompting user
  int favNumber; cin>>favNumber; // reading user input
  favNumber*=2;
  cout << "Favorite Number X 2 = " << favNumber << endl; // outputting user input
  
}
Output:
> Enter your favorite number:
User types in “13”
> Favorite Number X 2 = 26
You can play around with all the code we've used in this article on Replit:
Join our discord server for updates, announcements, future event planning, to ask questions, or just for fun!
Copyright ©2021 Howard County Hour of Code