0% found this document useful (0 votes)
45 views9 pages

A Taste of C++: 7.1 The Function 7.2 The and Streams 7.3 Comments 7.4 References

Uploaded by

KMC Nisaan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
45 views9 pages

A Taste of C++: 7.1 The Function 7.2 The and Streams 7.3 Comments 7.4 References

Uploaded by

KMC Nisaan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

© Christian Jacob

Chapter 7

A Taste of C++

7.1 The Function main()


7.2 The cout and cin Streams
7.3 Comments
7.4 References

Chapter Overview
Page 2 Chapter 7: A Taste of C++ © Christian Jacob

7.1 The Function main()

#include <iostream.h>

void main()
{
int pounds;
float kilos;

cout << “How many pounds do you weigh?”;


cin >> pounds;

kilos = pounds / 2.2;

cout << “You weigh “ << kilos;


cout << “ kilos” << endl;
}

First Back TOC Prev Next Last


Page 3 Chapter 7: A Taste of C++ © Christian Jacob

Top-level Diagram for Simple C++ Programs and Directives

directive

simple C++ simple


program main function

directive # include

< “

system file name local file name

> “

First Back TOC Prev Next Last


Page 4 Chapter 7: A Taste of C++ © Christian Jacob

Syntax Diagram for Simple main() Function

simple void main compound


main function () statement

compound
statement { }

declaration statement

statement expression ;

First Back TOC Prev Next Last


Page 5 Chapter 7: A Taste of C++ © Christian Jacob

Syntax Diagram for Type Declarations

declaration

type identifier ;

const = expression

type

int float double char other


types

First Back TOC Prev Next Last


Page 6 Chapter 7: A Taste of C++ © Christian Jacob

7.2 The cout and cin Streams

Streams

Hello, Universe!
Hello, Universe!

cout

cin

C++ program:

<< “Hello, Universe!” << endl;


>> pounds;

First Back TOC The cout and cin Streams Prev Next Last
Page 7 Chapter 7: A Taste of C++ © Christian Jacob

The objects cout and cin are streams, structures that hold data
temporarily.

Cout and cin are declared in the header file iostream.h.


• cout is an object of type ostream.
- It acts as a buffer or holding place, that accepts output from a
program and eventually causes it to appear on the screen.
- The insertion operator, <<, puts data into the output buffer.
• cin is an object of type istream.
- It acts as a buffer that retrieves data from the keyboard and
provides it, piece by piece, to the program.
- The extraction operator, >>, gets data from an input stream.
• The operators << and >> are defined to cascade:

cin >> pounds >> kilos;


cout << “Weight: ”<<kilos<<“ kilos”<<endl;

First Back TOC The cout and cin Streams Prev Next Last
Page 8 Chapter 7: A Taste of C++ © Christian Jacob

7.3 Comments

/*----------- poundsToKilos.cc -----------------------


Converts pounds, entered as a whole number,
to kilograms.
Programmer: Christian Jacob, 10/05/2000
----------------------------------------------------*/
#include <iostream.h>

void main()
{
int pounds; // weight in pounds (user input)
float kilos; // metric weight -- real number (output)

cout << “How many pounds do you weigh?”; // on screen


cin >> pounds; // input from keyboard

kilos = pounds / 2.2; // pounds to kilos conversion

cout << “You weigh “ << kilos; // screen output


cout << “ kilos” << endl;
} // end of main()

First Back TOC Comments Prev Next Last


Page 9 Chapter 7: A Taste of C++ © Christian Jacob

7.4 References

• G. Blank and R. Barnes, The Universal Machine, Boston, MA: WCB/


McGraw-Hill, 1998. Chapters 3.1 and 3.2.3.

First Back TOC References Prev Next Last

You might also like