ulvis.paste.net

Paste Search Dynamic
Recent pastes
cities
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4. #include <iomanip>
  5.  
  6. using namespace std;
  7.  
  8. int main()
  9. {
  10.     cout << fixed << showpoint << setprecision(2);
  11.     cout << "Hello, we will calculate the average carbon footprint of each cities and how much they will pay." << endl;
  12.     cout << "May you please enter the path to the file to be read for calculation: " << endl;
  13.  
  14.     string inPath;
  15.     getline(cin, inPath);
  16.     ifstream take(inPath);
  17.  
  18.     if (take.is_open()) // If the file is open with no errors then execute code block below.
  19.     {
  20.         cout << "May you please enter the path to the file to be given the information about to be calculated." << endl;
  21.         string outPath;
  22.         getline(cin, outPath);
  23.         ofstream give(outPath);
  24.  
  25.         if (give.is_open())
  26.         {
  27.  
  28.  
  29.  
  30.             if (take.peek() != eof)// If the file is not at the end of the file or not empty, then execute code block below.
  31.             {
  32.  
  33.                 double totalfine = 0;
  34.                 int numcities = 0;
  35.                 cout << "*******************************************************************************************" << endl;
  36.                 cout << setw(20) << left << "City" << setw(25) << "Average Footprint" << setw(20) << "Fine ($)" << endl;
  37.                 cout << "*******************************************************************************************" << endl;
  38.                 while (take.peek() != eof) // While the file has not reached the end of the file character, keep taking in city name and carbon footprint.
  39.                 {
  40.                     string city;
  41.                     take >> city;
  42.                     int carbonprint;
  43.                     take >> carbonprint;
  44.                     int total = 0;
  45.                     int count = 0;
  46.  
  47.  
  48.                     while (carbonprint >= 0) // while the carbon footprint is positive then keep totalling up the sum and count up how many times there is a carbon footprint calculated.
  49.                     {
  50.                         total += carbonprint;
  51.                         take >> carbonprint;
  52.                         count++;
  53.                     }
  54.                     double realaverage;
  55.                     realaverage = static_cast<double>(total) / count;
  56.                     int roundedaverage = static_cast<int>(realaverage + 0.5);
  57.                     double fine;
  58.                     if (count > 0)
  59.                     {
  60.                         if (roundedaverage <= 1)
  61.                         {
  62.                             fine = 0.00;
  63.                         }
  64.                         else if (roundedaverage > 1 && roundedaverage <= 3)
  65.                         {
  66.                             fine = 1000000.00;
  67.                         }
  68.                         else if (roundedaverage > 3 && roundedaverage <= 5)
  69.                         {
  70.                             fine = 2000000.00;
  71.                         }
  72.                         else if (roundedaverage > 5 && roundedaverage <= 7)
  73.                         {
  74.                             fine = 3000000.00;
  75.                         }
  76.                         else
  77.                         {
  78.                             fine = 4500000.00;
  79.                         }
  80.  
  81.  
  82.                         cout << setw(20) << left << city << setw(25) << roundedaverage << setw(20) << fine << endl;
  83.                         totalfine += fine;
  84.                     }
  85.                     else
  86.                     {
  87.                         cout << setw(20) << left << city << setw(25) << "No carbonprint" << setw(20) << "No fine was calcualted." << endl;
  88.                     }
  89.  
  90.                     numcities++;
  91.                 }
  92.                 cout << "*****************************************************************************************" << endl;
  93.                 cout << "Total fine : " << totalfine << endl;
  94.                 cout << "Total cities in file: " << numcities << endl;
  95.                 system("pause");
  96.                 exit(0);
  97.  
  98.             }
  99.             else
  100.             {
  101.                 cout << "Your file is empty. Please close the program." << endl;
  102.             }
  103.  
  104.  
  105.         }
  106.         give.close();
  107.     }
  108.     else
  109.     {
  110.         cout << "There was an issue opening the file. Either file does not exist or fullpath was not given." << endl;
  111.     }
  112.     take.close();
  113.     system("pause");
  114.     exit(0);
Parsed in 0.031 seconds