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 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
|
#include <iostream>
#include <string>
#include <iomanip>
#include <istream>
#include <math.h>
#include <fstream>
using namespace std;
//prototypes
double Area_Circle(int&,double&);
int Area_Rectangle(int&,int&,double&);
int Area_Square(int&,double&);
void Menu(int&);
void Input_Side(int&);
void Output_Rectangle(int&,int&,double&);
void Output_Shape(int&,double&);
bool CheckFiles(ofstream&,string);
bool CheckFiles(ifstream&,string);
void OutFileShapes1(ofstream&,int&,int&,double&);
void OutFileShapes2(ofstream&,int&,double&);
int main()
{
char filename[]= "ShapesFile.txt";
int radius, width, length = 0, side, choice;
double area;
char answer;
ofstream shapesFileOut;
ifstream shapesFileIn;
shapesFileOut.open(filename);
shapesFileIn.open(filename);
if(CheckFiles(shapesFileOut,filename) && CheckFiles(shapesFileIn,filename))
{
do
{
Menu(choice);
switch(choice)
{
case 1:
cout << "\nEnter the width: ";
Input_Side(width);
cout << "\nEnter the length: ";
Input_Side(length);
Area_Rectangle(width,length,area);
cout << " \n Rectangle:\t" << "Width\t" << "Length\t" << "Area\n";
Output_Rectangle(width,length,area);
OutFileShapes1(shapesFileOut,width,length,area);
break;
case 2:
cout << "\n Enter the radius: ";
Input_Side(radius);
Area_Circle(radius,area);
cout << " \n Circle:\t" << "Radius\t" << "Area\n";
Output_Shape(radius,area);
OutFileShapes2(shapesFileOut,radius,area);
break;
case 3:
cout << "\n Enter the length of side: ";
Input_Side(side);
Area_Square(side,area);
cout << " \n Square:\t" << "Sides\t" << "Area\n";
Output_Shape(side,area);
OutFileShapes2(shapesFileOut,side,area);
break;
case 4:
cout << " Have a nice day!" << endl << endl;
break;
//validation if non-menu items are entered.
default: cout << "\n That was an invalid choice." << endl << endl;
}
cout << " Would you like to run the program again? (Y or N) ";
cin >> answer;
cout << endl;
// loop to prompt user to run program again
}while(answer == 'Y' || answer == 'y');
}
else
cout << "File open error!" << endl;
cout << " Have a nice day!" << endl << endl;
system("PAUSE");
return EXIT_SUCCESS;
}
void Menu(int &choice)
{
cout << "\n Choose a shape to calculate AREA";
cout << "\n...................................";
cout << "\n 1- Rectangle\n 2- Circle\n 3- Square\n 4- Exit";
cout << "\n\n Enter your choice: ";
cin >> choice;
}
void Input_Side(int &side)
{
cin >> side;
}
double Area_Circle(int &radius, double &area)
{
area = (3.14 * radius * radius);
}
int Area_Rectangle(int &length, int &width, double &area)
{
area = (length * width);
}
int Area_Square(int &side, double &area)
{
area = (side * side);
}
void Output_Rectangle(int &width, int &length, double &area)
{
cout << "\t\t" << width << "\t" << length << "\t" << area;
cout << endl << endl;
}
void Output_Shape(int &side, double &area)
{
cout << "\t\t" << side << "\t" << area;
cout << endl << endl;
}
bool CheckFiles(ofstream& ShapesFile, string filename)
{
ShapesFile.open(filename.c_str(), ios::out);
if (ShapesFile.fail())
return false;
else
return true;
}
bool CheckFiles(ifstream& ShapesFile, string filename)
{
ShapesFile.open(filename.c_str(), ios::out);
if (ShapesFile.fail())
return false;
else
return true;
}
void OutFileShapes1(ofstream &ShapesFile,int &width, int &length, double &area)
{
ShapesFile << setw(8) << width << setw(8) << length << setw(8) << area;
}
void OutFileShapes2(ofstream &ShapesFile, int &side, double &area)
{
ShapesFile << setw(16) << side << setw(8) << area;
}
|