C# User Input

Get User Input

You have already learned that Console.WriteLine() is used to output (print) values. Now we will use
Console.ReadLine()
to get user input.

In the following example, the user can input his or hers username, which is stored in the variable userName. Then we print the value of
userName
:

Example

// Type your username and press enter
Console.WriteLine("Enter username:");

// Create a string variable and get user input from the keyboard and store it in the variable
string userName = Console.ReadLine();

// Print the value of the variable (userName), which will display the input value
Console.WriteLine("Username is: " + userName);

Continue reading C# User Input

C++ User Input

C++ User Input

You have already learned that cout is used to output (print) values. Now we will use cin to get user input.

cin is a predefined variable that reads data from the keyboard with the extraction operator (>>).

In the following example, the user can input a number, which is stored in the variable x. Then we print the value of x: Continue reading C++ User Input

C User Input

User Input

You have already learned that printf() is used to output values in C.

To get user input, you can use the scanf() function:

Example

Output a number entered by the user :

// Create an integer variable that will store the number we get from the user
int myNum;

// Ask the user to type a number
printf("Type a number: \n");

// Get and save the number the user types
scanf("%d", &myNum);

// Output the number the user typed
printf("Your number is: %d", myNum);

The scanf() function takes two arguments: the format specifier of the variable (%d in the example above) and the reference operator (&myNum), which stores the memory address of the variable.

 

Continue reading C User Input