Where is my program failing?#include <iostream> #include <fstream> // Needed to
ID: 3650985 • Letter: W
Question
Where is my program failing?#include <iostream>#include <fstream> // Needed to read from file or write to files
#include <string>
#include <iomanip>
using namespace std;
int main(){
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;
char 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';
}
}
}}
Explanation / Answer
There are three errors in your program.
1. Make the #include<string.h>
2. Declare "fine"as string not char.
3. Initilise fine like:::: fine="1000000";
4. Also add a } at the end of your program.
#include <fstream> // Needed to read from file or write to files
#include <string.h>
#include <iomanip>
using namespace std;
int main(){
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: "<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"<return 9;//ends program here
}
else
{
ofstream out;
cout<<"Enter full path to output file"<string outFile;
getline(cin, outFile);
out.open(outFile.c_str());
if(!out){
cout<<"Failed to create output file"<return 10;
}
while(in.peek() != EOF){
out<cout<string CityName = "";
cout<<"******************************************************** ";
out<<"Hello " << CityName<
cout<<"******************************************************** ";
cout<<"Hello " << CityName<int footprint = 0;
int sum = 0;
double AverageCFP = 0.0;
int RoundedAverage = 0;
int numFP = 0;
char fine = ' ';
in>>footprint;
if(footprint<1){
out<<"Your city did not have a fine."<out<<"****************************************************** ";
cout<<"Your city did not have a fine."<out<<"****************************************************** ";
}
else{
out<<"Here are your monthly Carbon Footprints: ";
cout<<"Here are your monthly Carbon Footprints: ";
while(footprint>=0){
out<cout<sum+=footprint;
numFP++;
in>>footprint;
}
out<out<<"Your number of carbon footprints = "<out<<"Sum of all your carbon footprints = "<
cout<cout<<"Your number of carbon footprints = "<cout<<"Sum of all your carbon footprints = "<AverageCFP = double(sum)/numFP;
RoundedAverage = int(AverageCFP + 0.5);
out<<"Average CFP of all your footprints: "<out<<"Rouneded Average of all your footprints: "<
cout<<"Average CFP of all your footprints: "<cout<<"Rounded Average of all your footprints: "<if(RoundedAverage>1 && RoundedAverage<=3){
fine = '1000000';
}
}
}}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.