Friday, August 9, 2019

2. Print multiple lines

Following program prints multiple lines to the screen.

#include <iostream>
using namespace std;

int main(int argv, char** argc)
{
    cout<<"This is the first line.";  //no endl has used
    cout<<"This is the second line."<<endl;  // endl has been used
    cout<<"Third line is here";
    return 0;
}

The statement endl, which represent end of linemove the next output to next consecutive output line.

The endl term can be replace with "\n" as follows.

#include <iostream>
using namespace std;

int main(int argv, char** argc)
{
    cout<<"This is the first line.";  
    cout<<"This is the second line.\n";  
    cout<<"Third line is here";
    return 0;
}
The \n can be also placed at the beginning of the string as well.


The endl term and "\n" can be used in the same program.
#include <iostream>
using namespace std;

int main(int argv, char** argc)
{
    cout<<"This is the first line."<<endl;  
    cout<<"This is the second line.";  
    cout<<"\nThird line is here.\n";
    cout<<"Fourth line."<<endl;
    cout<<"Fifth line"<<endl;
    return 0;

}


Following program has single output statement but the outputs are appearing in multiple lines.

#include <iostream>
using namespace std;

int main(int argv, char** argc)
{
    cout<<"First\nSecond\nThird\nFourth\nFifth";
    return 0;
}



No comments:

Post a Comment