meljun cortes standard c++ input output

11
The Standard C++ Input/Output Commands MELJUN CORTES MELJUN CORTES

Upload: meljun-cortes

Post on 09-Aug-2015

128 views

Category:

Technology


0 download

TRANSCRIPT

Page 1: MELJUN CORTES Standard C++ Input Output

The Standard C++ Input/Output Commands

MELJUN CORTESMELJUN CORTES

Page 2: MELJUN CORTES Standard C++ Input Output

The Console Output Library Command

There are several library commands used for output of data, but we’ll learn to use first the cout statement.

The syntax is: 

cout [<< format string] [<< argument list] Where:

– cout refers to the console output library command– << refers to the standard output stream operator (SOSO)– the format string can be an ordinary character, string, or an escape

character– the argument list can be a simple variable, constant, or an expression.

Page 3: MELJUN CORTES Standard C++ Input Output

Example#include <iostream>

using namespace std; void main(void){

char ch;int i;float f;double d;

 // initialize variables

ch = 'Z';i = 70;f = 0.123456789;d = 0.123456789123456789;

 

Page 4: MELJUN CORTES Standard C++ Input Output

Example (continued…)cout << "Hello\n"; // prints a stringcout << 'A' << "\n" ; // prints a char constant cout << ch << "\n"; // prints the value of a char variable cout << 567 << "\n"; // prints an integer constant

cout << i << "\n"; // prints the value of integer variable cout << 1.25 << "\n"; // prints a float constantcout << f << "\n"; // prints the value of float variablecout << 3.1416 << "\n"; // prints a double constant cout << d << "\n"; // prints a value of a double variable cout << "ch = " << ch << "\n"; // using different parameterscout << "i = " << i << " f = " << f << " d= " << d << "\n";

}

Page 5: MELJUN CORTES Standard C++ Input Output

The Console Input Library Command

Like the output library commands, there are also several input library commands, but we’ll learn to use first the cin statement.

The syntax is:cin [>> argument list]

 Here, the argument list can only be variables, which

will hold the data input of the user through the use of the keyboard.

Page 6: MELJUN CORTES Standard C++ Input Output

Example#include <iostream>

using namespace std;

void main(void)

{

char ch;

int i;

float f;

double d;

Page 7: MELJUN CORTES Standard C++ Input Output

Example (continued…)cin >> ch; // expects a character input from usercin >> i; // expects an integer input from usercin >> f; // expects a float input from usercin >> d; // expects a double input from user

 // outputs the input valuescout << “ch = ” << ch;

cout << “ i = ” << i << “ f = ” << f ; cout << “ d = ” << d << “\n”;}

Page 8: MELJUN CORTES Standard C++ Input Output

cin Examples with Prompts

#include <iostream>

using namespace std; void main(void){

char ch;int i;float f;double d;

 cout << "Input value of char variable ch: ";cin >> ch;

Page 9: MELJUN CORTES Standard C++ Input Output

cin Examples with Prompts (continued…)

cout << "Input value of int variable i: ";cin >> i;cout << "Input value of float variable f: ";cin >> f;cout << "Input value of double variable d: ";cin >> d;

 // outputs the input values

cout << "ch = " << ch << " i = " << i; cout << " f = " << f << " d = " << d << "\n";}

Page 10: MELJUN CORTES Standard C++ Input Output

Special Formatting Characters • The ‘\n’ character is just one of the special formatting

characters of C++, it is referred to as the new line character. • If used alone, the character must be enclosed in single quotes

or in double quotes. • It can also be used in conjunction with other characters in a

format string enclosed in double quotes. • There is, however, an alternative to the ‘\n’ character that is

more readable. • Users can use the endl in place of ‘\n’, read as end line. • But unlike the ‘\n’ character, endl can not be used as part of a

string, i.e enclosing it in double quotes – or even single quotes. • Doing so will cause unexpected results or a logical error.

Page 11: MELJUN CORTES Standard C++ Input Output

Common Formatting Characters