Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

#include <iostream> #include <fstream> // Needed to read from file or write to f

ID: 3651007 • Letter: #

Question

#include <iostream>
#include <fstream> // Needed to read from file or write to files
#include <string>
#include <string.h>
#include <iomanip>
using namespace std;

int main(){
int numCities;
int numFine;

cout<<"************************************************************** ";
cout<<"Welcome to Rose State Software Engineering Lab. ";
cout<<"We will calculate average carbon footprint and corresponding fine. ";
cout<<"************************************************************** ";

//Read an entire line from a file and
//print to console.
ifstream in;//Declare an object of ifstream type
string inputFile;
cout<<"Enter full path to input file: "<<endl;
getline(cin, inputFile);//Must use getline because file name may contain white space
//String class was added later so cannot pass string as such
//to open function which requires a C style string
//So we use c_str function of string class to convert it
//to c style string, which open function accepts
in.open(inputFile.c_str());
if(!in){
cout<<"File opening error"<<endl;
return 9;//ends program here
}
else
{
ofstream out;
cout<<"Enter full path to output file"<<endl;
string outFile;
getline(cin, outFile);
out.open(outFile.c_str());

if(!out){
cout<<"Failed to create output file"<<endl;
return 10;
}

while(in.peek() != EOF){
out<<fixed<<showpoint<<setprecision(2);
cout<<fixed<<showpoint<<setprecision(2);
string CityName = "";
cout<<"******************************************************** ";
out<<"Hello " << CityName<<endl;

cout<<"******************************************************** ";
cout<<"Hello " << CityName<<endl;
int footprint = 0;
int sum = 0;
double AverageCFP = 0.0;
int RoundedAverage = 0;
int numFP = 0;
string fine = " ";

in>>footprint;

if(footprint<1){
out<<"Your city did not have a fine."<<endl;
out<<"****************************************************** ";
cout<<"Your city did not have a fine."<<endl;
out<<"****************************************************** ";
}
else{
out<<"Here are your monthly Carbon Footprints: ";
cout<<"Here are your monthly Carbon Footprints: ";

while(footprint>=0){
out<<footprint<<" ";
cout<<footprint<<" ";
sum+=footprint;
numFP++;
in>>footprint;
}
out<<endl;
out<<"Your number of carbon footprints = "<<numFP<<endl;
out<<"Sum of all your carbon footprints = "<<sum<<endl;

cout<<endl;
cout<<"Your number of carbon footprints = "<<numFP<<endl;
cout<<"Sum of all your carbon footprints = "<<sum<<endl;
AverageCFP = double(sum)/numFP;
RoundedAverage = int(AverageCFP + 0.5);
out<<"Average CFP of all your footprints: "<<AverageCFP<<endl;
out<<"Rouneded Average of all your footprints: "<<RoundedAverage<<endl;

cout<<"Average CFP of all your footprints: "<<AverageCFP<<endl;
cout<<"Rounded Average of all your footprints: "<<RoundedAverage<<endl;
if(RoundedAverage>1 && RoundedAverage<=3){
fine = "1000000";
}
else if (RoundedAverage>3 && RoundedAverage<=5){
fine = "2000000";
}
else if (RoundedAverage>5 && RoundedAverage<=7){
fine = "3000000";
}
else if (RoundedAverage>7){
fine = "4500000";
}
out<<"Your Fine = "<<fine<<endl;
cout<<"Your Fine = "<<fine<<endl;
}//data processing ends here
}
out<<"**********************************************************"<<endl;
cout<<"**********************************************************"<<endl;
out<<"Cities in File = "<<numCities<<endl;
cout<<"Cities in File = "<<numCities<<endl;
out<<"Cities With fine of 1000000 ="<<numFine<<endl;
cout<<"Cities With fine of 1000000 ="<<numFine<<endl;
out<<"Cities With fine of 2000000="<<numFine<<endl;
cout<<"Cities With fine of 2000000="<<numFine<<endl;
out<<"Cities With fine of 3000000="<<numFine<<endl;
cout<<"Cities With fine of 3000000="<<numFine<<endl;
out<<"Cities With fine of 4500000="<<numFine<<endl;
out.close();
}//else block for output file

}//else block



}
}

Explanation / Answer

try using cstring instead of string.h