/*
ID Block:
Author: Sarah Choi
Date: 03/05/2015
Project: Carbon Footprint Assignment 5 Part 1
Complier: Visual Studio 2013
Operating System: Windows 7
*/
/*
Problem: Program will ask user to input file name.
Program must open the file for each city to receive the average carbon footprint for the city.
Program will then compute the fine that the city must pay according to their average carbon footprint.
IOAA Document
Part 1: Data Input
Item Variable Data Type Remarks
Data File Name DataFile string input
dataFile flagdataFile flag true or false
OutputName outputFile string output
City Name cityName string
Carbon Numbers oneCarbon int
Counter numTests int
Fine fine double
Average Average Carbon double
Rounded Average roundedAverage double double rounded
Part 2: Data Ouput
1. Greet User
2. Ask User to input file name
3. Ask User to input the output file name
4. Output City Name, Rounded Carbon Average, and Fine corresponding to the data input.
5. If Error, output corresponding error message only.
Part 3: Analysis
declare oneCarbon from input file
sum += oneCarbon
averageCarbon = static_cast<double>(sum) / numTests;
roundedAvgCarbon = round(averageCarbon);
Part 4: Algorithm
1. Input all of the required #include and using namespace std.
2. Greet the user and explain what the program is going to do.
3. Ask the user to input file name.
4. Check the file to make sure that it is not empty or have any negative values.
If input is invalid, print "Invalid Input."
If input is valid, compute the fine according to the data.
5. Ouput Closing Statement "THank you for using El Camino's Carbon Footprint Program"
*/
#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
#include <cmath>
using namespace std;
int main()
{
string DataFile;
bool flagDataFile = true;
cout << "Hello, Welcome to Sarah's Carbon Footprint Program! :)" << endl
<< "We will calculate your city's Average Carbon Footprint" << endl
<< " and provide the Corresponding Fine." << endl << endl;
cout << "Please enter the full path of the input data file name.";
getline(cin, DataFile); //Read whole line
ifstream in(DataFile); //Store the path of the file
if (!in.is_open())
{
cout << "Failed to open Input File. Invalid File." << endl;
}
else if (in.peek() == EOF)
{
cout << "File is Empty." << endl;
}
else
{
flagDataFile = true;
}
string outfileName;
cout << "Enter full path to output file: ";
getline(cin, outfileName);
ofstream out(outfileName, ios::app);
if (!out.is_open())
{
cout << "Failed to open output file." << endl;
exit(0);
}
do{
string cityName;
in >> cityName;
int oneCarbon;
int sum = 0;
int numTests = 0;
in >> oneCarbon;
while (oneCarbon >= 0)
{
sum += oneCarbon;
numTests++;
in >> oneCarbon;
}
if (numTests > 0)
{
double averageCarbon = static_cast<double>(sum) / numTests;
int roundedAvgCarbon = round(averageCarbon);
double fine;
if (roundedAvgCarbon >= 0 && roundedAvgCarbon <= 1)
{
fine = 0.00;
}
else if (roundedAvgCarbon > 1 && roundedAvgCarbon <= 3)
{
fine = 1000000.00;
}
else if (roundedAvgCarbon > 3 && roundedAvgCarbon <= 5)
{
fine = 2000000.00;
}
else if (roundedAvgCarbon > 5 && roundedAvgCarbon <= 7)
{
fine = 3000000.00;
}
else if (roundedAvgCarbon > 7)
{
fine = 4500000.00;
}
cout << endl;
cout << setfill('*') << setw(50) << "" << left << fixed << endl;
cout << setfill(' ') << setw(10) << "City" << setw(30) << "Rounded Average Carbon FP"
<< setw(10) << "Fine($)" << endl;
cout << setfill(' ') << setw(10) << cityName << setw(30) << roundedAvgCarbon
<< setw(10) << setprecision(2) << fine << endl;
cout << setfill('*') << setw(50) << "" << left << endl << endl;
out << endl;
out << setfill('*') << setw(50) << "" << left << fixed << endl;
out << setfill(' ') << setw(10) << "City" << setw(30) << "Rounded Average Carbon FP"
<< setw(10) << "Fine($)" << endl;
out << setfill(' ') << setw(10) << cityName << setw(30) << roundedAvgCarbon
<< setw(10) << setprecision(2) << fine << endl;
out << setfill('*') << setw(50) << "" << left << endl << endl;
}
else
{
cout << "The City has no Valid Carbon Footprint. Please try again." << endl;
out << "The City has no Valid Carbon Footprint. Please try again." << endl;
}
} while (in.peek() != EOF);
in.close();
out.close();
system("pause");
return 0;
}