Documente Academic
Documente Profesional
Documente Cultură
Java
int main () {
unsigned int x;
cout << Enter a number: ;
cin >> x;
cout << You entered << x << endl;
system(PAUSE);// compiler-specific
return 0;
}
Notes on example
Entire program contained in main function no
enclosing class
Function heading for main:
Return type is typically int, not void
No other modifiers (e.g. public, static)
No parameters (this may vary)
Return statement required
Function main has return type of int
0 is traditional return value signal to operating system
(UNIX) that program ended normally
Preprocessor directives
The first line of code in the example program:
#include <iostream>
isnt, technically, a C++ instruction; it is a preprocessor
directive
The closest analog in Java is an import statement
You can use as many or as few of these as you
need, in any order.
They should appear at the beginning of the file,
before any C++ statements
Input/Output in C++
Streaming I/O; a standard library (iostream)
provides objects similar to Javas System.out and
System.in
2 output stream objects defined in iostream
library:
cout: standard output stream (screen)
cerr: also screen by default
1 input stream object: cin, represents standard
input (keyboard)
Input/output in C++
I/O operations are performed by operators (not
methods or functions, per se)
Insertion operator (<<) does output
Extraction operator (>>) does input
Direction of arrows indicates data destination:
<< inserts data on to output stream (cout)
>> extracts data from input stream (cin) into a variable
Console output
The object cout (from iostream) represents the
standard output stream (screen)
The insertion operator is used to insert expressions
onto the screen
Can be as few as one or as many as ??? expressions
Each separate expression gets its own insertion operator
Values of expressions appear on screen in the order of
appearance in source code, from left to right
<< is a binary operator
<< is called the output or insertion operator
STATEMENT
cout << You are << age << years old\n ;
Manipulators
Manipulators are expressions that affect the
state of an output stream
endl, fixed, showpoint, setw, and setprecision
are manipulators that can be used to control
output format
endl is use to terminate the current output line,
and create blank lines in output same effect
as the \n character
Using Manipulators
fixed and showpoint
Use the following statement to specify that (for output
sent to the cout stream) decimal format (not scientific
notation) be used
cout.setf(ios::fixed);
Use the statement below to specify that a decimal
point be included (even for floating values with 0 as
fractional part)
cout.setf(ios::showpoint);
Together, these two statements ensure that output of
floating-point numbers will be uniform for the
duration of the program (or until the settings are
changed by a subsequent statement or statements)
setprecision(n)
requires #include <iomanip.h> and appears in an
expression using insertion operator (<<)
int main ( )
{
float myNumber = 123.4587 ;
system(PAUSE);
return 0 ;
}
Manipulator setw
int main ( )
{
int myNumber = 123 ;
int yourNumber = 5 ;
system(PAUSE);
return 0 ;
}
What is exact output?
#include <iomanip.h> // for setw( ) and setprecision( )
#include <iostream.h>
#include <stdlib.h>
int main ( )
{
float myNumber = 123.4 ;
float yourNumber = 3.14159 ;
system(PAUSE);
return 0 ;
}
312.0 More Examples 4.827
x y
float x = 312.0 ;
float y = 4.827 ;
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
int main()
{
char c;
cout << Enter a character: ;
cin.get(c); // reads the character entered
cout << You entered: << c << endl;
system(PAUSE);
return 0;
}
Weakness of get()
In the previous example, a single character
is read and echoed to the screen
The next example attempts to do the same
thing, only with two characters, but it
doesnt work
Take a look at the code what is output?
Example
#include <iostream.h>
#include <stdlib.h>
int main()
{
char c, d;
cout << "Enter a character: ";
cin.get(c); // reads the character entered
cout << "Enter another character: ";
cin.get(d);
cout << "You entered: " << c << " and " << d << endl;
system("PAUSE");
return 0;
}
At keyboard you type:
A[space]B[space]C[Enter]
char first ;
char middle ;
char last ; first middle last
cin.get ( first ) ; A B
cin.get ( middle ) ;
cin.get ( last ) ; first middle last
int main()
{
char c, d;
cout << "Enter a character: ";
cin.get(c); // reads the character entered
cin.ignore(); // throws away the newline character
cout << "Enter another character: ";
cin.get(d);
cin.ignore();
cout << "You entered: " << c << " and " << d << endl;
system("PAUSE");
return 0;
}
Other forms of ignore()
You can also choose to call ignore() with
one or two argument(s); using this form,
you can specify how many characters to
ignore, and/or the last character in a series
of characters to ignore
The example on the next slide illustrates
these two forms
Example
char c, d, e;
cout << "Enter a character: ";
cin.get(c); // reads the character entered
cin.ignore(1); // discards the newline character
cout << "Enter another character: ";
cin.get(d);
cin.ignore(100, '\n'); // discards next 100 characters or
// next newline, whichever comes first
cout << "Enter one more character: ";
cin.get(e);
cin.ignore(); // discards newline
cout << "You entered: " << c << ", " << d << " and " << e << endl;
String Input in C++
Input of a string is possible using the extraction
operator >>.
EXAMPLE
string message ;
cin >> message ;
cout << message ;
HOWEVER . . .
Extraction operator >>
When using the extraction operator ( >> ) to
read input characters into a string variable:
the >> operator skips any leading whitespace
characters such as blanks and newlines
it then reads successive characters into the
string, and stops at the first trailing whitespace
character (which is not consumed, but remains
waiting in the input stream)
Example
string name;
cout << Enter your name: ;
cin >> name;
cout << You entered: << name << endl;
Output:
EXAMPLE
string message ;
getline (cin, message ) ;
getline(istream, string)
getline does not skip leading whitespace
characters such as blanks and newlines
getline reads successive characters (including
blanks) into the string, and stops when it
reaches the newline character \n
the newline is consumed by getline, but is not
stored into the string variable
Example
string name;
cout << Enter your name: ;
getline(cin, name);
cout << You entered: << name << endl;
Output:
#include <fstream.h>