Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2020
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.52 KB | None | 0 0
  1. #include <cstdlib>
  2. #include <fstream>
  3. #include <iostream>
  4. #include <string>
  5. #include <vector>
  6. using namespace std;
  7.  
  8. int main()
  9. {
  10. fstream file;
  11. int line_count = 0;
  12. int months;
  13. string name_file;
  14.  
  15. std::cout << "Please input the file name for your report file: ";
  16. std::cin >> name_file;
  17.  
  18. if (name_file != "load_report.txt")
  19. {
  20. std::cout << "Invalid report file!\n";
  21. exit(1);
  22. }
  23.  
  24. std::cout << "Please provide the number of months to consolidate: ";
  25. std::cin >> months;
  26.  
  27. if (months <= 0)
  28. {
  29. std::cout << "You need to have at least one month in order to"
  30. << " consolidate your sales.\n";
  31. exit(1);
  32. }
  33.  
  34. std::cout << "\n";
  35.  
  36. file.open(name_file, ios::in);
  37.  
  38. if (file.is_open())
  39. {
  40. string line;
  41. while (getline(file, line))
  42. {
  43. line_count++;
  44. }
  45. }
  46.  
  47. else
  48. {
  49. std::cout << "Could not open file!\n";
  50. exit(1);
  51. }
  52.  
  53. file.close();
  54.  
  55. file.open(name_file, ios::in);
  56.  
  57. vector<float> a(line_count);
  58.  
  59. int k = 0;
  60.  
  61. if (file.is_open())
  62. {
  63. for (auto & val : a)
  64. {
  65. file >> val;
  66. }
  67. }
  68.  
  69. else
  70. {
  71. std::cout << "Could not open file!\n";
  72. exit(1);
  73. }
  74.  
  75. file.close();
  76.  
  77. int loops = line_count / months;
  78.  
  79. for (int i = 0; i < loops; ++i)
  80. {
  81. float sum = 0;
  82. std::cout << "Month ";
  83. for (int j = 0; j < months; ++j)
  84. {
  85. std::cout << i * months + j + 1 << " ";
  86. sum += a[i * months + j];
  87. }
  88. std::cout << "sales: $" << sum << "\n";
  89. }
  90.  
  91. return 0;
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement