Here are a set of examples demonstrating how the 'while' instruction can be used to execute a set of instructions a certain number of times. Note the use of a counter in all these examples to keep track of how many times the instructions have been executed. The first example calculates the square footage (area) of five rooms given the length and width of each room.
// A program to calculate the square footage of 5 rooms with the length and
// width entered by a user.
// File: ch2xmpl1.cpp
#include <iostream.h>
void main()
{
// Purpose: to calculate the square footage of 5 rooms with the length and
// width entered by a user.
// Receives: NONE
// Returns: NONE
int count = 0; // initialize the counter to 0
int length;
int width;
int sqFootage;
while (count < 5)
{ count++;
cout << "Please enter the length of a room";
cin >> length;
cout << "Please enter the width of a room";
cin >> width;
sqFootage = length * width;
cout << "The square footage for a room with a length of " << length;
cout << " and a width of " << width << " is: " << sqFootage;
cout << endl << endl << endl; // To skip a few lines between each room
}
cout << "That completes the square footage program.";
}
The next program is very similar except that it calcuates the distance cars have driven given their average speed and the time they were driven. Note that the variables 'time' and 'distance' are declared as doubles to allow for fractions of time.
// A program to calculate the distance driven by six cars with the average
// speed and the total driving time entered by a user.
// Note that this program uses the formula:
// Distance = Speed * Time
// File: ch2xmpl2.cpp
#include <iostream.h>
void main()
{
// Purpose: to calculate the distance driven by six cars.
// Receives: NONE
// Returns: NONE
int count = 0; // initialize the counter to 0
int speed;
double time; // The time is in hours - 3.5 would equal 3 and 1/2 hours
double distance;
while (count < 6)
{ count++;
cout << "Please enter the average speed of a car in miles per hour";
cin >> speed;
cout << "Please enter the time in hours the car was driven";
cin >> time;
distance = speed * time;
cout << "The distance for a car driven at a average speed of " << speed;
cout << " for a time of " << time << " hours is: " << distance;
cout << endl << endl << endl; // To skip a few lines between each room
}
cout << "That completes the distance program.";
}
Either of these programs could be modified to also output a total. Below is the code for the previous
program modified to output the total driven by all the cars. One additional change involves adding
'endl' to the end of each prompt. This makes the prompts look nicer.
// A program to calculate the distance driven by six cars with the average
// speed and the total driving time entered by a user. The total miles driven
// is also calculated.
// Note that this program uses the formula:
// Distance = Speed * Time
// File: ch2xmpl3.cpp
#include <iostream.h>
void main()
{
// Purpose: to calculate the distance driven by six cars and the total distance driven.
// Receives: NONE
// Returns: NONE
int count = 0; // initialize the counter to 0
double totalDistance = 0;
int speed;
double time; // The time is in hours - 3.5 would equal 3 and 1/2 hours
double distance;
while (count < 6)
{ count++;
cout << "Please enter the average speed of a car in miles per hour" << endl;
cin >> speed;
cout << "Please enter the time in hours the car was driven" << endl;
cin >> time;
distance = speed * time;
totalDistance += distance;
cout << "The distance for a car driven at a average speed of " << speed;
cout << " for a time of " << time << " hours is: " << distance;
cout << endl << endl << endl; // To skip a few lines between each room
}
cout << "The total distance driven by all six cars was: " << totalDistance;
}
The final example here shows how one can use the value of the counter in the calculation and, at
least in this example, avoid having any user input at all. This program outputs the squares of all
numbers between 1 and 12.
// A program to calculate and output the squares of all numbers between 1 and 12.
// File: ch2xmpl4.cpp
#include <iostream.h>
void main()
{
// Purpose: to calculate and output the squares of all numbers between
// 1 and 12.
// Receives: NONE
// Returns: NONE
int count = 0;
int square;
while (count < 12)
{ count++;
square = count * count;
cout << "The square of: " << count << " is: " << square << endl;
}
}
Discussion of Simple Iteration in the Story of C++
| |
Chapter Two Example Menu | Next Section |