Today in my C++ tutorial, I will show you the use of endl manipulator.
#include<iostream.h>
#include<conio.h>
void main ()
{
clrscr ();
char name [40];
float m1, m2, m3, av;
cout<<”Enter Your Name: -“;
cin>>name;
cout<<”Enter marks in three subjects: -“;
cin>>m1>.m2>>m3;
av=(m1+m2+m3)/3;
cout<<”Your Name is: -“ << name;
cout<<endl<<”Your average is: -“ << av;
getch ();
}
enld is a manipulator, which causes a linefeed to be inserted into the stream. It has the same effect as sending the single ‘\n’ character. C++ supports both. One point to note that here with cin object we can enter only a single word. To input multiple words in a character array, we have to use the get () function of cin object. get () function takes two arguments, the first is the array variable name and the second is the size of the array. So the above program can be modified as: -
#include<iostream.h>
#include<conio.h>
void main ()
{
clrscr ();
char name [40];
float m1, m2, m3, av;
cout<<”Enter Your Name: -“;
cin>>name;
cout<<”Enter marks in three subjects: -“;
cin>>m1>.m2>>m3;
av=(m1+m2+m3)/3;
cout<<”Your Name is: -“ ;
cout<< name;
cout<<endl<<”Your average is: -“ << av;
getch ();
}
Here we can also use the gets () function of C to enter multiple words. In such case we have to include the stdio.h header file.
#include<iostream.h>
#include<conio.h>
void main ()
{
clrscr ();
char name [40];
float m1, m2, m3, av;
cout<<”Enter Your Name: -“;
cin.get (name, 40);
cout<<”Enter marks in three subjects: -“;
cin>>m1>.m2>>m3;
av=(m1+m2+m3)/3;
cout <<”Your Name is: -“ << name;
cout<<endl<<”Your average is: -“ << av;
getch ();
}
No comments:
Post a Comment