// main.cpp file
#include <iostream>
#include <string>
#include "Bagger.h"
int main()
{
int f;
char response = 'y';
std::string searchTerm;
Bagger class1;
class1.openWebLog();
f = class1.size();
std::cout << "There are " << (f-1) << " lines in the weblog." << std::endl;
class1.addLines(f);
class1.lineNum(f);
std::cout << "Would you like to search for a specific string? (y/n) ";
std::cin >> response;
if ( response == 'y' || response == 'Y' )
std::cout << "What string would you like to search for?" << std::endl;
std::cin >> searchTerm;
class1.lineSearch(searchTerm, f);
return 0;
}
// Header file
#ifndef BAGGER_H
#define BAGGER_H
#include <fstream>
#include <string>
class Bagger
{
public:
Bagger();
void openWebLog();
int size();
void lineNum( int &index );
void addLines( int &index );
// void doubleCapacity();
void lineSearch( std::string key, int &index );
private:
std::string key;
size_t array_size;
std::string *arr;
std::ifstream in; // Private data - for protecting data
int lineNumber;
int index;
int linesAdded;
// int numAllocated, numUsed;
};
#endif // BAGGER_H
// Implementation file
#include "Bagger.h"
#include <iostream>
// This is an implementation file, storing all of my class's
// member functions, as well as the class's constructor.
Bagger::Bagger() // Constructor
{
array_size=0;
index = 1;
linesAdded = 0;
// numAllocated = 10;
}
void Bagger::openWebLog() // Simply opens the weblog file
{
// Precondition: None
// Postcondition: The weblog file is opened and
// ready to be processed.
in.open("weblog_medium.txt");
}
int Bagger::size()
{
// Precondition: weblog file has been opened
// Postcondition: All of the lines in the weblog will be counted
// and the number stored for reference.
std::string lineCounting;
while(getline(in,lineCounting))
{
getline(in,lineCounting);
array_size++;
}
in.close();
return array_size;
}
void Bagger::addLines( int &index )
{
std::cout << "How many lines do you want to add from the open file to the array? "
<< std::endl << "Choose between 0 and " << (array_size)
<< "." << std::endl;
std::cin >> index;
arr = new std::string[index];
in.open("weblog_medium.txt");
for (int count = 0; count < index; count++)
{
getline(in,arr[count]);
}
}
void Bagger::lineNum( int &index )
{
char response = 'y';
std::cout << "The web log file has been parsed." << std::endl;
while (response == 'y' || response == 'Y')
{
std::cout << std::endl << "Which line would you like to see? "
<< std::endl << "Pick a number between 1 and "
<< (index) << "." << std::endl;
std::cin >> lineNumber;
if (lineNumber>index || lineNumber<1) break;
std::cout << "Line " << lineNumber << " is: " << std::endl
<< std::endl << arr[lineNumber-1] << std::endl
<< std::endl
<< "Would you like to see another line? (y/n)";
std::cin >> response;
}
}
void Bagger::lineSearch(std::string key, int &index)
{
// Postcondition: If I find a string 'key', store the line number
std::cout << "The following lines have the requested string: "
<< std::endl;
for(int i=0; i<index; i++)
{
if(arr[i].find(key))
{
std::cout << "Line #" << i << std::endl;
}
}
}
/*
void Bagger::doubleCapacity()
{
std::string *bigger = new std::string[numAllocated*2];
for (int i= 0; i< numUsed; i++)
bigger[i] = arr[i];
delete[] arr;
arr = bigger;
numAllocated*= 2;
}
*/