#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>
using namespace std;
int main()
{
system("Color 4E");
int value = 0;
int sum = 0;
int count = 0;
double fines = 0;
cout << fixed<<showpoint <<setprecision(2);
string fname;
cout << "Enter input file name: ";
getline(cin, fname);
ifstream in;
in.open(fname.c_str());
if(!in)
{
cout << "Failed to open input file: \n";
return 2;
}
string fname2;
cout << "Enter output file name: ";
getline(cin, fname2);
ofstream out;
out.open(fname2.c_str());
string city;
double average = 0.0;
int rounded = 0;
if(!out)
{
cout << "Output file could not be created. Results will be printed to console"<<endl;
cout << "********************************************************************** \n";
cout << "City \t\t Rounded Average Carbon FP \t Fines <$> \n";
cout << "********************************************************************** \n";
in>>city;
in>>value;
while(in)
{
if(value>0)
{
count++;
sum = sum + value;
}
in>>value;
if(count != 0)
{
average = double(sum)/count;
rounded = int(average + 0.5);
if(rounded <= 1)
{
fines = 0.0;
}
else if(rounded > 1 || rounded <=3)
{
fines = 1000000.0;
}
else if(rounded > 3 || rounded <= 5)
{
fines = 2000000.0;
}
else if(rounded > 5 || rounded <= 7)
{
fines = 3000000.0;
}
else if(rounded > 7)
{
fines = 4500000;
}
else
{
cout<<"No value avalible for carbon FP. Fine not collected: \n";
}
cout<<city<<"\t"<<rounded<<"\t\t\t\t"<<fines<<endl;
}
}
}
else
{
in.close();
in.open(fname.c_str());
in>>city;
in>>value;
while(in)
{
if(value>0)
{
count++;
sum = sum + value;
}
in>>value;
if(count != 0)
{
average = double(sum)/count;
rounded = int(average + 0.5);
if(rounded <= 1)
{
fines = 0.0;
}
else if(rounded > 1 || rounded <=3)
{
fines = 1000000.0;
}
else if(rounded > 3 || rounded <= 5)
{
fines = 2000000.0;
}
else if(rounded > 5 || rounded <= 7)
{
fines = 3000000.0;
}
else if(rounded > 7)
{
fines = 4500000.;
}
else
{
cout<<"No value avalible for carbon FP. Fine not collected: \n";
}
cout << city << "\t"<<rounded<<"\t\t\t\t"<<fines<<endl;
}
}
}
in.close();
out.close();
return 0;
}
/* results if no output file is present
Enter input file name: C:\Users\God\Documents\Visual Studio 2010\Projects\Carbon
\CarbonFP.txt
Enter output file name:
Output file could not be created. Results will be printed to console
**********************************************************************
City Rounded Average Carbon FP Fines <$>
**********************************************************************
Amarillo 3 1000000.00
Amarillo 3 1000000.00
Amarillo 2 1000000.00
Amarillo 2 1000000.00
Amarillo 3 1000000.00
Amarillo 3 1000000.00
Press any key to continue . . .
*/
/*Results if output file is dragged and dropped into console
Enter input file name: C:\Users\God\Documents\Visual Studio 2010\Projects\Carbon
\CarbonFP.txt
Enter output file name: C:\Users\God\Documents\Visual Studio 2010\Projects\Carbo
n\out.txt
Amarillo 3 1000000.00
Amarillo 3 1000000.00
Amarillo 2 1000000.00
Amarillo 2 1000000.00
Amarillo 3 1000000.00
Amarillo 3 1000000.00
Press any key to continue . . .
*/
/* input file
Amarillo 3 2 1 3 5 -7
Rochester 5 6 7 4 6 -2
Albuquerque 3 4 5 2 3 -9
Durham 9 8 7 9 9 8 -4
Boise 3 4 6 2 8 9 -1
Jacksonville 2 5 2 1 5 7 2 1 1 -3
Lexington 8 9 12 3 4 10 11 -6
Tulsa 8 2 9 5 6 11 8 4 2 9 -1
San_Francisco 1 1 2 1 1 0 0 1 -8
Washington -3
*/
As you can see in the commented out section of my output, the only city name to display is Amarillo, and alliteratively my results are not being printed to my out file. i have tried to various ways to receive the input for my "string city" from setting its value to an empty string, reviving its input from various locations in the while loop, but i cant figure out my problem.... any help would be tremendously appreciated.

New Topic/Question
Reply



MultiQuote



|