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 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
|
#include <iostream>
#include <string>
#include <ctime>
#include <fstream>
using namespace std;
class Item
{
private:
std::string name, description;
//If status is 'true' the item is the murder weapon.
bool status;
// 'rft' stands for 'read from text'. These are place keepers for reading from the description file
int rft_begin, rft_end;
public:
//Constructor
Item(std::string, bool);
//Destructor
~Item();
//Fetch name of item
std::string getName();
//Fetch description of item
void getDescription(std::string);
//Fecth status - true for murder weapon, else false
bool getStatus();
};
//Constructor - being passed name and status (murder weapon or not)
Item::Item(std::string iName, bool iStatus)
{
name = iName;
status = iStatus;
}
//Destructor - not used
Item::~Item() {}
//Function to return item name
std::string Item::getName()
{
return name;
}
//Function to return item status - is it the murder weapon? (true for yes)
bool Item::getStatus()
{
return status;
}
//Function to return item descriptions
void Item::getDescription(string iName)
{
//Opening text file
string tempName = iName;
std::string text;
fstream readText;
readText.open("Item.txt");
int i = 0; //For keeping track of iterations
/*Was going to use a switch statement here but they can't parse strings, so will
have to use if statements*/
if (readText.is_open())
{
while (!readText.eof()) //Will loop until the eof (end of file).
{
getline(readText, text);
i++;
if (tempName == "tortoise" && i < 3)
{
cout << text << endl;
}
else if (tempName == "ping pong bat" && i >= 3 && i < 6)
{
cout << text << endl;
}
else if (tempName == "shetland pony" && i >= 6 && i < 12)
{
cout << text << endl;
}
else if (tempName == "orange peeler" && i >= 12 && i < 14)
{
cout << text << endl;
}
else if (tempName == "bedazzled laptop" && i >= 14 && i < 22)
{
cout << text << endl;
}
else if (tempName == "Hendrix-enchanted-guitar" && i >= 22 && i < 28)
{
cout << text << endl;
}
else if (tempName == "Beatles songbook" && i >= 28 && i < 31)
{
cout << text << endl;
}
else if (tempName == "milk-bottle-of-death" && i >= 31 && i < 34)
{
cout << text << endl;
}
}
system("pause");
}
readText.close();
}
int main()
{
//Array of items to be randomised
string aItems[8] = { "tortoise", "ping-pong-bat", "shetland-pony",
"orange-peeler", "bedazzled-laptop", "Hendrix-enchanted-guitar",
"Beatles-songbook", "milk-bottle-of-death" };
//Variables for randomisation.
int num = 0;
srand(time(0));
string sTemp;
//Loop for randomising names.
for (int i = 0; i < 12; i++)
{
//Randomising array of items
num = (rand() % 8);
sTemp = aItems[i];
aItems[i] = aItems[num];
aItems[num] = sTemp;
}
//Initialising Items.
Item i0(aItems[0], false);
Item i1(aItems[1], false);
Item i2(aItems[2], false);
Item i3(aItems[3], false);
Item i4(aItems[4], false);
Item i5(aItems[5], true); //Murder weapon.
Item i6(aItems[6], false);
Item i7(aItems[7], false);
string itemName = i1.getName();
//Debugging test
cout << i1.getName() << endl;
cout << i1.getStatus() << endl;
i1.getDescription(itemName);
system("pause");
return 0;
}
|