Ok so, this IS a homework assignment. I'm not asking for the answer. I need help on how to get started. Any help would be greatly appreciated. The assignment is to revise some code from a previous assignment, but I'm having trouble with figuring it out. The instructions are:

You will revise your code from Programming Assignment 4 in the following ways:

1. Place your fairy data into a text file. You will open the file and read it in order to construct your fairies.
2. Make all inherited methods virtual. You will call all methods using a base class pointer.
3. Dynamically create your fairies and store the pointer to each fairy in a vector. The vector type is the base class.
4. You may access each fairy from the vector using either the brackets operator, i.e., array access, or an iterator.
5. When you are finished, delete each fairy object.

The original code below creates a game where the user encounters different fairies. Each fairy acts differently. Some reward points, some take away points. I'm just having trouble revising my code. Can anyone help please?

Here is my code (its rather long)

main

#include <stdio.h>
#include <iostream>
#include "Fairy.h"
#include "GoodFairy.h"
#include "EvilFairy.h"
#include "EquiFairy.h"
using namespace std;

int main()
{
	GoodFairy f1("Bob");
	EquiFairy f2("Joe");
	EvilFairy f3("Billy-Bob");
	GoodFairy f4("John");
	EquiFairy f5("Joe-Bob");
	
	int totalPoints = 0;
	totalPoints += f1.talkToFairy();
	totalPoints += f2.talkToFairy();
	totalPoints += f3.talkToFairy();
	totalPoints += f4.talkToFairy();
	totalPoints += f5.talkToFairy();
	
	cout << "Your total score is: " << totalPoints <<endl;
	
	if(totalPoints < 0)
		cout << "COMMON BRO!  IN THE NEGATIVES??" << endl;
		
	else if(totalPoints == 0)
		cout << "I give you credit for trying" << endl;
	
	else if(totalPoints > 0)
		cout << "OH YES! THERE WE GO! A TRUE GAMER, SCORIN BIG!" << endl;
	else
		cout << "error has been encountered yo, try again!" << endl;
	
	cout << endl;
	cout << "Thank you for playing!" << endl;
	return 0;
}

Fairy.h

#ifndef FAIRY_H
#define FAIRY_H

#include <iostream>
using namespace std;

class Fairy
{
protected:
	string name;
	int decision;
	int amountOfPoints;
	int Fairywishes;
	
public:
	Fairy(string);
	~Fairy();
	int talkToFairy(); 
	virtual void askForWish();
	virtual void helpFairy();
	void sayBye();
};

#endif

Fairy.cpp

#include "Fairy.h"
#include <iostream>
using namespace std;

Fairy::Fairy( string n )
{
	name = n;
	amountOfPoints = 0;
	Fairywishes = 0;
}

Fairy::~Fairy()
{}

int Fairy::talkToFairy()
{
	cout << endl;
	cout << "Hello there.  My name is " <<name.data() << " and I am mystical fairy!" <<endl;
	
	do{
		cout << endl;
		cout << "How can I assist you today?" << endl;
		cout << "1. Can I get a wish?" << endl;
		cout << "2. Do you need any help?" << endl;
		cout << "3. I think that's all for today.  Bye!" << endl;
		cout << endl;
		cin >> decision;
		switch(decision)
		{
			case 1:
				askForWish();
				break;
			case 2:
				helpFairy();
				break;
			case 3:
				sayBye();
				break;
			default:
				cout << "Please enter 1, 2, or 3 to make you decision: " << endl;
		}
	} while(decision != 3);
	return amountOfPoints;
}

void Fairy::askForWish()
{
	cout << "Sure :)" << endl;
}

void Fairy::helpFairy()
{
	cout << "Sure :)" << endl;
}

void Fairy::sayBye()
{
	cout << "Okay then.  See ya next time!" << endl;
}

GoodFairy.h

#ifndef GOODFAIRY_H
#define GOODFAIRY_H

#include "Fairy.h"

class GoodFairy : public Fairy
{

protected:
	bool help;
	
public:
	GoodFairy(string);
	~GoodFairy();
	void askForWish();
	void helpFairy();
	void sayBye();
};

#endif

GoodFairy.cpp

#include "GoodFairy.h"

GoodFairy::GoodFairy(string w) : Fairy(w), help(false)
{}

GoodFairy::~GoodFairy()
{}

void GoodFairy::askForWish()
{
	if(Fairywishes < 5)
	{
		cout << "You wish has been granted" << endl;
		cout << "You have earned 10 points! :)" << endl;
		amountOfPoints += 10;
		Fairywishes++;
		
	}
	else
	{
		cout << "I'm out of magic dust!  No more wishes :(" << endl;
	}
}


void GoodFairy::helpFairy()
{
	if (!help)
	{
		cout << "Yeah! I do need some help as a matter of fact." << endl;
		cout << "You have earned 50 point! :D" << endl;
		amountOfPoints += 50;
		help = true;
	}
	else
	{
		cout << "Umm...nah...I got this yo." << endl;
	}
}

void GoodFairy::sayBye()
{
	cout << "Okay, Goodbye!" << endl;
}

EvilFairy.h

#ifndef EVILFAIRY_H
#define EVILFAIRY_H

#include "Fairy.h"

class EvilFairy : public Fairy
{
public:
	EvilFairy(string);
	~EvilFairy();
	void askForWish();
	void helpFairy();
	void sayBye();
};

#endif

EvilFairy.cpp

#include "EvilFairy.h"

EvilFairy::EvilFairy(string w) : Fairy(w)
{}

EvilFairy::~EvilFairy()
{}

void EvilFairy::askForWish()
{
	cout << "No.  I will not grant you a wish...ever." << endl;
	cout << "You have lost 10 points :(" << endl;
	amountOfPoints -= 10;
}

void EvilFairy::helpFairy()
{
	cout << "Me? Need help? Never! I'm the smartest fairy ever!" << endl;
	cout << "You have lost 50 points :(" << endl;
	amountOfPoints -=50;
}

EquiFairy.h

#ifndef EQUIFAIRY_H
#define EQUIFAIRY_H
#include "Fairy.h"

class EquiFairy : public Fairy
{
public:
	EquiFairy(string);
	~EquiFairy();
	void askForWish();
	void helpFairy();
	void sayBye();
	int randomDecision();
};

#endif

EquiFairy.cpp

#include "EquiFairy.h"

EquiFairy::EquiFairy(string w) : Fairy(w)
{}

EquiFairy::~EquiFairy()
{}

void EquiFairy::askForWish()
{
	if(Fairywishes < 3)
	{
		int x = randomDecision();
		if(x == 1)
		{
			cout << "Sure..why not?  You wish has been granted." << endl;
			cout << "You have earned 10 points! :)" << endl;
			amountOfPoints += 10;
			Fairywishes++;
		}
		else if(x == 2)
		{
			cout << "I am so not in the mood to grant you a wish right now..." << endl;
			cout << "You have lost 10 points! :(" << endl;
			amountOfPoints -= 10;
		}
	}

	else
	{
		cout << "Umm...if I recall correctly...I already granted you 3 wishes.  No more!" << endl;
	}
}

void EquiFairy::helpFairy()
{
	cout << "I'll think about it" << endl;
}

int EquiFairy::randomDecision()
{
	int i = (rand()%2)+1;
	return i;
}

Recommended Answers

All 5 Replies

Maybe something like this.

#include <stdio.h>
#include <iostream>
// # 3
#include <vector>
#include "Fairy.h"
#include "GoodFairy.h"
#include "EvilFairy.h"
#include "EquiFairy.h"
using namespace std;


// # 1
bool loadFairy(string infile,vector<Fairy *> &f){
  bool ret(true);
  // Use ifstream to open and read data in infile
  // If file is open
     // Loop thru the lines in the file to create
     // Fairies.

         // I think you are going to have to do something like
         // Good Bob
         // Equi Joe
         // Evil Billy-Bob
         // etc...
         // Because you have to know what type of Fairy to create

         // Get the type of Fairy so you can create the correct one
         // Example:
         if  fairyTypeYouReadOutOfFile is Good
            fairy = new GoodFairy(nameYouReadOutOfFile);
         // The other Fairy types 
         // Save the new fairy to the vector
    // End of file read loop
  // Else
     // Set error

  return ret; 
}

int main(int argc, char **argv)
{
     if ( argc != 2 ) {
         cout << "Usage: " << argv[0] << " <Fairy file>" << endl;
         return -1;
     }
     // Get the file name
     string Infile(argv[1]);
// # 3
     vector<Fairy *> fairies;
    
     // Replace below with file loading 
	//GoodFairy f1("Bob");
	//EquiFairy f2("Joe");
	//EvilFairy f3("Billy-Bob");
	//GoodFairy f4("John");
	//EquiFairy f5("Joe-Bob");
        
     // Get the Fairy information
     loadFairyInfo(Infile, fairies);
	
	int totalPoints = 0;
	//totalPoints += f1.talkToFairy();
	//totalPoints += f2.talkToFairy();
	//totalPoints += f3.talkToFairy();
	//totalPoints += f4.talkToFairy();
	//totalPoints += f5.talkToFairy();
   
// # 4
     // Loop thru all of the Fairies you put into the vector
     // and talkToFairy()
 
	cout << "Your total score is: " << totalPoints <<endl;
        // etc ...	
// # 5
     // Loop thru all of the Faiyies you create and give back the memory

	return 0;
}

1. Change four functions in fairy.h to pure virtual functions and remove the corresponding functions from fairy.cpp. You will also have to declare the same functions in each of the other header files as just virtual.

// fairy.h
	virtual void askForWish() = 0;
	virtual void helpFairy() = 0;
	virtual void sayBye() = 0;

2. in main(), delete the fairy declarations and replace it with vector<Fairy*> fairyList; 3. Create a text file that contains the fairy type and fairy name on each line. Then in main(), have it read the text file, allocate the correct type of fairy, then call the vector's push_back method to add the fairy to the vector.

4. Finally, replace all the lines in main() that call talkToFairy() with a single line inside a loop which loops through all the items in the fairyList vector. According to your instructions you would just use [] to access the item in the vector, such as fairyList[i]->talkToFairy();

Thank you! That helped a lot. Would you mind explaining the third step a bit more? I'm a little confused on how to read in the type and the name and then how to allocate the correct type of fairy.

The trick is to create a Fairy* pointer, then typecast the return from new to type Fairy, like below.

vector<Fairy*> farieList;
    std::ifstream in("Faries.txt");
    if( !in.is_open() )
    {
        cout << "Can't open the file\n";
        return 1;
    }
    string fairy_type, fairy_name;
    while( in >> fairy_type >> fairy_name )
    {
        Fairy* fairy = 0;
        if( fairy_type == "GoodFairy" )
            fairy = dynamic_cast<Fairy*> (new GoodFairy(fairy_name));
        else if( fairy_type == "EquiFairy" )
            fairy = dynamic_cast<Fairy*> (new EquiFairy(fairy_name));
        else if( fairy_type == "EvilFairy" )
            fairy = dynamic_cast<Fairy*> (new EvilFairy(fairy_name));
        if( fairy != 0)
            farieList.push_back(fairy);
    }

Thank you very much! That really helped me out and I learned something new! :D
Appreciate it!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.