Learning Outcomes:: What Is Cout Command For? How To Use Cout Command?
Learning Outcomes:: What Is Cout Command For? How To Use Cout Command?
1
Prepared by Assoc Prof Dr Junita Mohamad Saleh
Console Output Command:
cout
cout is a C++ command referred to as console output
It is used to output or display text or values or data of variables on the
computer screen
It is used with output stream operator <<. Spaces before or after << does
not matter.
E.g. to display my name and age on the monitor screen, my C++ statements
can be like so:
cout << “My name is Junita Mohamad Saleh.”;
cout << “I am “ << “15 years old.”;
The above C++ statements will output the following line:
My name is Junita Mohamad Saleh.I am 15 years old.
Prepared by Assoc Prof Dr Junita Mohamad Saleh 2
Type this and observe the output
#include <iostream>
using namespace std;
int main()
{
cout<<"Learn C++ without fear";
system("PAUSE");
return 0;
}
Used only with cout command to format displayed output as many
times as needed.
E.g. Given C++ statements below:
cout << “My name is Junita Mohamad Saleh.” << endl ;
cout << “I am 15 years old.”;
cout << “\n Notice that one line and one space are skipped”;
The above C++ statements will output the following line:
My name is Junita Mohamad Saleh.
I am 15 years old.
Notice that one line and one space are skipped
#include <iostream>
using namespace std;
int main()
{
printf ("Learn C++ without fear\n“);
system("PAUSE");
return 0;
}
Prepared by Assoc Prof Dr Junita Mohamad Saleh 6
Exercise 1: Question
Write a program that prints your full name on one line. Then use
asterisk (*) to make a display of the first alphabet of your name.
Example display:
JUNITA MOHAMAD SALEH
*******
*
*
*
* *
* *
#include <iostream.h>
Using namespace std;
void main()
{
cout<<“JUNITA MOHAMAD SALEH”<<endl;
cout<<“ ********”<<endl;
cout<<“ *”<<endl;
cout<<“ *”<< “\n”;
cout<<“ *”<<endl;
cout<<“ * *\n”;
cout<<“ * *”<<endl;
}
Prepared by Assoc Prof Dr Junita Mohamad Saleh 8