Hello i wrote this program calculating carbon footprint. My problem is i do not
ID: 3700840 • Letter: H
Question
Hello i wrote this program calculating carbon footprint. My problem is i do not know how to input and output a file. Im using visual studio as my compiler. I tried writring on notepad and writing the name of the file into my program but does not work. if it doesnt work my program will mention an error .Need some help!!:/ I pasted my code to see if i also made an error
#include <iostream>
#include<string>
#include<cmath>
#include<iomanip>
#include<fstream>
using namespace std;
int main()
{
string Datafile;
bool flagDataFile = true;
cout << "Welcome to Carbon Footprint program ";
cout << "Please enter the input data file : " << endl;
getline(cin, Datafile);
ifstream in(Datafile);
if (!in.is_open())
{
cout << "Failed to open input data file. It is Invalid." << endl;
}
else if (in.peek() == EOF)
{
cout << "File is empty. " << endl;
}
else
{
flagDataFile = true;
}
string outfileName;
cout << "Enter full path to output file: ";
getline(cin, outfileName);
ofstream out(outfileName, ios::app);
if (!out.is_open())
{
cout << "could not open output file. " << endl;
exit(0);
}
do {
string cityName;
in >> cityName;
int oneCarbon;
int sum = 0;
int numTests = 0;
in >> oneCarbon;
while (oneCarbon >= 0)
{
sum += oneCarbon;
numTests++;
in >> oneCarbon;
}
if (numTests > 0)
{
double averageCarbon = static_cast<double>(sum) / numTests;
int roundedAvgCarbon = round(averageCarbon);
double fine;
if (roundedAvgCarbon >= 0 && roundedAvgCarbon <= 1)
{
fine = 0.00;
}
else if (roundedAvgCarbon > 1 && roundedAvgCarbon <= 3)
{
fine = 1000000.00;
}
else if (roundedAvgCarbon > 3 && roundedAvgCarbon <= 5)
{
fine = 2000000.00;
}
else if (roundedAvgCarbon > 5 && roundedAvgCarbon <= 7)
{
fine = 3000000.00;
}
else if (roundedAvgCarbon > 7)
{
fine = 4500000.00;
}
cout << endl;
cout << setfill('*') << setw(50) << "" << left << fixed << endl;
cout << setfill(' ') << setw(10) << "City" << setw(30) << "Rounded Average Carbon FP"
<< setw(10) << "Fine($)" << endl;
cout << setfill(' ') << setw(10) << cityName << setw(30) << roundedAvgCarbon
<< setw(10) << setprecision(2) << fine << endl;
cout << setfill('*') << setw(50) << "" << left << endl << endl;
out << endl;
out << setfill('*') << setw(50) << "" << left << fixed << endl;
out << setfill(' ') << setw(10) << "City" << setw(30) << "Rounded Average Carbon FP"
<< setw(10) << "Fine($)" << endl;
out << setfill(' ') << setw(10) << cityName << setw(30) << roundedAvgCarbon
<< setw(10) << setprecision(2) << fine << endl;
out << setfill('*') << setw(50) << "" << left << endl << endl;
}
else
{
cout << "The City has no Valid Carbon Footprint. Please try again." << endl;
out << "The City has no Valid Carbon Footprint. Please try again." << endl;
}
} while (in.peek() != EOF);
in.close();
out.close();
system("pause");
return 0;
}
Explanation / Answer
Solution:
Save the .txt file in the same directory where you have saved the .cpp file
You dont need to give the complete path.
Otherwise send the the .txt file to get the solution.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.