sf411
2011-10-03 15:28:39
Can anyone figure out a more efficient way to do the, no output file statement, to print to console even though the output file does not exist.
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
using namespace std;
int main(){
cout<<fixed<<showpoint<<setprecision(2);
ifstream In;
string fname, town;
cout<< "Welcome to the Engineering Lab." << endl
<< "This program will calculate the average carbon footprint and corresponding fine." << endl
<< "Please be ready to provide the data file name for carbon FP for US cities." << endl
<< "Please enter the full path of input data file name: ";
getline(cin,fname);
In.open(fname.c_str());
if (In){
cout<< "Please provide full path to output file: ";
getline(cin,fname);
ofstream out;
out.open(fname.c_str());
if (out){
out<< "Welcome to the Engineering Lab." << endl
<< "This program will calculate the average carbon footprint and corresponding fine." << endl;
int numTn = 0;
int totalFP = 0;
In>>town;
cout<< "****************************************************************"<<endl;
cout<< "* City Rounded Average Carbon FP Fine *" <<endl;
cout<< "****************************************************************"<<endl;
out<< "****************************************************************"<<endl;
out<< "* City Rounded Average Carbon FP Fine *" <<endl;
out<< "****************************************************************"<<endl;
while (In){
cout <<"* " << town;
out <<"* " << town;
int sumFP = 0;
int numFP = 0;
int number = 0;
int rounded;
int FP;
double average;
In>>number;
if (number < 0){
cout<< " No value on carbon FP. No fine. *" << endl;
FP = 0;
out<< " No value on carbon FP. No fine. *" << endl;
FP = 0;
}
else{
while(number >= 0){
sumFP += number;
numFP += 1;
In >> number;
}
if (numFP >= 0)
{
average = 1.0*sumFP/numFP;
rounded = int(average + 0.5);
if (rounded > 7){
FP = 4500000;
}
else if ((7>= rounded)&(rounded > 5)){
FP = 3000000;
}
else if ((5>= rounded)&(rounded > 3)){
FP = 2000000;
}
else if ((3>= rounded)&(rounded > 1)){
FP = 1000000;
}
else if (rounded <= 1){
FP = 0;
}
}
cout<< setw(30 - town.size() ) << rounded << setw(21) << FP << ".00 *" << endl;
out<< setw(30 - town.size() ) << rounded << setw(21) << FP << ".00 *" << endl;
}
numTn = numTn + 1;
totalFP = FP + totalFP;
In>>town;
if (In.eof()) {
cout<< "****************************************************************"<<endl;
cout<< "Number of cities: " << numTn << endl;
cout<< "Total Fine: " << totalFP <<".00" << endl
<< "Thank you for using the Engineering Lab. ";
out<< "****************************************************************"<<endl;
out<< "Number of cities: " << numTn << endl;
out<< "Total Fine: " << totalFP <<".00" << endl
<< "Thank you for using the Engineering Lab. ";
}
}
}
else{
cout << "Failed to open output file. Data will only be displayed on console." <<endl;
int numTn = 0;
int totalFP = 0;
In>>town;
cout<< "****************************************************************"<<endl;
cout<< "* City Rounded Average Carbon FP Fine *" <<endl;
cout<< "****************************************************************"<<endl;
while (In){
cout <<"* " << town;
out <<"* " << town;
int sumFP = 0;
int numFP = 0;
int number = 0;
int rounded;
int FP;
double average;
In>>number;
if (number < 0){
cout<< " No value on carbon FP. No fine. *" << endl;
FP = 0;
}
else{
while(number >= 0){
sumFP += number;
numFP += 1;
In >> number;
}
if (numFP >= 0)
{
average = 1.0*sumFP/numFP;
rounded = int(average + 0.5);
if (rounded > 7){
FP = 4500000;
}
else if ((7>= rounded)&(rounded > 5)){
FP = 3000000;
}
else if ((5>= rounded)&(rounded > 3)){
FP = 2000000;
}
else if ((3>= rounded)&(rounded > 1)){
FP = 1000000;
}
else if (rounded <= 1){
FP = 0;
}
}
cout<< setw(30 - town.size() ) << rounded << setw(21) << FP << ".00 *" << endl;
}
numTn = numTn + 1;
totalFP = FP + totalFP;
In>>town;
if (In.eof()) {
cout<< "****************************************************************"<<endl;
cout<< "Number of cities: " << numTn << endl;
cout<< "Total Fine: " << totalFP <<".00" << endl
<< "Thank you for using the Engineering Lab. ";
}
}
}
out.close();
}
else{
cout<< "Error opening input file. File Either does not exist or has no read permission." << endl
<< "Thank you for using the Engineering Lab. ";
}
In.close();
return 0;
}