1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
|
#include <iostream>
#include <fstream>
using namespace std;
void welcome(void);
void farewell(void);
void displayRectangle(double length, double width, double area, double perim);
void calculateRectangle(double length, double width, double area, double perim);
bool getRectangle(ifstream &inFile, double &length, double &width);
int main()
{
ifstream inFile;
double length, width;
double area, perim;
welcome();
//inFile.open("rectngles.txt"); // <== incorect file name!
inFile.open("rectangles.txt");
if(!inFile)
{
cout << "\a\a~*~ ERROR opening the input file! ~*~\n";
return 1; // or you could use exit(1);
}
while (getRectangle(inFile, length, width))
{
calculateRectangle(length, width, area, perim);
displayRectangle(length, width, area, perim);
}
inFile.close();
farewell();
return 0;
}
/**~*~*
This function displays general information
about the program
*~**/
void welcome(void)
{
cout << "WELCOME to the RECTANGLE calculator!\n\n"
<< "This program will output the\n"
<< "\tperimeter and\n"
<< "\tarea\n"
<< "of several rectangles.\n\n";
return;
}
/**~*~*
This function displays the end-of-the-program
message
*~**/
void farewell(void)
{
cout << "\n\n"
<< "\t ~~*~~ The END ~~*~~ \n\n"
<< "\t ~~*~~ \n"
<< "\t Thank you\n\tfor using my program!\n";
return;
}
/**~*~*
This function calculates
the area and the perimeter of a rectangle
*~**/
void calculateRectangle(double length, double width, double area, double perim)
{
perim = 2 * (length + width);
area = length * width;
return;
}
/**~*~*
This function displays the length and the width of a rectangle
and its area and perimeter
*~**/
void displayRectangle(double length, double width, double area, double perim)
{
cout << "\nRESULTS: ";
cout << "\tLength = " << length;
cout << ", Width = " << width << endl;
cout << "\t\tPerimeter = " << perim << endl;
cout << "\t\t Area = " << area << endl;
return;
}
/**~*~*
This function reads the length and the width of a rectangle
from a file. It returns true in case of success, false otherwise.
*~**/
bool getRectangle(ifstream &inFile, double &length, double &width)
{
return inFile >> length >> width;
}
|