fork download
  1. /*
  2. ID Block:
  3. Author: Sarah Choi
  4. Date: 03/05/2015
  5. Project: Carbon Footprint Assignment 5 Part 1
  6. Complier: Visual Studio 2013
  7. Operating System: Windows 7
  8. */
  9.  
  10. /*
  11. Problem: Program will ask user to input file name.
  12.   Program must open the file for each city to receive the average carbon footprint for the city.
  13.   Program will then compute the fine that the city must pay according to their average carbon footprint.
  14.  
  15. IOAA Document
  16. Part 1: Data Input
  17. Item Variable Data Type Remarks
  18. Data File Name DataFile string input
  19. dataFile flagdataFile flag true or false
  20. OutputName outputFile string output
  21. City Name cityName string
  22. Carbon Numbers oneCarbon int
  23. Counter numTests int
  24. Fine fine double
  25. Average Average Carbon double
  26. Rounded Average roundedAverage double double rounded
  27.  
  28. Part 2: Data Ouput
  29. 1. Greet User
  30. 2. Ask User to input file name
  31. 3. Ask User to input the output file name
  32. 4. Output City Name, Rounded Carbon Average, and Fine corresponding to the data input.
  33. 5. If Error, output corresponding error message only.
  34.  
  35. Part 3: Analysis
  36. declare oneCarbon from input file
  37. sum += oneCarbon
  38. averageCarbon = static_cast<double>(sum) / numTests;
  39. roundedAvgCarbon = round(averageCarbon);
  40.  
  41. Part 4: Algorithm
  42. 1. Input all of the required #include and using namespace std.
  43. 2. Greet the user and explain what the program is going to do.
  44. 3. Ask the user to input file name.
  45. 4. Check the file to make sure that it is not empty or have any negative values.
  46.   If input is invalid, print "Invalid Input."
  47.   If input is valid, compute the fine according to the data.
  48. 5. Ouput Closing Statement "THank you for using El Camino's Carbon Footprint Program"
  49. */
  50.  
  51. #include <iostream>
  52. #include <string>
  53. #include <fstream>
  54. #include <iomanip>
  55. #include <cmath>
  56.  
  57. using namespace std;
  58.  
  59. int main()
  60. {
  61. string DataFile;
  62. bool flagDataFile = true;
  63.  
  64. cout << "Hello, Welcome to Sarah's Carbon Footprint Program! :)" << endl
  65. << "We will calculate your city's Average Carbon Footprint" << endl
  66. << " and provide the Corresponding Fine." << endl << endl;
  67.  
  68. cout << "Please enter the full path of the input data file name.";
  69. getline(cin, DataFile); //Read whole line
  70. ifstream in(DataFile); //Store the path of the file
  71.  
  72. if (!in.is_open())
  73. {
  74. cout << "Failed to open Input File. Invalid File." << endl;
  75. }
  76. else if (in.peek() == EOF)
  77. {
  78. cout << "File is Empty." << endl;
  79. }
  80. else
  81. {
  82. flagDataFile = true;
  83. }
  84.  
  85. string outfileName;
  86. cout << "Enter full path to output file: ";
  87. getline(cin, outfileName);
  88. ofstream out(outfileName, ios::app);
  89.  
  90. if (!out.is_open())
  91. {
  92. cout << "Failed to open output file." << endl;
  93. exit(0);
  94. }
  95.  
  96. do{
  97.  
  98. string cityName;
  99. in >> cityName;
  100. int oneCarbon;
  101. int sum = 0;
  102. int numTests = 0;
  103. in >> oneCarbon;
  104.  
  105. while (oneCarbon >= 0)
  106. {
  107. sum += oneCarbon;
  108. numTests++;
  109. in >> oneCarbon;
  110. }
  111.  
  112. if (numTests > 0)
  113. {
  114.  
  115. double averageCarbon = static_cast<double>(sum) / numTests;
  116. int roundedAvgCarbon = round(averageCarbon);
  117.  
  118. double fine;
  119.  
  120. if (roundedAvgCarbon >= 0 && roundedAvgCarbon <= 1)
  121. {
  122. fine = 0.00;
  123. }
  124. else if (roundedAvgCarbon > 1 && roundedAvgCarbon <= 3)
  125. {
  126. fine = 1000000.00;
  127. }
  128. else if (roundedAvgCarbon > 3 && roundedAvgCarbon <= 5)
  129. {
  130. fine = 2000000.00;
  131. }
  132. else if (roundedAvgCarbon > 5 && roundedAvgCarbon <= 7)
  133. {
  134. fine = 3000000.00;
  135. }
  136. else if (roundedAvgCarbon > 7)
  137. {
  138. fine = 4500000.00;
  139. }
  140.  
  141. cout << endl;
  142. cout << setfill('*') << setw(50) << "" << left << fixed << endl;
  143. cout << setfill(' ') << setw(10) << "City" << setw(30) << "Rounded Average Carbon FP"
  144. << setw(10) << "Fine($)" << endl;
  145. cout << setfill(' ') << setw(10) << cityName << setw(30) << roundedAvgCarbon
  146. << setw(10) << setprecision(2) << fine << endl;
  147. cout << setfill('*') << setw(50) << "" << left << endl << endl;
  148.  
  149. out << endl;
  150. out << setfill('*') << setw(50) << "" << left << fixed << endl;
  151. out << setfill(' ') << setw(10) << "City" << setw(30) << "Rounded Average Carbon FP"
  152. << setw(10) << "Fine($)" << endl;
  153. out << setfill(' ') << setw(10) << cityName << setw(30) << roundedAvgCarbon
  154. << setw(10) << setprecision(2) << fine << endl;
  155. out << setfill('*') << setw(50) << "" << left << endl << endl;
  156. }
  157. else
  158. {
  159. cout << "The City has no Valid Carbon Footprint. Please try again." << endl;
  160. out << "The City has no Valid Carbon Footprint. Please try again." << endl;
  161. }
  162. } while (in.peek() != EOF);
  163.  
  164. in.close();
  165. out.close();
  166.  
  167. system("pause");
  168. return 0;
  169. }
Success #stdin #stdout 0s 5320KB
stdin
Standard input is empty
stdout
Hello, Welcome to Sarah's Carbon Footprint Program! :)
We will calculate your city's Average Carbon Footprint
 and provide the Corresponding Fine.

Please enter the full path of the input data file name.Failed to open Input File. Invalid File.
Enter full path to output file: Failed to open output file.