I am a No0b so please forgive my idiotic questions I may have. I am learning about Polymorphism, Classes/Objects, and vectors(containers). So, my professor decided to assign a program that uses it all.
It is supposed to be a command prompt style like this:
Welcome to the Containable Program
Please enter a command.
Type in 'help' for more info and 'end' to exit the program.
help
Available commands:
- end
terminate the program
- help
show all available commands
- print
show all available elements in the list
- addbook
add a book (title, author and price) to the list
- addword
add the given word (and its definition) to the list
- search word
display all items in the list that contains the given word
- save filename
save the current list to a given file
Containable, MyContainer, Word, Book, and commands then I have my main.cpp.
Here is the code I have so far:
1. main.cpp (Controls everything)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
|
#include <fstream>
#include <iomanip>
#include <algorithm>
#include "Containable.h"
#include "Book.h"
#include "Command.h"
#include "MyContainer.h"
#include "Word.h"
int main()
{
string foundList;
MyContainer bigList;
Containable masterList;
Command comln;
string userInput;
char charSearch[] = "searchword ";
bool isDone = false;
while (isDone == false)
{
cout << "Please enter a command. \n- Type in 'help' for more info and 'end' to exit program.\n";
getline(cin, userInput, '\n');
/*End program*/
if (userInput == "end")
{
isDone = true;
}
/*Print out all available commands*/
else if (userInput == "help")
{
comln.getCom();
}
/*Print out all inputs*/
else if (userInput == "print")
{
cout << bigList.toString();
}
/*Add a word*/
else if (userInput == "addword")
{
string word;
string definition;
cout << "What word would you like to define: ";
getline(cin, word, '\n');
cout << "What is the description: ";
getline(cin, definition, '\n');
Word * pWord1 = new Word(word, definition);
bigList.add((Containable *)pWord1);
}
/*Add a book*/
else if (userInput == "addbook")
{
string bookT;
string bookA;
string bookD;
double price;
cout << "Book title: ";
getline(cin, bookT, '\n');
cout << "Author(s): ";
getline(cin, bookA, '\n');
cout << "Description: ";
getline(cin, bookD, '\n');
cout << "Price: $";
cin >> price;
Book * pBook1 = new Book(bookT, bookA, bookD, price);
bigList.add((Containable *)pBook1);
}
/*Search for a key word input*/
else if (userInput == "searchword")
{
string keyWord;
cout << "Search by 'key word': ";
getline(cin, keyWord, '\n');
bigList.search(keyWord);
}
/*Save file*/
else if (userInput == "save filename")
{
ifstream file;
file.open("file.txt");
string z_Size = bigList.toString();
file.close();
}
/*Check if input is valid*/
else
{
cout << "\nInvalid input, try again.\n\n";
}
}
cout << "Thanks for using my program!";
cin.get();
return 0;
}
|
2. Containable.h
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
|
#ifndef CONTAINABLE_H
#define CONTAINABLE_H
#include <iostream>
#include <string>
using namespace std;
class Containable
{
public:
virtual bool contains(string aWord)
{
return false;
}
virtual string toString()
{
return "Containable";
}
};
#endif
|
3. MyContainer.h (Holds every book and word added by user)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
|
#ifndef MYCONTAINER_H
#define MYCONTAINER_H
#include <vector>
#include <iostream>
#include "Containable.h"
class MyContainer
{
private:
vector<Containable *> m_list;
public:
MyContainer()
{
}
void add(Containable *c)
{
m_list.push_back(c);
}
virtual string toString()
{
Containable *p;
int size = m_list.size();
string info = "";
for (int count = 0; count < size; count++)
info += m_list[count]->toString() + "\n";
return info;
}
vector<Containable *> search(string searchWord)
{
vector<Containable *> foundList;
int size = m_list.size();
for (int count = 0; count < size; count++)
{
if (m_list[count]->contains(searchWord))
foundList.push_back(m_list[count]);
}
return foundList;
}
};
#endif
|
4. Commands.h (List of commands)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
|
#ifndef COMMAND_H
#define COMMAND_H
using namespace std;
class Command
{
public:
void getCom()
{
cout << "\n{Available Commands}\n\n";
cout << "- end\n\n";
cout << "- help\n\n";
cout << "- print\n\n";
cout << "- addword\n\n";
cout << "- addbook\n\n";
cout << "- search word\n\n";
cout << "- save filename\n\n";
}
};
#endif
|
5. Book.h (Holds book title, author, description, and price)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
|
#ifndef BOOK_H
#define BOOK_H
#include "Containable.h"
class Book : Containable
{
private:
string m_title;
string m_author;
double m_price;
string m_bookDesc;
public:
Book(string title, string author, string bookDesc, double price)
{
m_title = title;
m_author = author;
m_bookDesc = bookDesc;
m_price = price;
}
virtual bool contains(string aSearchBook)
{
return false;
}
virtual string toString(string title, string author, string bookDesc, double price)
{
return Containable::toString() + "\nBook Title: " + title + "\nAuthor: " + author + "\nDescription: " + bookDesc + "\nPrice: " + to_string(m_price);
}
};
#endif
|
6. Word.h (Holds user words and definition)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
|
#ifndef WORD_H
#define WORD_H
#include "Containable.h"
#include "Command.h"
class Word : Containable
{
private:
string m_word;
string m_definition;
public:
Word(string word, string definition)
{
m_word = word;
m_definition = definition;
}
virtual bool contains(string aSearchWord)
{
return false;
}
virtual string toString(string word, string definition)
{
return Containable::toString() + "\nWord: " + word + "\nDefinition: " + definition;
}
public:
};
#endif
|
Gee, thanks kbw.
Ok, new question. I figured out how to store the data the user enters into MyContainer; but I cannot figure out how I am going to search the words that are being searched for and print all to the screen. Like a key word search. How should I do this?
I keep getting containable printed to the screen because that is technically what I am asking for in Containable.h. How could I modify it to print all keywords?