I am coding a program to calculate the % of organic farms in each if 4 western provinces.
My input file contains the following
Manitoba 21071 90
Saskatchewan 50598 773
Alberta 53652 197
B.C. 20290 319
The first number is total farms, second is organic farms.
The out put is coming out as follows
Province: Manitoba
Percent Organic: 0.00
Province: Saskatchewan
Percent Organic: 0.43
Province: Alberta
Percent Organic: 1.53
Province: B.C.
Percent Organic: 0.37
The percentages are shifted down by one province ie Manitoba should have a percentage of .43.
Not sure how to calculate the total and the average.
CODE
//* This program to calculate the percentage of organic farms in Western Canada*//
//Header Files
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
using namespace std;
int main()
{
//declare variables
ifstream infile;
ofstream outfile;
string province_name;
double total, organic;
double percent =0;
double average =0;
infile.open ("C:\\IO_Files\\organic_farms.txt");
outfile.open ("C:\\IO_Files\\organicdatasheet.txt");
//format output to two decimal places
outfile << fixed << showpoint;
outfile << setprecision(2);
do
{
// read data from the input file and write it to the output file
infile >> province_name;
outfile << "Province: " << province_name << endl;
infile >> total >> organic;
outfile << "Percent Organic: " << setw(6) << percent << endl;
outfile << endl;
average = (organic)/4;
percent = ((organic/total)*100);
}
while (!infile.eof()); //loop as long as the end of file is not reached
//close files
infile.close();
outfile.close();
return 0;
}