正在加载图片...
// Fig. 18.1: fig18_01. cpp 7 // Addition program that displays the sum of two numbers Outline 3 #include <iostream> / allows program to perform input and output Include the contents 5 int maino of the iostream 6{ 7 int number1; / first integer to add fig18_01. cpp Declare integer variables 10 std: cin > number1;//read first integer from-user into number1 Use stream extraction 12 int number2; / second integer to add operator with standard input 13 int sum;// sum of number1 and numbe stream to obtain user input 15 std: cout << Ent ond integer::// prompt user for data 16 std: : cin >s number2; / read second integer from user into number2 17 sum= number 1 number 2:// add the numbers: store result in sum 18 std: cout < sum is<< sum <<std::end i/ display sum; end line 19 Stream manipulator 20 return 0;// indicate that program ended successfully std: endl outputs a 213// end function main newline, then"flushes output Enter first integer: 45 buffer Enter second integer: 72 sum is 117 Concatenating, chaining or cascading stream insertion operations C 2007 Pearson Education Inc. All rights reserved© 2007 Pearson Education, Inc. All rights reserved. 7 1 // Fig. 18.1: fig18_01.cpp 2 // Addition program that displays the sum of two numbers. 3 #include <iostream> // allows program to perform input and output 4 5 int main() 6 { 7 int number1; // first integer to add 8 9 std::cout << "Enter first integer: "; // prompt user for data 10 std::cin >> number1; // read first integer from user into number1 11 12 int number2; // second integer to add 13 int sum; // sum of number1 and number2 14 15 std::cout << "Enter second integer: "; // prompt user for data 16 std::cin >> number2; // read second integer from user into number2 17 sum = number1 + number2; // add the numbers; store result in sum 18 std::cout << "Sum is " << sum << std::endl; // display sum; end line 19 20 return 0; // indicate that program ended successfully 21 } // end function main Enter first integer: 45 Enter second integer: 72 Sum is 117 Outline fig18_01.cpp Declare integer variables Use stream extraction operator with standard input stream to obtain user input Stream manipulator std::endl outputs a newline, then “flushes output buffer” Concatenating, chaining or cascading stream insertion operations Include the contents of the iostream
<<向上翻页向下翻页>>
©2008-现在 cucdc.com 高等教育资讯网 版权所有