ilteris kaplan blog

Archive of blog posts since 2005

April 5, 2008

Wiki

Text-printing program

#wiki

Text-printing program


 1  // Fig. 2.1: fig02_01.cpp
 2  // Text-printing program.
 3  #include <iostream> // allows program to output data to the screen
 4
 5  // function main begins program execution
 6  int main()
 7  {
 8     std::cout << "Welcome to C++!\n"; // display message
 9
10     return 0; // indicate that program ended successfully
11
12  } // end function main

Line 3 is a preprocessor directive which is a message to the C++ preprocessor. is input/output stream header file. This file must be included for any program that outputs data to the screen or inputs data from the keyboard using C++-style stream input/output.\ Line 8: The notation std::cout specifies that we are using a name, in this case cout, that belongs to “namespace” std. \ Line 8:The << operator is referred to as the stream insertion operator. When this program executes, the value to the right of the operator, the right operand, is inserted in the output stream. Notice that the operator points in the direction of where the data goes. \

Addition program that displays the sum of two integers entered at the keyboard

 
 1  // Fig. 2.5: fig02_05.cpp
 2  // Addition program that displays the sum of two numbers.
 3  #include <iostream> // allows program to perform input and output
 4
     using std::cout; // program uses cout
     using std::cin;  // program uses cin
     using std::endl; // program uses endl
 5  // function main begins program execution
 6  int main()
 7  {
 8     // variable declarations
 9     int number1; // first integer to add  
10     int number2; // second integer to add 
11     int sum; // sum of number1 and number2
12
13     cout << "Enter first integer: "; // prompt user for data
14     cin >> number1; // read first integer from user into number1
15
16     cout << "Enter second integer: "; // prompt user for data
17     cin >> number2; // read second integer from user into number2
18
19     sum = number1 + number2; // add the numbers; store result in sum
20
21     cout << "Sum is " << sum << end1; // display sum; end line
22
23     return 0; // indicate that program ended successfully
24
25  } // end function main

Line 4 are using declarations that eliminate the need to repeat the std:: prefix as we did in earlier programs. Once we insert these using declarations, we can write cout instead of std::cout, cin instead of std::cin and endl instead of std::endl, respectively, in the remainder of the program. \

Line 21 displays the character string Sum is followed by the numerical value of variable sum followed by std::endl a so-called stream manipulator. The name endl is an abbreviation for “end line” and belongs to namespace std. The std::endl stream manipulator outputs a newline, then “flushes the output buffer.” This simply means that, on some systems where outputs accumulate in the machine until there are enough to “make it worthwhile” to display on the screen, std::endl forces any accumulated outputs to be displayed at that moment. This can be important when the outputs are prompting the user for an action, such as entering data. \

Continue Reading

Back to Archive