Vous êtes sur la page 1sur 11

C++ Input-Output Statements

Module 4
Basic C++ I/O: Output statement (cout)
The standard C++ library includes the header file iostream.h,
where the standard input and output stream objects are declared.

By default, the standard output of a program is the screen, and the


C++ stream object defined to access it is cout.

cout is an identifier and is used in conjunction with the insertion


operator, which is written as << (two "less than" signs).

cout must be written in all small letters followed by an insertion


operator << and use semicolon ; to end the statement.

2
C++ Output Statement (cout)

Syntax Example Output


cout<<“constant string”; cout<<“Hello World!”; Hello World!

cout<<“constant string1”; cout<<“Adamson ”; Adamson University


cout<<“constant string2”; cout<<“University”;
cout<<variable; int x = 5; 5
cout<<x;
cout<<variable<<“constant string”; int x = 14; 14 is an integer.
cout<<x<<“is an integer. “;
Basic Input/Output > Output Statement(cout)

Additionally, to add a new-line, you may also use the endl


manipulator or backslash code \n

Syntax Example Output


cout<<“constant1” <<endl; cout << "First." << endl; First.
cout<<“constant2\n”; cout << "Second.\n“; Second.
cout << “Third.”; Third.
cout<<var1<<var2; int a = 10, b = 28; 10
cout<<a<<“\n”<<b; 28
Note: “\n” will create newline.

Introduction to Computer Programming


Basic C++ I/O: Output statement (cout)

int age = 24; int zipcode = 123;

cout << "Hello, I am " << age


cout << " years old and my zipcode is " << zipcode;

Output: ???

Introduction to Computer Programming


Basic C++ I/O: Input statement (cin)
Basic C++ I/O: Input statement (cin)

Introduction to Computer Programming


Basic Input/Output>Input Statement (cin)

The standard input device is usually the keyboard. Handling


the standard input in C++ is done by applying the overloaded
operator of extraction (>>) on the cin stream.
Example: int age;
cin >> age;
The first statement declares a variable of type int called age,
and the second one waits for an input from cin (the keyboard)
in order to store it in this integer variable.
Basic C++ I/O statements
#include<iostream.h> Output:
#include<conio.h>
int main ()
Please enter an integer: 4
{
The value you entered is 4 and its double is 8.
int i;
Thank you.
cout << "Please enter an integer: ";
cin >> i;
cout << "The value you entered is " << i;
cout << " and its double is " << i*2 << ".\n";
cout<<“Thank you.”;
return 0;
}

Introduction to Computer Programming


Basic C++ I/O statements
#include<iostream.h>
#include<conio.h>
int main ()
{ char mystr[20];
cout << "What's your name? ";
cin >> mystr;
cout << "Hello " << mystr << “!\n";
cout << "What is your favorite fruit? ";
cin >> mystr;
cout << "I like " << mystr << " too!\n";
return 0;
}
Introduction to Computer Programming
Basic C++ I/O statements + Operators
#include<iostream.h>
#include<conio.h>
int main ()
{ int x, y, sum;
cout << “Enter 2 integers: ";
cin >> x >> y;
sum = x + y;
cout << x << “ + “<< y <<“ = “ <<sum;
return 0;
}

Introduction to Computer Programming

Vous aimerez peut-être aussi