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
|
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
using namespace std;
//Function protypes
void print_no_rain(string no_rain[], int ray_size, ofstream & fout);
float avg(float total, int ray_size);
void begin(string & username, string &filename1, string & filename2, ifstream & fin, ofstream & fout);
void display1(string cityname, string username, string month_low, string month_high, float average, float lowest, float highest,string no_rain[],int ray_size, ofstream & fout);
void graph_display(int ray_size, string months[], float rainfall[], ofstream & fout);
int main()
{
const int ray_size = 12;
ifstream fin;
ofstream fout;
string filename1, filename2, username, cityname;
float rainfall[ray_size];
float average = 0;
float total = 0;
float highest;
float lowest;
string months[12] = { "January" , "February", "March", "April", "May","June", "July", "August", "September", "October", "Nomvember", "December" };
string month_high;
string month_low;
string no_rain[12];
// Collect the username and file names from the user
begin(username, filename1,filename2,fin,fout);
getline(fin, cityname);
highest = rainfall[0];
lowest = rainfall[0];
for (int i = 0; i < ray_size; i ++)
{
fin >> rainfall[i];
total += rainfall[i];
if (rainfall[i] > highest)
{
highest = rainfall[i];
month_high = months[i];
}
if (rainfall[i] < lowest)
{
lowest = rainfall[i];
month_low = months[i];
}
if (rainfall[i] == 0)
{
no_rain[i] = months[i];
}
}
average = avg(total, ray_size);
display1(cityname, username, month_low, month_high, average, lowest, highest, no_rain, ray_size, fout);
graph_display(ray_size, months, rainfall, fout);
return 0;
}
void print_no_rain(string no_rain[], int ray_size, ofstream & fout)
{
for (int x = 0; x < ray_size; x++)
{
cout << no_rain[x];
fout << no_rain[x];
}
}
// func calculate the average
float avg(float total, int ray_size)
{
float average;
average = total / ray_size;
return average;
}
// func collects user name and file names, also opens/creates files
void begin(string &username, string &filename1, string &filename2, ifstream &fin, ofstream &fout )
{
cout << "Enter your name:";
getline(cin, username);
cout << "Enter the input file name:";
getline(cin, filename1);
cout << "Enter the output file name:";
getline(cin, filename2);
fin.open(filename1.c_str());
fout.open(filename2.c_str());
cout << " " << endl;
}
//func displays data
void display1(string cityname, string username, string month_low, string month_high, float average, float lowest, float highest, string no_rain[], int ray_size, ofstream & fout)
{
cout << "The rainfall report for " << cityname << "prepared by " << username << ":" << endl;
fout << "The rainfall report for " << cityname << "prepared by " << username << ":" << endl;
cout << setw(35) << left << fixed << setprecision(2) << "Year's Average Rainfall:" << average << " Inches" << endl;
fout << setw(35) << left << fixed << setprecision(2) << "Year's Average Rainfall:" << average << " Inches" << endl;
cout << setw(34) << "Month with Lowest Rainfall:" << month_low << " Rainfall:" << lowest << " Inches" << endl;
fout << setw(34) << "Month with Lowest Rainfall:" << month_low << " Rainfall:" << lowest << " Inches" << endl;
cout << setw(35) << "Months with no Rainfall:";
fout << setw(35) << "Months with no Rainfall:";
print_no_rain(no_rain,ray_size,fout);
cout << setw(35) << "\nMonth with Highest Rainfall: " << month_high << " Rainfall:" << highest << " Inches" << endl;
fout << setw(35) << "\nMonth with Highest Rainfall: " << month_high << " Rainfall:" << highest << " Inches" << endl;
cout << "\n\n";
fout << "\n\n";
}
//func to display the graph
void graph_display(int ray_size, string months[], float rainfall[], ofstream & fout)
{
fout << "------------------------------------------------------------------------------" << endl;
cout << "------------------------------------------------------------------------------" << endl;
for (int i = 0; i < ray_size; i++)
{
fout << setw(10) << months[i] << "|";
cout << setw(10) << months[i] << "|";
for (int y = 0; y < rainfall[i]; y++)
{
cout << " * ";
fout << " * ";
}
cout << " " << endl;
fout << " " << endl;
}
cout << "------------------------------------------------------------------------------" << endl;
cout << setw(11) << "Inches" << " 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20" << endl;
fout << "------------------------------------------------------------------------------" << endl;
fout << setw(11) << "Inches" << " 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20" << endl;
}
|