next up previous
Next: Newline Up: Overview of C++ Previous: Variable Types

Example 1:Convert the Gallons into Liters

Write a program that is capable of converting the gallons into liters. The user should be able to enter the values of liters through the keyboard. Consider for this example that the gallons will be given as integers by the user. The computer should display on the screen the converted values of liters.






/* This program should be saved as prog1.cpp
This program will convert the given gallons (integer)
to liters (real)
The user should be able to enter the number of gallons
through the keyboard and the compute should letters the
output on the screen 1 gallon=3.7854 liters */

#include <iostream.h>

main( )
{
int gallons;
float liters;
cout << "Enter a whole number of gallons: ";
cin >> gallons; // this inputs from the user
liters=3.7854*gallons; // convert to liters
cout << "Liters =" << liters << ;
return 0;
}








Note that the statement :
cout << "Liters=" << liters;
it uses two output operators within the same output statement. Specifically, it outputs the string "Liters=" followed by the value of liters. In general, you can string together as many output operations as you like within one output statement. Just use a separate << for each item.


next up previous
Next: Newline Up: Overview of C++ Previous: Variable Types
Yousef Haik
2/23/1998