In this c++ tutorial, you will learn about the types of c++ input statements like fixed (static) inputs, how to run c++ program in cmd (command line inputs) and how to take input in c++ called runtime (dynamic) inputs with clear examples.
C++ INPUT STATEMENTS
- Like c language, C++ supports three input statements
- Types
- Assignment Inputs
- Command Line Inputs
- Dynamic Inputs (Runtime Inputs)
1. ASSIGNMENT INPUTS (STATIC INPUTS)
- Assigning the value to the variable with the help of an equal operator is called as assignment inputs.
- We can directly assign the value to variable using equal operator (=)
- Fixed values
Syntax
I. EXAMPLE OF ASSIGNMENT INPUTS
SOURCE CODE
#include <iostream>
using namespace std;
int main()
{
cout<<“\tAssignment Inputs\n”;
cout<<“————————————-\n”;
int id=19; // fixed value
char name[]=”Ganesh”; // fixed value
cout<<“Id=\t : “<<id<<endl;
cout<<“Name=\t : “<<name<<endl;
cout<<“————————————-\n”;
return 0;
}
OUTPUT
2. COMMAND LINE INPUTS (CMD)
- Providing necessary input values (arguments) to the main() method at the time of program execution
- Passing the arguments to main() function by using command prompt or IDE
- In the command line arguments, the main() takes two arguments
- 1st argument:
- It is an integer type variable
- It is used provide (count) the total number arguments passed in the program (including program name)
- Return type : int
- 2nd argument:
- It is 1D char pointer
- It is used to hold the actual arguments which is passed to the main() function
NOTE
- Usually single pointer stores single string (name) and 1D char pointer stores multiple names (multiple strings)
II. EXAMPLE OF COMMAND LINE INPUTS
SOURCE CODE
#include <iostream>
using namespace std;
int main(int c, char* names[])
{
cout<<“\tCommand Line Inputs\n”;
cout<<“————————————-\n”;
cout<<“Total Number Arguments : “<<c<<endl;
cout<<“Cities \n”;
// print the command line arguments value
for(int i=0;i<c;i++)
{
cout<<names[i]<<endl;
}
cout<<“————————————-\n”;
return 0;
}
SET ACTUAL ARGUMENTS (CMD ARGUMENTS) IN CODE BLOCKS IDE
- Single click on the project properties in code blocks IDE.
- Then click the “Set programs’ arguments” option.
Screenshot Example
SET ACTUAL ARGUMENTS (Continue)
- In the Program arguments textbox, just enter the actual arguments one by one.
OUTPUT
3. DYNAMIC INPUTS (RUNTIME INPUTS)
- It is possible to give values to variables interactively through the keyboard at the time of execution (Providing values at the time of runtime)
- Dynamic inputs are done by using the predefined methods like cin>>, cout<<
Streams
- In c++, the input and output (I/O) is achieved with help of stream concept
- Stream is the sequence of bytes (objects)
- Streams are classified as two types like input stream, output stream
Input Streams
- If bytes flow from device to the main() function, then this process is called input
- Ex. Keyboard
Output Streams
- If bytes flow from main() to device, then this process is called output
- Ex. Display screen (Monitor)
PREDEFINED METHODS
1. cin()
- Standard input stream (cin)
- It is a predefined object of istream class
- It is connected with the standard input device (e.g. Keyboard)
- It is used to read a line of text from keyboard
- It is used along with extraction operator (<<) to read input from the keyboard
- It is defined in <iostream> header file
- The “c” in cin refers to character and “in” refers the input, hence cin means character input
- It is similar to scanf() / gets() method in c language
- Return Type: istream
2. cout()
- Standard output stream (cout)
- It is a predefined object of ostream class
- It is connected with the standard output device (e.g. Display screen / Monitor)
- Write data to display screen (monitor)
- It is used along with insertion operator (>>) to write output to display screen (display stream of characters)
- It is defined in <iostream> header file
- The “c” in cout refers to character and “out” refers the output, hence cout means character output
- It is similar to printf() method in c language
- Return Type: ostream
3. endl
- Standard output stream (endl)
- It is a predefined object of ostream class
- It is defined in the <iostream.h> header file
- It is used to insert new line characters and flushes the stream.
III. EXAMPLE OF DYNAMIC INPUTS (RUNTIME INPUTS)
SOURCE CODE
#include <iostream>
using namespace std;
int main()
{
// variable declarations
int id;
char name[50], loc[80];
cout<<“\tDynamic Inputs\n”;
cout<<“————————————-\n”;
cout<<“Enter the person name : “;
cin>>name;
cout<<“Enter the person id : “;
cin>>id;
cout<<“Enter the person location : “;
cin>>loc;
cout<<“\n\t Person Profile\n”;
cout<<“Person Id\t: “<<id<<endl;
cout<<“Person Name\t: “<<name<<endl;
cout<<“Person Location\t: “<<loc<<endl;
cout<<“————————————-\n”;
return 0;
}
OUTPUT
More Tutorials