Simplest program and Data Output Sen Zhang
The simplest program looks like a single person company! void main() { // this starts a comment line // here is where you put your code… } One function, with a head and the empty body
The simplest program looks like a single person company! int main() { // this starts a comment line // here is where you put your code… return 0; } One function, with a head and the empty body
Why main needs to return a value ? DOS command to determine the return code is ERRORLEVEL, most people use the name errorlevel. Errorlevels are not a standard feature of every command. A certain errorlevel may mean anything the programmer wanted it to. Most programmers agree that an errorlevel 0 means the command executed succesfully, and an errorlevel 1 or higher usually spells trouble. But there are many exceptions to this general rule. int main() { return 11.9; } c:\sen\t1222\Debug>echo %errorlevel% 0 c:\sen\t1222\Debug>t1222 c:\sen\t1222\Debug>echo %errorlevel% 11
5 The cout Object The cout object represents the standard output display device, so anything you insert into cout will reflect on the standard output display device. – The display device is usually a video screen – Name derived from Console OUTput and pronounced “see out” Data is passed to cout by the insertion symbol cout << “Hello there, World!”;
6 C++ Sample Code using cout
7 Preprocessor Command Performs an action before the compiler translates source code to machine code – An example is: #include – Causes the iostream file to be inserted wherever the #include command appears iostream is part of the C++ standard library – Included in iostream are two important classes: istream : Declarations and methods for data input ostream : Declarations and methods for data output
8 Namespaces Files accessed by compiler when looking for prewritten classes or functions Sample namespace statement: – using namespace std; – iostream contained in a namespace called std – Compiler uses iostream’s cout object from std whenever cout is referenced
9 More C++ Sample Code
10 More C++ Sample Code (continued)
#include using namespace std; int main() { cout<<"hi!"; cout<<endl; cout<<3+5; cout<<endl; cout<<"3+5"; return 0; }
12 Formatted Output Read book, page 53 to 66 Besides displaying correct information, a program must present results attractively. Field width manipulators: control format of numbers displayed by cout – Manipulators included in the output stream
Output manipulators Output manipulators are items used to manipulate how the output stream of characters is displayed.
Manipulators for columnizng styles setw(n) setfill(‘c’)
When a manipulator requiring an argument is used, the iomanip header file must be included as part of the program. This is accomplished by the preprocessor command #include.
Setw(width) The setw(width) manipulator included in the stream of data passed to cout is used to set the displayed field width. The width in this manipulator should be replaced by a concrete integer number say 10. It will reserves a field with a width of 10 for any subsequent number in the stream.
Setfill(symbol) Set the default fill character to x. (The default fill character is a space) It will use the symbol to fill out the space that has not been used by the data.
Special manipulators for numbers fixed setprecision(n)
fixed Always show a decimal point and use a default of 6 digits after the decimal point. Fill with trailing zeros, if necessary.
setprecision(number) Set the floating-point precision to the places of number. If the fixed manipulator is designated, number specifies the total number of displayed digits after the decimal point; otherwise, it specifies the total number of significant digits displayed (both integer portion and fractional portion)
21 Formatted Output (continued)
22 Formatted Output (continued)
Links to several lists of basic C++ constructs A list of keywords A list of operators, with associativity of each and precedence among them. A list of operators, with associativity of each and precedence among them. A list of io flags and format manipulators A c++ reference website Literals Tokens identifiers 23