Basic input and output operations

First Program in CPP

When learning C++, the first program you write is generally a turning point because it summarizes input, processing, and output. The goal of this pilot program is straightforward: ask the user for a number, then repeat it back. Let us break down this classic C++ program, examining its various parts and features.


Program:

#include<iostream.h>

using namespace std;

int main() {

int a;

cout<<"enter a number"; //input statement

cin>>a;

cout<<"the number is : "<<a; //output statement

return 0;

}

Comments

Our program is accompanied with comments, the voiceless guides explaining its inner workings. Single-line comments are indicated with // and give brief annotations; multi-line comments are enclosed in /* */ and offer more context and clarification.


Single Line Comment (// Double Slash)


// This is an example program to add two numbers 

// C++ program to illustrate the addition of numbers

 

Multi-line Comment (/*     */)


/*This is an example

 program to 

add two numbers */

Output Statement (cout)


  • The cout is a predefined object that represents the standard output stream


Syntax: cout << string or variable;

The operator << is called the insertion or put to operator.

Example:


cout<< “C++ is better than c”;


Causes the string in quotation marks to be displayed on the screen.


Input Statement (cin)


  • The cin is a predefined object that represents the standard input stream


Syntax:
cin >> variable;

The operator >> is called the extraction or get from operator.

 

Example

cin>>a;

 

The value given through the keyboard will be stored in the variable ‘a’.


In the end, my first attempt at writing C++ code captures the essence of input/output activities by encapsulating user interaction in a clear and organized manner. We build relationships with our users by using cout and cin, which encourages interaction and participation. This is evidence of the power and adaptability of C++ as a programming language.

Post a Comment

0 Comments