Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <string.h>
- using namespace std;
- const int MAX_CHAR = 101;
- void openFile(ifstream &inFile);
- void printFile(ifstream &inFile);
- void countType(ifstream &inFile);
- void countWeight(ifstream &inFile);
- int main()
- {
- ifstream inFile;
- openFile(inFile);
- countType(inFile);
- countWeight(inFile);
- return 0;
- }
- void openFile(ifstream &inFile)
- {
- inFile.open("cars.txt");
- if(!inFile)
- {
- cout << "File not opened." << endl;
- exit(0);
- }
- }
- void countType(ifstream &inFile)
- {
- int countE = 0, countJ = 0, countU = 0;
- char tempType[MAX_CHAR];
- int i;
- while(!inFile.eof())
- {
- for ( i=0; i < 8; i++ )
- {
- inFile.ignore(100, ';');
- }
- inFile.get(tempType, MAX_CHAR, '\n');
- if(strcmp(tempType, "Europe") == 0)
- {
- countE++;
- }
- else if(strcmp(tempType, "Japan") == 0)
- {
- countJ++;
- }
- else if(strcmp(tempType, "US") == 0)
- {
- countU++;
- }
- else
- inFile.ignore(50, '\n');
- }
- cout << "European cars count = " << countE << endl;
- cout << "Japanese cars count = " << countJ << endl;
- cout << "US cars count = " << countU << endl;
- inFile.ignore(100, '\n');
- inFile.clear();
- inFile.seekg(0);
- }
- void countWeight(ifstream &inFile)
- {
- int countSM = 0, countMD = 0, countLG = 0, countXL = 0, countNON = 0;
- int tempWeight;
- int j;
- while(!inFile.eof())
- {
- for ( j=0; j < 5; j++ )
- {
- inFile.ignore(100, ';');
- }
- inFile >> tempWeight;
- cout << tempWeight;
- while (!inFile)
- {
- inFile.clear();
- inFile.ignore(100, '\n');
- }
- inFile.ignore(100, '\n');
- if ( tempWeight >= 1000 && tempWeight <= 2000 )
- {
- countSM++;
- }
- else if ( tempWeight > 2000 && tempWeight <= 3000 )
- {
- countMD++;
- }
- else if ( tempWeight > 3000 && tempWeight <= 4000 )
- {
- countLG++;
- }
- else if ( tempWeight > 4000 )
- {
- countXL++;
- }
- else
- {
- countNON++;
- }
- }
- //inFile.clear();
- cout << endl;
- cout << "There are " << countSM << " cars between 1000 and 2000 pounds." << endl;
- cout << "There are " << countMD << " cars between 2000 and 3000 pounds." << endl;
- cout << "There are " << countLG << " cars between 3000 and 4000 pounds." << endl;
- cout << "There are " << countXL << " cars greater than 4000 pounds." << endl;
- //cout << "There are " << countNON << " cars which don't belong to any other category." << endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement