How to Read a Single Integer Value From the User
When designing a program you may need to add code to obtain a single integer value from a user. This value can then be stored and used in the program in many ways. The integer value could trigger another portion of code to execute or it could be the input to a function that will execute later. Each programming language requires different code to obtain an integer from the keyboard. One language, C++, can obtain a single integer value from the user easily.
Instructions
-
-
1
Enable the input/output stream. In C++, type “#include <iostream>” at the top of the program. This will allow you to obtain information from the keyboard and output information to the screen.
-
2
Declare the integer. The program needs to know that the integer exists before it can be obtained from the user. Declare the integer in C++ with the statement “int x;” (int in C++ stands for integer). Place the declaration at the top of the section that will use this integer.
-
-
3
Add code to ask the user for the integer value. In C++, type “cout >> “Enter the integer value.”;.”
-
4
Type the code to obtain the value from the keyboard input. In C++, type “cin << x;.”
-
5
Finish adding any other code and compile.
-
1