Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 136,070 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,603 people online right now. Registration is fast and FREE... Join Now!




Help needed-file problem-output shifts down

 
Reply to this topicStart new topic

Help needed-file problem-output shifts down

clevs
21 Apr, 2008 - 07:10 PM
Post #1

New D.I.C Head
*

Joined: 1 Apr, 2007
Posts: 13


My Contributions
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;
}      



User is offlineProfile CardPM
+Quote Post

clevs
RE: Help Needed-file Problem-output Shifts Down
21 Apr, 2008 - 08:55 PM
Post #2

New D.I.C Head
*

Joined: 1 Apr, 2007
Posts: 13


My Contributions
Thanks but I managed to figure this out all by myself. Wah hoo.
Just had to shift some code around a bit.

User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/1/08 07:06PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month