Advertisement
Guest User

Untitled

a guest
Jun 29th, 2018
212
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.45 KB | None | 0 0
  1.  
  2. #include <iostream>
  3. #include <fstream>
  4. #include <string.h>
  5.  
  6. using namespace std;
  7. const int MAX_CHAR = 101;
  8.  
  9. void openFile(ifstream &inFile);
  10. void printFile(ifstream &inFile);
  11. void countType(ifstream &inFile);
  12. void countWeight(ifstream &inFile);
  13.  
  14. int main()
  15. {
  16. ifstream inFile;
  17. openFile(inFile);
  18. countType(inFile);
  19. countWeight(inFile);
  20. return 0;
  21. }
  22.  
  23. void openFile(ifstream &inFile)
  24. {
  25. inFile.open("cars.txt");
  26. if(!inFile)
  27. {
  28. cout << "File not opened." << endl;
  29. exit(0);
  30. }
  31. }
  32.  
  33. void countType(ifstream &inFile)
  34. {
  35. int countE = 0, countJ = 0, countU = 0;
  36. char tempType[MAX_CHAR];
  37. int i;
  38. while(!inFile.eof())
  39. {
  40. for ( i=0; i < 8; i++ )
  41. {
  42. inFile.ignore(100, ';');
  43. }
  44. inFile.get(tempType, MAX_CHAR, '\n');
  45.  
  46. if(strcmp(tempType, "Europe") == 0)
  47. {
  48. countE++;
  49. }
  50. else if(strcmp(tempType, "Japan") == 0)
  51. {
  52. countJ++;
  53. }
  54. else if(strcmp(tempType, "US") == 0)
  55. {
  56. countU++;
  57. }
  58. else
  59. inFile.ignore(50, '\n');
  60. }
  61. cout << "European cars count = " << countE << endl;
  62. cout << "Japanese cars count = " << countJ << endl;
  63. cout << "US cars count = " << countU << endl;
  64.  
  65. inFile.ignore(100, '\n');
  66. inFile.clear();
  67. inFile.seekg(0);
  68. }
  69.  
  70. void countWeight(ifstream &inFile)
  71. {
  72. int countSM = 0, countMD = 0, countLG = 0, countXL = 0, countNON = 0;
  73. int tempWeight;
  74. int j;
  75.  
  76. while(!inFile.eof())
  77. {
  78. for ( j=0; j < 5; j++ )
  79. {
  80. inFile.ignore(100, ';');
  81. }
  82. inFile >> tempWeight;
  83. cout << tempWeight;
  84. while (!inFile)
  85. {
  86. inFile.clear();
  87. inFile.ignore(100, '\n');
  88. }
  89. inFile.ignore(100, '\n');
  90.  
  91. if ( tempWeight >= 1000 && tempWeight <= 2000 )
  92. {
  93. countSM++;
  94. }
  95. else if ( tempWeight > 2000 && tempWeight <= 3000 )
  96. {
  97. countMD++;
  98. }
  99. else if ( tempWeight > 3000 && tempWeight <= 4000 )
  100. {
  101. countLG++;
  102. }
  103. else if ( tempWeight > 4000 )
  104. {
  105. countXL++;
  106. }
  107. else
  108. {
  109. countNON++;
  110. }
  111. }
  112. //inFile.clear();
  113. cout << endl;
  114. cout << "There are " << countSM << " cars between 1000 and 2000 pounds." << endl;
  115. cout << "There are " << countMD << " cars between 2000 and 3000 pounds." << endl;
  116. cout << "There are " << countLG << " cars between 3000 and 4000 pounds." << endl;
  117. cout << "There are " << countXL << " cars greater than 4000 pounds." << endl;
  118. //cout << "There are " << countNON << " cars which don't belong to any other category." << endl;
  119. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement