fork download
  1. //OrinTill Qimid CS1A Chapter 4, P. 294, 4
  2. //
  3. /**************************************************************
  4.  * Tic Tac Toe
  5.  *
  6.  * ____________________________________________________________
  7.  *
  8.  * ____________________________________________________________
  9.  * INPUT
  10.  *
  11.  * OUTPUT
  12.  *
  13.  **************************************************************/
  14. #include <iostream>
  15. #include <vector>
  16. #include <limits>
  17. #include <fstream>
  18. using namespace std;
  19.  
  20. int main() {
  21. const int MONTHS = 3;
  22. const int DAYS = 30;
  23.  
  24. // Initialize a 2D vector to store weather data for each month and each day
  25. vector<vector<char>> weatherData(MONTHS, vector<char>(DAYS));
  26.  
  27. // Input weather data from file
  28. string filename = "RainOrShine.dat";
  29. ifstream inputFile(filename);
  30. if (!inputFile) {
  31. cerr << "Error opening file: " << filename << endl;
  32. return 1;
  33. }
  34.  
  35. for (int month = 0; month < weatherData.size(); ++month) {
  36. for (int day = 0; day < weatherData[month].size(); ++day) {
  37. inputFile >> weatherData[month][day];
  38. }
  39. }
  40. inputFile.close();
  41.  
  42. // Display weather report
  43. const string monthNames[3] = {"June", "July", "August"};
  44. int totalRainy = 0, totalCloudy = 0, totalSunny = 0;
  45. int maxRainyDays = 0;
  46. string rainiestMonth;
  47.  
  48. for (int month = 0; month < MONTHS; ++month) {
  49. int rainyDays = 0, cloudyDays = 0, sunnyDays = 0;
  50. for (char day : weatherData[month]) {
  51. if (day == 'R') {
  52. rainyDays++;
  53. } else if (day == 'C') {
  54. cloudyDays++;
  55. } else if (day == 'S') {
  56. sunnyDays++;
  57. }
  58. }
  59.  
  60. totalRainy += rainyDays;
  61. totalCloudy += cloudyDays;
  62. totalSunny += sunnyDays;
  63.  
  64. cout << "\nWeather Report for " << monthNames[month] << ":" << endl;
  65. cout << "Rainy Days: " << rainyDays << endl;
  66. cout << "Cloudy Days: " << cloudyDays << endl;
  67. cout << "Sunny Days: " << sunnyDays << endl;
  68.  
  69. if (rainyDays > maxRainyDays) {
  70. maxRainyDays = rainyDays;
  71. rainiestMonth = monthNames[month];
  72. }
  73. }
  74.  
  75. cout << "\nThree-Month Weather Summary:" << endl;
  76. cout << "Total Rainy Days: " << totalRainy << endl;
  77. cout << "Total Cloudy Days: " << totalCloudy << endl;
  78. cout << "Total Sunny Days: " << totalSunny << endl;
  79. cout << "Month with the Most Rainy Days: " << rainiestMonth << " with " << maxRainyDays << " rainy days." << endl;
  80.  
  81. return 0;
  82. }
  83.  
Success #stdin #stdout 0.01s 5272KB
stdin
Standard input is empty
stdout
Standard output is empty