General Forums >> Ask A Tech Expert >> C++ code help

Rate

C++ code help

34 Views
1 Replies Flag as inappropriate
Photo_user_blank_big

2 posts

back to top

Posted about 1 month ago

 

I'm not sure if this is the right section to post this in but.... i have this game that i have to make for class, I've been working on it for a while and i just can't figure out whats wrong and i have only 6 more hours so i thought i'd try asking for help. The game is basically a linkedlist of rooms and each room has objects in it stored in a dynamic array. The program reads from the file and everything and as far as i can see it creates the linked list and everything correctly ( i put cout's throughout the classes to make sure it does what its told). So when i pull it up and select 1 or 2 to go to the next or previous room it doesn't change. I can't figure out whats wrong, is it the iterators or what? here's the code: sorry its long



#include "zyg.h"

#include "zygnot.h"

#include "zygbot.h"

#include "room.h"

#include "linkedList.cpp"

#include <fstream>

#include <iostream>

using namespace std;

using namespace macs262_labs;





// loadRooms fills a linked list of Room

// objects with occupants of type Zyg*.

// A Zyg can be either a Zygbot or a Zygnot

void loadRooms(LinkedList<Room*>& rooms, int &itemsNeeded);

// intro displays a welcome message

void intro();

// menu displays the user options

void menu();





int main()

{

//variables

int itemsNeeded = 0, choice = 1;

LinkedList<Room*> rooms;

LinkedList<Room*>::node_iterator iter;

string item = "";

bool tookItem = false, gaveItem = false;



//loads rooms then brings up the intro

loadRooms(rooms, itemsNeeded);

intro();



iter = rooms.begin();



while(choice != 6)

{

if(choice != 1 && choice != 2 && choice != 3 && choice != 4 && choice != 5)

{

cout << "That choice is invalid!\nPlease enter a new option to continue: ";

cin >> choice;

}



menu();

cin >> choice;



switch (choice)

{

case 1:

++iter;

if(iter.hasNext())

cout << "\nHas next\n";

else

cout << "\nNo next\n";

break;

case 2:

--iter;

break;

case 3:

//CHANGED HEAD TO PUBLIC!!

rooms.head->data->displayOccupant…

break;

case 4:

if(item.compare("") != 0)

{

cout << "You already have an item, don't be greedy!\n";

break;

}

cout << "\nEnter an item to take: ";

cin >> item;

getline(cin, item);

tookItem = (*iter)->getItem(item);

if(tookItem == false)

{

cout << "You could not get the " << item << endl;

item = "";

break;

}

cout << "You now have 1 " << item << endl;

break;

case 5:

if(item.compare("") == 0)

{

cout << "That's generous but you don't have an item to give away \n";

break;

}

gaveItem = (*iter)->giveItem(item);

if(gaveItem == false)

{

cout << "Oops, you still have your item\n";

break;

}

cout << "Good, you gave away your item!\n";

item = "";

itemsNeeded--;



if(itemsNeeded == 0)

{

cout << "Congratulations, all needs have been satisfied!";

return 0;

}

break;

case 6:

return 0;

}

}



};



// loadRooms fills a linked list of Room

// objects with occupants of type Zyg*.

// A Zyg can be either a Zygbot or a Zygnot

void loadRooms(LinkedList<Room*>& rooms, int &itemsNeeded)

{

ifstream indata; // indata is like cin

int numOccupants, quant, numnots = 0, created = 0; // variable for input value

string bots; // string input variable

indata.open("ZyreneInfo.txt"); // opens the file

if(!indata) // file couldn't be opened

{

cout << "Error: the config file could not be opened" << endl;

return;

}



indata >> numOccupants;

indata.ignore();



cout << "NUMOCCUPANTS: " << numOccupants << endl;



while(!indata.eof())

{

Room* nextRoom = new Room(numOccupants);

rooms.insertAtHead(nextRoom);



if(created == numOccupants)

{

created = 0;

indata >> numOccupants;

indata.ignore();

}



while(created < numOccupants)

{

getline(indata, bots);



cout << "First Reading: " << bots << endl;



if(bots.compare("Zygbot") == 0)

{

cout << "Made a zygbot in room " << endl;

getline(indata, bots);

indata >> quant;

indata.ignore();



Zyg* zyg = new Zygbot(bots, quant);

nextRoom->addOccupant(zyg);

created ++;



cout << "Zygbot with quantity: " << quant << " and item: " << bots << endl;

}

else if (bots.compare("Zygnot") == 0)

{

cout << "Made a zygnot in room" << endl;



getline(indata, bots);



Zyg* zyg = new Zygnot(bots);

nextRoom->addOccupant(zyg);

created++;

numnots++;

}

cout << "NUM created: " << created << endl;

}

}

itemsNeeded = numnots;

}



// intro displays a welcome message

void intro()

{

cout << "Welcome to Zyrene!\n\n";

cout << "Your mission is to find Zygbots who have items to give and Zygnots who need items." << endl;

cout << "They will report their status when you ask for a list of room occupants" << endl;

cout << "Good Luck!\n\n";

}



// menu displays the user options

void menu()

 {"\nDo you want to...\n";"1: Move to the next room\n";"2: Move to the previous room\n";"3: See a list of the room occupants\n";"4: Take an item\n";"5: Give an item\n";"6: Exit\n";"Please enter a choice: ";


cout <<


cout <<


cout <<


cout <<


cout <<


cout <<


cout <<


cout <<


}