Different compilers use different suffix
conventions; the most common include .cc, .cxx, .cpp, .cp, and .C.
To compile:
To compile:
$ CC prog1.cc
prog1.exe is generated on Windows, a.out is generated on UNIX
To run:
$prog1 or $.\prog1
prog1.exe is generated on Windows, a.out is generated on UNIX
To run:
$prog1 or $.\prog1
The command used to run the C++ compiler varies across compilers and
operating systems.
$ g++ -o prog1 prog1.cc
Depending on the release of the GNU compiler you are using, you may need to specify -std=c++0x to turn on C++ 11 support.
Typically, we put all the #include directives for a program at the beginning of the source file.
Our output statement uses the << operator twice. Because the operator returns its
left-hand operand, the result of the first operator becomes the left-hand operand of
the second.
$ g++ -o prog1 prog1.cc
Depending on the release of the GNU compiler you are using, you may need to specify -std=c++0x to turn on C++ 11 support.
Typically, we put all the #include directives for a program at the beginning of the source file.
std::cout << "Enter two numbers:" << std::endl;
Equivalent to: (std::cout << "Enter two numbers:") << std::end;
Writing endl has the effect of ending the current line and flushing the buffer associated with that device. Flushing the buffer ensures that all the output the program has generated so far is actually written to the output stream, rather than sitting in memory waiting to be written.
Comments:
Writing endl has the effect of ending the current line and flushing the buffer associated with that device. Flushing the buffer ensures that all the output the program has generated so far is actually written to the output stream, rather than sitting in memory waiting to be written.
Comments:
/* and */
//
Comment Pairs do not nest.
//
Comment Pairs do not nest.
while (std::cin >> value)
Evaluating the while condition executes the expression std::cin >> value
Evaluating the while condition executes the expression std::cin >> value
The input operator (§ 1.2, p. 8) returns its left operand, which in
this case is std::cin. This condition, therefore, tests std::cin.
When we use an istream as a condition, the effect is to test the state of the
stream. If the stream is valid—that is, if the stream hasn’t encountered an error—
then the test succeeds. An istream becomes invalid when we hit end-of-file or
encounter an invalid input, such as reading a value that is not an integer. An
istream that is in an invalid state will cause the condition to yield false.
Entering an End-of-File from the Keyboard
Entering an End-of-File from the Keyboard
When we enter input to a program from the keyboard, different operating
systems use different conventions to allow us to indicate end-of-file. On
Windows systems we enter an end-of-file by typing a control-z—hold down
the Ctrl key and press z—followed by hitting either the Enter or Return
key. On UNIX systems, including on Mac OS X machines, end-of-file is
usually control-d.
Assuming $ is the system prompt and our addition program has been
compiled into an executable file named addItems.exe (or addItems on UNIX
systems), this command will read transactions from a file named infile
and write its output to a file named outfile in the current directory.
Using File Redirection
It can be tedious to repeatedly type these transactions as input to the programs you are testing. Most operating systems support file redirection, which lets us associate a named file with the standard input and the standard output:
$ addItems <infile >outfile
It can be tedious to repeatedly type these transactions as input to the programs you are testing. Most operating systems support file redirection, which lets us associate a named file with the standard input and the standard output:
$ addItems <infile >outfile
Comments
Post a Comment