c++

C++ Basic Input/Output

In C++, cout is used to print the instruction and show it on the console window . cout does the same thing as printf does in C.The proper syntax for cout requires << operator [ cout<<] for displaying the output and in case you forget to do so you will face syntax error.

Similarly the concept of cin is relatable to the scanf used in C language. Cin is used to take the data from the user , the entered data is stored in the memory loaction and the code reacts with respect to that. The proper syntax for cin requires >>operator [ cin>>].


Example 1: String Output

#include 
using namespace std;
int main()
{
// prints the string enclosed in double quotes
cout << "GRATITUDE";
return 0;
}

Output:

GRATITUDE

Working of this program

  1. We first include the header file called a iostream that allows us to display output.
  2. The cout object is defined inside the std namespace. To use the std namespace, we used the using namespace std; statement.
  3. Every C++ program starts with the main() function. The execution of code begins from the start of the main() function while the compilation begins with the first line of the code.
  4. cout is an object that prints the string inside quotation marks " ". It is followed by the << operator
  5. 5. return 0; is used to signal the compiler that we have not faced any problem while writing the code and in case you have faced any problem kindly return 1. The program ends with this statement, however, this statement is not mandatory.
#include <iostream>
int main()
{
// prints the string enclosed in double quotes
    std::cout << "GRATITUDE";
    return 0;
}

Example 2: Numbers and Characters Output

#include <iostream>
using namespace std;
int main()
{
    int number1 = 33;
    double number2 = 129.254;
    char ch = 'G';
    cout << num1 << endl;     // print integer
    cout << num2 << endl;     // print double
    cout << "character: " << ch << endl;     // print char
    return 0;
}

Output:

33
129.254
character: G

Note:

  1. The endl is a manipulator which is used to insert new line by which each output is displayed in new line.
  2. The << operator can be used more than once if we want to print different variables, strings and so on in a single statement.

For example:

cout << "character: " << ch << endl;


Example 3: Integer Input/Output

#include <iostream>
using namespace std;
int main()
{
    int number;
    cout << "Enter integer: ";
    cin >> number; // Taking input
    cout << "digit is: " << number;
    return 0;
}

Output:

Enter integer: 2500
digit is: 2500

In the program, we used

cin >> number;

to take input from the user. The input is stored in the variable number . We use the >> operator with cin to take input.


Example 4: C++ Taking Multiple Inputs

#include <iostream>
using namespace std;
int main()
{
    char p;
    int number;

    cout << "Enter a character and an integer: ";
    cin >> p >> number;

    cout << "Character: " << p << endl;
    cout << "digit: " << number;

    return 0;
}

Output:

Enter a character and an integer: D
64
Character: D
digit: 64