Hey guys, slightly less noob programmer here for a some more help, This week in class we were assigned to create a phonebook with a couple functions to manipulate it, but it seems I am having a couple problems.
It seems to me that I have most of the program running but I do not know how to fix the last error its giving me. It says that in transducer.h when I declare Phonebook phonebook; it says im missing a ';' before phonebook...
well anyways, here's the code
Person.h
#pragma once
#include <string>
#include "Phonebook.h"
class Person
{
public:
friend class Phonebook;
private:
int id;
std::string name;
std::string category;
std::string phoneNumber;
};
Phonebook.h
#pragma once
#include <vector>
#include "Transducer.h"
#include "Person.h"
#include <fstream>
class Phonebook
{
public:
Phonebook();
int count(){return vPhonebook.size();}
std::string show();
//string showCat(vector<Person> vPhonebook);
std::string save(std::string filename);
std::string load(std::string filename);
std::string add(std::string n, std::string c, std::string p);
//string remove();
std::string clearPhonebook(){vPhonebook.clear();return "Phone Book is cleared";}
private:
std::vector<Person> vPhonebook;
int lastPersonIDUsed;
};
Phonebook.cpp
#include "Phonebook.h"
#include "misc.h"
#include <fstream>
Phonebook::Phonebook()
{
lastPersonIDUsed=0;
}
string Phonebook::show()
{
std::string result;
result += padRight("Name",'.',10);
result += padRight("Author",'.',20);
result += padRight("Phone Number",'.',20);
vector<Person>::iterator iter;
iter = vPhonebook.begin();
while(iter!=vPhonebook.end())
{
result += padRight((*iter).name,' ',20);
result += padRight((*iter).category,' ',20);
result += padRight((*iter).phoneNumber,' ',20);
}
return result;
}
//string Phonebook::showCat(vector<Person> &vPhonebook)
//{
//
//}
string Phonebook::save(std::string filename)
{
ofstream fout;
fout.open(filename);
fout<<lastPersonIDUsed<<endl;
vector<Person>::iterator iter;
iter = vPhonebook.begin();
while(iter!=vPhonebook.end())
{
fout<<(*iter).name<<' ';
fout<<(*iter).category<<' ';
fout<<(*iter).phoneNumber<<' ';
iter++;
}
return "Books saved in file.\n";
}
string Phonebook::load(string filename)
{
clearPhonebook();
ifstream fin;
fin.open(filename);
std::string inputLine;
getline(fin, inputLine);
lastPersonIDUsed = stringToInt(inputLine);
while(!fin.eof())
{
getline(fin, inputLine);
vector<string> token = tokenize(inputLine);
if(token.size()==3)
{
Person readPerson;
readPerson.name=token[1];
readPerson.category=token[2];
readPerson.phoneNumber=token[3];
vPhonebook.push_back(readPerson);
}
}
}
//string Phonebook::clear(vector<Person> &vPhonebook)
//{
//
//}
std::string Phonebook::add(std::string n, std::string c, std::string p)
{
Person newPerson;
newPerson.name=n;
newPerson.category=c;
newPerson.phoneNumber=p;
lastPersonIDUsed++;
newPerson.id=lastPersonIDUsed;
vPhonebook.push_back(newPerson);
return "Person added.\n";
}
//string Phonebook::remove()
//{
//
//}
Transducer.h
#pragma once
#include "Phonebook.h"
#include <string>
class Transducer
{
public:
std::string transduce(std::string command);
private:
Phonebook phonebook; //The problem my compiler is having is with this line
};
The problem my compiler is the ABOVE line
Transducer.cpp
#include "Transducer.h"
#include <vector>
#include "misc.h"
//ADD This is a string.
string Transducer::transduce(std::string command)
{
std::string result;
vector<std::string> token = tokenize(command);
if((token[0]=="HELP")||(token[0]=="H"||token[0]=="help")||(token[0]=="h"))
{
result="Commands:\n";
result+="-------------------\n";
result+="\tHELP(H)\n";
result+="\tQUIT(Q)\n";
result+="\tSHOW(SH)\n";
//result+="\tSHOWCAT(SC)\n";
result+="\tSAVE(S)\n";
result+="\tLOAD(L)\n";
result+="\tCLEAR(CL)\n";
result+="\tCOUNT(CO)\n";
result+="\tADD(A)\n";
//result+="\tREMOVE(R)\n";
}
else if((token[0]=="SHOW")||(token[0]=="SH"||token[0]=="show")||(token[0]=="sh"))
{
result=phonebook.show();
}
//else if((token[0]=="SHOWCAT")||(token[0]=="SC"||token[0]=="showcat")||(token[0]=="sc"))
//{
//result=phonebook.showCat();
//}
else if((token[0]=="SAVE")||(token[0]=="S"||token[0]=="save")||(token[0]=="s"))
{
if
(token.size()!=2)
{
result="ERROR: wrong number of perameters.";
}
else
{
result=phonebook.save(token[1]);
}
}
else if((token[0]=="LOAD")||(token[0]=="L"||token[0]=="load")||(token[0]=="l"))
{
if
(token.size()!=2)
{
result="ERROR: wrong number of perameters.";
}
else
{
result=phonebook.load(token[1]);
}
}
else if((token[0]=="CLEAR")||(token[0]=="CL"||token[0]=="clear")||(token[0]=="cl"))
{
result=phonebook.clearPhonebook();
}
else if((token[0]=="COUNT")||(token[0]=="CO"||token[0]=="count")||(token[0]=="co"))
{
result="There are "+intToString(phonebook.count())+" people in the Phone book";
}
else if((token[0]=="ADD")||(token[0]=="A"||token[0]=="add")||(token[0]=="a"))
{
result=phonebook.add(token[1],token[2],token[3]);
}
//else if((token[0]=="REMOVE")||(token[0]=="R"||token[0]=="remove")||(token[0]=="r"))
//{
//result=phonebook.remove();
//}
else
{
result="Unrecognized Command!!!";
}
return result;
}
Main.cpp
#include <iostream>
#include <string>
#include <fstream>
#include <vector>
#include <time.h>
#include "Transducer.h"
using namespace std;
void commandLineInterface(Transducer & t)
{
string com;
cout<<t.transduce("HELP")<<endl;
do
{
cout<<">";
getline(cin,com);
cout<<t.transduce(com)<<endl;
} while ((com!="QUIT")&&(com!="Q")&&(com!="quit")&&(com!="q"));
}
string getCommandFrom(ifstream & fin)
{
string com;
if(fin.eof())
{
com="ENDBATCH";
}
else
{
getline(fin,com);
}
if( fin.eof() && com=="" )
{
com="ENDBATCH";
}
return com;
}
string metaHelp()
{
string result;
result += "Meta Commands:\n" ;
result += "--------------\n" ;
result += "\tBATCH <filename>\n";
result += "\tENDBATCH\n" ;
result += "\tREDIRECT <filename>\n";
result += "\tDIRECT\n" ;
result += "\tTON\n" ;
result += "\tTOFF\n" ;
result += "\tMETAHELP\n\n" ;
return result;
}
void advancedCommandLineInterface(Transducer & t)
{
ifstream fin;
ofstream fout;
bool batchActive=false;
bool redirectActive = false;
bool timerOn=false;
string com;
cout<<metaHelp();
cout<<t.transduce((string)"HELP")<<endl;
do
{
if(!batchActive)
{
cout<<">";
getline(cin,com);
}
else
{
com=getCommandFrom(fin);
}
//META COMMANDS
if(com.substr(0,5)=="BATCH")
{
string filename=com.substr(6,9999);
fin.open(filename.c_str());
batchActive=true;
}
else if(com=="ENDBATCH")
{
fin.close();
fin.clear();
batchActive=false;
}
else if(com.substr(0,8)=="REDIRECT")
{
string filename=com.substr(9,9999);
fout.open(filename.c_str());
redirectActive=true;
}
else if(com=="DIRECT")
{
fout.close();
fout.clear();
redirectActive=false;
}
else if(com=="TON")
{
timerOn=true;
}
else if(com=="TOFF")
{
timerOn=false;
}
else if(com=="METAHELP")
{
if(redirectActive)
{
fout<<metaHelp();
}
else
{
cout<<metaHelp();
}
}
else
{
string transducerResults;
clock_t startTime=clock();
transducerResults=t.transduce(com);
clock_t endTime=clock();
clock_t elapsedTime=endTime-startTime;
if(!redirectActive)
{
cout<<transducerResults<<endl;
if(timerOn) cout<<"["<<elapsedTime<<"ms]"<<endl;
}
else
{
fout<<transducerResults<<endl;
if(timerOn) fout<<"["<<elapsedTime<<"ms]"<<endl;
}
}
} while ((com != "QUIT")&&(com != "quit")&&(com != "Q")&&(com != "q"));
}
int main()
{
Transducer transducer;
advancedCommandLineInterface(transducer);
return 0;
}
Any and all help is greatly appreciated. Thanks again guys.