What do I have to do to this program to get it to read the attached file When I
ID: 3651032 • Letter: W
Question
What do I have to do to this program to get it to read the attached file When I drag and drop.#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;
int CarbonFP.txt;
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
}
}
CarbonFP[1]
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
Explanation / Answer
try using cstring instead of string.h
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.