C++: a beginner's Guide, Second Edition


CRITICAL SKILL 1.7: Reading Input from the Keyboard



Download 11,33 Mb.
Pdf ko'rish
bet16/194
Sana12.03.2022
Hajmi11,33 Mb.
#491693
1   ...   12   13   14   15   16   17   18   19   ...   194
Bog'liq
C A Beginner\'s Guide 2nd Edition (2003)

CRITICAL SKILL 1.7: Reading Input from the Keyboard 
 
The preceding examples have operated on data explicitly specified in the program. For example, the 
area program just shown computes the area of a rectangle that is 7 by 5, and these dimensions are part 
of the program itself. Of course, the calculation of a rectangle’s area is the same no matter what its size
so the program would be much more useful if it would prompt the user for the dimensions of the 
rectangle, allowing the user to enter them using the keyboard.
To enable the user to enter data into a program from the keyboard, you will use the >> operator. This is 
the C++ input operator. To read from the keyboard, use this general form
cin >> var;
Here, cin is another predefined identifier. It stands for console input and is automatically supplied by 
C++. By default, cin is linked to the keyboard, although it can be redirected to other devices. The variable 
that receives input is specified by var.
Here is the area program rewritten to allow the user to enter the dimensions of the rectangle:


20 
C++ A Beginner’s Guide by Herbert Schildt 
Here is a sample run:
Enter the length: 8
Enter the width: 3
The area is 24
Pay special attention to these lines:
cout << "Enter the length: ";
cin >> length; // input the length
The cout statement prompts the user. The cin statement reads the user’s response, storing the value in 
length. Thus, the value entered by the user (which must be an integer in this case) is put into the 
variable that is on the right side of the >> (in this case, length). Thus, after the cin statement executes
length will contain the rectangle’s length. (If the user enters a nonnumeric response, length will be 
zero.) The statements that prompt and read the width work in the same way.

Download 11,33 Mb.

Do'stlaringiz bilan baham:
1   ...   12   13   14   15   16   17   18   19   ...   194




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2024
ma'muriyatiga murojaat qiling

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish