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 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278
|
#include <iostream>
#include <string>
#include <iomanip> // for set precision
#include <fstream>
using namespace std;
void center(string display); // Functions
void showMenu();
void openBox();
void rainfall();
double sumOfRain(double []);
double averageOfRain(double []);
double highestMonth(double [], int &);
double lowestMonth(double [], int &);
ifstream input;
int main()
{
char choice;
//Constants for the menu choice
cout << fixed << showpoint << setprecision (2); //Set decimal point to two figures (1/100th)
do
{
//Display the menu and get the user's choice.
showMenu();
while (! (cin >> choice) || !(choice == '1' || choice == '2' || choice == '3')) // Validate the menu selection.
//Loop , and the the user input of what choice to do
{
center( "Are you drunk?? thats invalid order.");
cout<< endl;
cin.clear();
fflush(stdin); // Clear Input buffer.
center ("Re-Enter your choice : ");
}
switch (choice) //Switch menu choices
{
case '1':
cout<< endl;
cout << "\n\nAlright Man, lets create an open box ";
cout<< "based off of a rectangle : ";
openBox(); // Calling the box function
break;
case '2':
cout << endl;
center("Rainfall Statistics : ");
int i = 0;
double rainfall[12], total, lowest, average;
string months[12] = {"January","Febuary","March","April","May","June","July","August","September","October","November","December"};
string filename;
i = 0;
cout<<"Enter file name: ";
getline(cin,filename);
ifstream input;
input.open(filename);
if(!input)
{
cout<<"ERROR: File does not exists"<<endl;
}
else
{
while(i<12 && input>>rainfall[i])
i++;
input.close();
total = getTotal(rainfall, 12);
lowest = getLowest(rainfall, 12);
average = total/ (12);
cout << average;
for(i=0; i<12; i++)
{
cout<<left<<setw(10)<<months[i]<<setprecision(2)<<fixed<<setw(5)<<rainfall[i]<<endl;
}
}
break;
case '3':
center ("Programmer: Andrew Holder");
cout << endl;
center ("BYE BYE!!! Press <Enter> key to END the program...");
fflush(stdin);
cin.get();
break;
}
system("CLS"); //Clears screen and then reloops for the entire program
}
while (choice!= '3'); // THis is part of the do while loop that keeps continuing until you pick end program.
cout << endl << endl;
fflush(stdin);
return 0;
}
void showMenu() // Menu function
{
cout << endl;
center("Project Number5");
cout<< endl ;
center("=~=~=~=~=~=~=~=~=~=");
cout<<endl << endl;
center("1 Open Box ");
cout<<endl;
center("2 Rainfall Statisitics");
cout<<endl;
center("3 End program. ");
cout << endl <<endl;
center("Enter Your Choice: ");
}
void openBox() //Function for calculating most amount of volume by cutting box.
{
double length, width;
double volumeMax = 0, volumeTemp, cut, height =0;
cout << "\n\nPlease enter your length of Rectangle: ";
while ( !(cin >> length) || length <= 0 ) // User input of length rectangle (cannot be equal to
// or less than 0.
{
cout << "Error!\n";
cin.clear();
fflush(stdin);
cout << "Enter again:\n";
}
cout << "\nPlease Enter the Width of your Rectangle: "; // User input of Width
while ( !(cin >> width) || width <= 0 )
{
cout << "Error!";
cin.clear();
fflush(stdin);
cout << "Enter again:";
}
for(cut = 0.0; cut <= (width / 2) ; cut += 0.005)
{
volumeTemp = (length - (2 * cut)) * (width - (2*cut)) * (cut);
if(volumeTemp > volumeMax)
{
volumeMax = volumeTemp;
height = cut;
}
}
cout << "Here are your box's properties";
cout << "\nYour Box's Length: " << length;
cout << "\nYour Box's Width: " << width;
cout << "\nThe size of your Cut: " << height;
cout << "\nBox's Max Volume: " << volumeMax;
fflush(stdin);
cin.get();
}
void rainfall()
{
fflush(stdin);
double avg = 0;
double sum = 0;
int i = 0;
double rainfall[12];
string months[12] = {"January","Febuary","March","April","May","June","July","August","September","October","November","December"};
string filename;
avg = 0;
sum = 0;
i = 0;
cout<<"Enter file name: ";
getline(cin,filename);
ifstream input;
input.open(filename);
if(!input)
{
cout<<"ERROR: File does not exists"<<endl;
}
else
{
cout<<"MONTH RAINFALL"<<endl;
while(input>>rainfall[i])
cout<<left<<setw(10)<<months[i]<<setprecision(2)<<fixed<<setw(5)<<rainfall[i]<<endl;
sum = sum + rainfall[i];
i++;
input.close();
//calculate average
avg = sum / 12;
//calculate highest and lowest rainfall
double highest, lowest;
highest=rainfall[0];
lowest=rainfall[0];
for(i=1;i<12;i++)
{
if(highest<rainfall[i])
highest=rainfall[i];
if(lowest>rainfall[i])
lowest=rainfall[i];
}
cout<<endl<<endl;
//display result
cout<<"Total Rainfall : "<<sum<<endl;
cout<<"Average Rainfall : "<<avg<<endl;
cout<<"Highest Rainfall: "<<highest<<endl;
cout<<"Lowest Rainfall: "<<lowest<<endl;
}
cout<<endl<<"Press enter key to continue<Enter> ";
cin.get();
}
double getTotal (const double array[], double size)
{
double total = 0; //Accumulator
for (int i =0; i < size; i++)
total += array[i];
return total;
}
double getLowest (const double array[], double size)
{
double lowest;
lowest = array[0];
for (int i = 0; i< size, i++)
{
if(array[i]< lowest)
lowest = array[i];
}
return lowest;
}
void center(string display) // Formula for setting text center
{
int number = ((80 - display.length()))/ 2;
for (int i=1; i<=number; i++) //for loop to establish center text
cout << " ";
cout << display;
}
|