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


Project 1-1 Converting Feet to Meters



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

Project 1-1 Converting Feet to Meters 
Although the preceding sample programs illustrate several important features of the C++ language, they 
are not very useful. You may not know much about C++ at this point, but you can still put what you have 
learned to work to create a practical program. In this project, we will create a program that converts 
feet to meters. The program prompts the user for the number of feet. It then displays that value 
converted into meters.
A meter is equal to approximately 3.28 feet. Thus, we need to use floating-point data. To perform the 
conversion, the program declares two double variables. One will hold the number of feet, and the 
second will hold the conversion to meters.
Step by Step
1.
Create a new C++ file called FtoM.cpp. (Remember, in C++ the name of the file is arbitrary, so you 
can use another name if you like.)
2.
Begin the program with these lines, which explain what the program does, include the iostream 
header, and specify the std namespace.
/*
Project 1-1
This program converts feet to meters.
Call this program FtoM.cpp.
*/


25 
C++ A Beginner’s Guide by Herbert Schildt 
#include
using namespace std;
3.
Begin main( ) by declaring the variables f and m:
int main()
{
double f; // holds the length in feet
double m; // holds the conversion to meters
4.
Add the code that inputs the number of feet:
cout << "Enter the length in feet: ";
cin >> f; // read the number of feet
5.
Add the code that performs the conversion and displays the result:
m = f / 3.28; // convert to meters
cout << f << " feet is " << m << " meters.";
6.
Conclude the program, as shown here:
return 0; }
7.
Your finished program should look like this:
/*
Project 1-1
This program converts feet to meters.
Call this program FtoM.cpp.
*/
#include
using namespace std;
int main()
{
double f; // holds the length in feet
double m; // holds the conversion to meters 
cout << "Enter the length in feet: ";
cin >> f; // read the number of feet
m = f / 3.28; // convert to meters
cout << f << " feet is " << m << " meters.";


26 
C++ A Beginner’s Guide by Herbert Schildt 
return 0;
}
8.
Compile and run the program. Here is a sample run:
Enter the length in feet: 5 5 feet is 1.52439 meters.
9.
Try entering other values. Also, try changing the program so that it converts meters to feet.
1.
What is C++’s keyword for the integer data type? 
2.
What is double? 
3.
How do you output a newline? 
Answer Key: 
1.
The integer data type is int. 
2.
double is the keyword for the double floating-point data type. 
3.
To output a newline, use \n.

Download 11,33 Mb.

Do'stlaringiz bilan baham:
1   ...   15   16   17   18   19   20   21   22   ...   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