I have the following files:
main.cpp
shop.hpp
player.hpp
With the following code in each of them:
main.ccp:
#include <iostream>
#include <cstdlib>
#include "shop.hpp"
using namespace std;
string *inventory= new string[3];
int invGold= 355;
int main(void){
shop store;
store.store();
}
shop.hpp:
#include <iostream>
#include <cstdlib>
using namespace std;
class shop{
public:
string shopOption;
string shopOptions[6]= {"Buy", "buy", "Sell", "sell", "Leave", "leave"};
string shopInv[3]= {"Sword", "Potion", "Arrows x 25"};
int shopInvAmount= sizeof(shopInv)/sizeof(shopInv[0]);
int shopPrices[3]= {250, 55, 70};
shop(){
cout << "Shopkeeper: We buy, we sell, what's your buisness?" << endl;
}
void store(void){
getline(cin,shopOption);
if(shopOption.compare(shopOptions[0]) == 0 || shopOption.compare(shopOptions[1]) == 0){
buy();
}
else if(shopOption.compare(shopOptions[2]) == 0 || shopOption.compare(shopOptions[3]) == 0){
sell();
}
else if(shopOption.compare(shopOptions[4]) == 0 || shopOption.compare(shopOptions[5]) == 0){
leave();
}
}
void buy(){
srand(time(0));
string buyQuotes[3]= {"What are you buyin', hon?", "Make it quick, I ain't got all day.", "Another day, another sell."};
int quotePick= rand() % sizeof(buyQuotes)/sizeof(buyQuotes[0]) - 1;
if (quotePick < 0){
quotePick= 0;
}
else if (quotePick > (sizeof(buyQuotes)/sizeof(buyQuotes))){
quotePick= sizeof(buyQuotes)/sizeof(buyQuotes);
}
cout << "TEST:" << sizeof(shopInv)/sizeof(shopInv[0]) << endl;
cout << buyQuotes[quotePick] << endl;
cout << "SHOP INVENTORY" << endl << "--------------" << endl;
cout << endl;
for (int i=0; i < sizeof(shopInv)/sizeof(shopInv[0]); i++){
cout << shopInv[i]<< ": " << shopPrices[i] << endl;
}
cout << endl << "What'll it be?:";
getline(cin,shopOption);
}
void sell(){
}
void leave(){
}
};
and player.hpp
class player{
public:
int playerHP= 18;
string playerInv[5] {};
int playerGold= 355;
};
Now, what i'd like to do, is that after the character selects the items they want to buy, and te amount of it, (Not programmed yet) check the price of the combined items, and see if the character has enough money on hand, and if the character buys the items, add them to the player's inventory.
But i'd like to keep the values the store uses, and everything related to the player in different class files.
Thing is, I have no idea how to pull something like that.
So, is t possible to access a class' variable from another class that is in another file althogether?
And if isn't, how would you suggest i get around this problem?
Start reading here: How does the compilation/linking process work? to get multiple files working for you. Odds are pretty good that whatever coding environment you are using will automate the process for you.
Then consider making an item class
class Item
{
public:
Item(string name, int price): mName(name), mPrice(price)
{
}
string getName()
{
return mName;
}
string getPrice()
{
return mPrice;
}
// other functions
private:
string mName;
int mPrice;
// other stuff
}
In Shop and Player, keep a list of Items
vector<Item> items;
When a Player tries to buy an item, find it in the list, ensure the Player can afford it, remove it from the Shop's list and add it to the Player's list.
Related
So I have a program that has a class that represents a player (also called player). The player needs to have a name, password, amount of experience, a position, and an inventory of four items. The program needs to create three (hardcoded) players each with a name, password, experience amount, position, and an inventory of four items. I have it mostly done except one thing, the inventory array. I have it partially set up with a setInv and getInv to both set and get the inventory, but I'm uncertain on how to use getInv to put items into the inventory so I can use getInv to display it. I tried putting an already filled array in the class but the display outputs none of the items. I also tried playerOne("a"); and playerOne.setInv() = "a"; to but both fail. With the first giving me the error of "too many arguments in function call".
I'm also getting the errors The second one give me two errors of "Index '4' is out of valid index range '0' to '3' for possibly stack allocated buffer 'inv'." and "Reading invalid data from 'inv': the readable size is '112' bytes, but '140' bytes may be read."
I'm very new to C++ and would appreciate any help, thank you!
#pragma warning(disable: 4996)
#include<string>
#include <iostream>
#include <cstdlib>
#include <string>
using namespace std;
//player class
class player {
public:
//name
void setName(string name) {
this->name = name;
} //setName end
string getName() {
return name;
} //getName end
//password
void setPass(string pass) {
this->pass = pass;
} //setPass end
string getPass() {
return pass;
} //getPass end
//experience
void setXP(int xp) {
this->xp = xp;
} //setXP end
int getXP() {
return xp;
} //getXP end
//position
void setPosX(int xPos) {
this->xPos = xPos;
} //setPosX end
void setPosY(int yPos) {
this->yPos = yPos;
} //setPosY end
int getPosX() {
return xPos;
} //getPosX end
int getPosY() {
return yPos;
} //getPosY end
//inventory
string setInv() {
string inv[4];
this->inv = inv[4];
} //setInv end
string getInv() {
return inv;
} //getInv end
void display();
private:
string name;
string pass;
string inv;
int xp = 0;
int xPos = 0;
int yPos = 0;
}; //class end
void player::display() {
//playerOne output
cout << "Player Info - \n";
cout << "Name: " << getName() << "\n";
cout << "Password: " << getPass() << "\n";
cout << "Experience: " << getXP() << "\n";
cout << "Position: " << getPosX() << ", " << getPosY() << "\n";
cout << "Inventory: ";
for (int i = 0; i < 4; i++) {
getInv();
cout << "\n\n";
} //for end
}
int main()
{
//playerOne
player playerOne;
playerOne.setName("Porcupixel");
playerOne.setPass("PokeHerFace2008");
playerOne.setXP(1477);
playerOne.setPosX(16884);
playerOne.setPosY(10950);
//playerTwo
player playerTwo;
playerTwo.setName("Commandroid");
playerTwo.setPass("RodgerRodger00110001");
playerTwo.setXP(73721);
playerTwo.setPosX(6620);
playerTwo.setPosY(36783);
//playerThree
player playerThree;
playerThree.setName("BumbleBeast");
playerThree.setPass("AutoBotsRule7");
playerThree.setXP(20641);
playerThree.setPosX(15128);
playerThree.setPosY(46976);
playerOne.display();
playerTwo.display();
playerThree.display();
return 0;
} //main end
All right, first for your inventory set up, you could do it in a bunch of diverse ways, to begin within your 'setInv' function you are not receiving parameters which is weird since what are you trying to initialize your inventory with? You could initialize all values passing in an array of strings if that makes sense, also from what I gather from your getInv function it looks like you're trying to store the contents of your array of strings in your private var 'inv' however you might be failing to do this since it is only a string and not an array of string, meaning it can only store ONE string or object.
Answering your question more specifically, your 'getInv' is not returning anything because you are not storing anything into your string to begin with:
Just to explain a little bit more.
string setInv() {
string inv[4];
this->inv = inv[4];
} //setInv end
In this code, you are declaring a new array of strings of size 4, then saying that inv is equal to inv[4] which is never initialized therefore you're not storing anything, anyway as I explained earlier it doesn't look like the right way to do what you're trying to!
I am trying to develop a text adventure in C++ where users can input string commands (ex. "take apple").
Here is a very naive sample of code I came up with:
# include <iostream>
using namespace std;
class fruit{
public:
string actual_name;
fruit(string name){
actual_name = name;
}
take() {
cout << "You take a " << actual_name << "." << endl;
}
};
fruit returnObjectFromName(string name, fruit Fruits[]){
for(int i = 0; i <= 1; i++){ // to be modified in future depending on Fruits[] in main()
if (Fruits[i].actual_name == name)
return Fruits[i];
}
}
int main(){
string verb;
cout << "Enter verb: ";
cin >> verb;
string object;
cout << "Enter object: ";
cin >> object;
fruit apple("apple");
fruit Fruits[] = { apple }; // to be extended in future
// returnObjectFromName(object, Fruits). ??? ()
}
How can I possibly get the fruit method with something similar to the function returnObjectFromName, if this is even possible?
I began the development with Python (independently), and there I can at least use eval(), but as I understand in C++ this is not an option.
I tried also with map, but I didn't manage to make it work with methods.
Thank you all for your answers.
Its not good way to rely on reflection in C++ and i think there is no way to list methods in classes. Maybe you can use function pointers but pointer to instance methods are hell.
I recommend to use polymorphism and good design. If some items might be taken, then use interface like this:
#include <iostream>
using namespace std;
class ITakeable {
public:
virtual bool isTakeable() = 0;
virtual void take() = 0;
virtual void cannotTake() = 0;
};
class fruit : public ITakeable {
public:
string actual_name;
fruit(string name){
actual_name = name;
}
bool isTakeable() {
return true;
}
void take() {
cout << "You take a " << actual_name << "." << endl;
}
void cannotTake() {
cout << "not needed to be implemented";
}
};
class airplane : public ITakeable {
public:
string actual_name;
airplane(string name){
actual_name = name;
}
bool isTakeable() {
return false;
}
void take() {
cout << "not needed to be implemented";
}
void cannotTake() {
cout << "You CANNOT take a " << actual_name << "." << endl;
}
};
int main() {
fruit apple("apple");
if (apple.isTakeable()) {
apple.take();
}
airplane plane("boeing");
if (plane.isTakeable()) {
plane.take();
} else {
plane.cannotTake();
}
// use of interface in general
ITakeable * something = &apple;
if (something->isTakeable()) {
something->take();
}
something = &plane;
if (something->isTakeable()) {
something->take();
} else {
something->cannotTake();
}
return 0;
}
Since fruit is a user defined type, you have to declare your own methods for your type or you inherit from one previously defined.
There are a lot of method for "built-in" string type
that Performs virtually the same job as eval (...) in python.
Also I noticed your function need not be defined independently outside of class fruit.
I pretty new to coding and a lot of things are pretty foreign to me. I am writing a c++ program that is supposed to take a list of songs from a txt file and be able to shuffle, sort, and search for a song in the list. I have only started on the sorting part as of now, but I am having trouble finding out how to format the algorithm to work with my vector of songs.
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <cstdlib>
#include <time.h>
#include <stdlib.h>
#include <bits/stdc++.h>
#include <algorithm>
#include "song.h"
using namespace std;
// given to you
void processFile(vector<Song> &playlist);
// you should create
void shuffle(vector<Song> &playlist);
void bubbleSort(vector<Song> &playlist);
void displayPlaylist(vector<Song> playlist);
int binarySearch(vector<Song> &playlist, string songTitle);
int main()
{
vector<Song> playlist;
// sets up playlist
processFile(playlist);
cout << "\nInitial playlist: " << endl;
//displayPlaylist(playlist);
displayPlaylist(playlist);
cout << "Welcome to the playlist display manager." << endl << endl;
while(1)
{
int option;
cout << "0. Exit" << endl;
cout << "1. Sort Playlist" << endl;
cout << "2. Shuffle Playlist" << endl;
cout << "3. Search Playlist" << endl;
cout << "Which option would you like" << endl;
cin >> option;
if(option == 0)
{
break;
}
else if(option == 1)
{
bubbleSort(playlist);
displayPlaylist(playlist);
}
else if(option == 2)
{
}
else if(option == 3)
{
}
else
{
cout << "invalid response...try again" << endl;
}
}
return 0;
}
void processFile(vector<Song> &playlist)
{
ifstream infile;
string line;
infile.open("songs.txt");
if(infile.is_open())
{
cout << "Successful songs opening." << endl;
}
else
{
cout << "Couldn't locate file. Program closing." << endl;
exit(EXIT_FAILURE);
}
while(getline(infile, line))
{
// first line --> song
// second line --> artist
if(line != "")
{
string song, artist;
song = line;
getline(infile, artist);
Song temp(song, artist);
playlist.push_back(temp);
}
}
return;
}
void shuffle(vector<Song> &playlist)
{
}
void selectionSort(vector<Song> &playlist, int n)
{
}
void bubbleSort(vector<Song>& playlist)
{
int size;
size = playlist.size();
for(int i= 0; i < size - 1; i++)
{
int smallIndex = i;
for(int j = i + 1; j < size; j++)
{
if(&playlist[j] < &playlist[smallIndex])
{
smallIndex = j;
}
}
string song, artist;
Song temp(song, artist);
temp = playlist[i];
playlist[i] = playlist[smallIndex];
playlist[smallIndex] = temp;
}
}
//display songs
void displayPlaylist(vector<Song> playlist)
{
for(int i = 0; i < playlist.size(); i++)
{
cout << playlist[i].getTitle() << " - " << playlist[i].getArtist() << endl;
}
}
Here is what I have so far. I am supposed to use a function to sort the sort the songs. The vector uses a class that was given to me to help classify the line of songs in the txt file by song then artist (title being the first thing listed in the line) and I'm supposed to sort by the title. This is just the last algorithm I attempted. I'm not required to use selection sorting. Whenever I call the function and try to display the list, it comes out the same.
edit: Sorry, it just occurred that I should probably go ahead and show all my code even if it's not done.
Your sorting algorithm is almost correct with small mistake .
You need to drop the & in if condition of your nested loop , you inner loop should be as follows ,
for(int j = i + 1; j < size; j++)
{
if(playlist[j] < playlist[smallIndex])
{
smallIndex = j;
}
}
Though this being said , since playlist is vector of Song objects , and you are using < operator on these objects , you will need to overload less-than < operator for your class . On the other hand , if you need to sort by song name or artist name , and they are well defined C++ objects(since most C++ library objects have already defined less-than operator for them) . For instance if song name or artist name are strings , and you need to sort by , let's say song name , then you can do it like this ,
if(playlist[j].song_name < playlist[smallIndex].song_name)
Here , you don't need to prefix the variable by ampersand & , you might be getting confused due to using ampersand with the variable playlist in function parameter list . Well , ampersand there is to tell the compiler to pass the variable as reference . For more information regarding reference variables , read the following links ,
What is reference variable in C++ and Reference variables
#include "assert.h"; // for some reason assert wouldn't work on my compiler without this
#include <iostream>
#include <string>
#include <limits> // This is helpful for inputting values. Otherwise, funny stuff happens
using namespace std;
class Product
{
public:
Product();
Product(string the_name, int the_price, int number_of);
string return_name();
void reduce_amount();
void print_data() const;
private:
string prod_name; // name of your product
int price_in_cents; // it's price in cents
int amount; // the number of the product that you have
};
Product::Product()
{
prod_name = "NULL_NAME: NEED DATA";
price_in_cents = 0;
}
Product::Product(string the_name, int the_price, int number_of)
{
assert(the_price>0);
assert(number_of>0);
assert(number_of<21);
assert(prod_name !="NULL_NAME: NEED DATA");
prod_name = the_name;
price_in_cents = the_price;
amount = number_of;
}
void Product::print_data() const
{
cout<<prod_name << endl;
cout<<"The price in cents is: " <<price_in_cents<< endl;
cout<< "Amount left: " << " " << amount << endl;
}
void Product::reduce_amount()
{
amount = amount -1;
}
string Product::return_name()
{
return prod_name;
}
class Vending_Machine
{
public:
Vending_Machine();
void empty_coins();
void print_vend_stats();
void add_product();
Product buy_product();
private:
int income_in_cents;
Product product1();
Product product2();
Product product3();
Product product4();
Product product5();
};
void Vending_Machine::empty_coins()
{
cout << "The total amount of money earned today is " << income_in_cents << " cents" << endl;
income_in_cents = 0;
cout << "All the coins have been withdrawn. The balance is now zero." << endl;
}
void Vending_Machine::print_vend_stats()
{
cout<< "Total income thus far: " << income_in_cents << endl;
if (product1().return_name() != "NULL_NAME: NEED DATA")
{
//stuff happens
}
}
int main()
{
return 0;
}
So, I'm not sure if I did all the identation correctly, but I'm having a problem with the boolean statement in vending machine print_vend_stats() function. It's saying I am making an undefined fereence to product1(). What does this mean?
When you declare
Product product1();
you declare a member function, the parentheses is what makes it a function.
If you drop the parentheses
Product product1;
you declare a member variable, an actual instance of the Product class.
Another example, you wouldn't write e.g.
int income_in_cents();
do declare income_in_cents as a variable, now would you?
It doesn't matter if the type is a primitive type like int, or a class like Product, Member variables are declared like normal variables like you do anywhere else.
I'm writing a program that uses OOP to store student records. At the moment I only have two classes, one for each individual course module called 'Courses', and one ( well two if you count the abstract base class) for the type of degree programme called 'Physics' derived from the 'Records' base class.
I'm using two maps in the program. One to store the individual courses for each individual record and sort them by course code, and one to store all the records and sort them by ID numbers.
I planned on having the user input all student information, including codes, storing this in a vector (named 'prec' in the code), then pushing the vector elements into the map used to store all the records. The code is far from finished, I was just attempting to run it to see if I was on the right track.
The code builds without any errors, but when I attempt to run it, it comes up with the error message: " Debug assertion failed: expression vector subscript out of range". I feel this may have something to do with the way I am using individual vector elements to call my functions to store courses in the maps but I cant quite get it, any help would be much appreciated!
Here are my files:
header file:
#ifndef MY_CLASS_H // Pre-processor directives to prevent multiple definition
#define MY_CLASS_h
#include <iostream>
#include <string>
#include <utility>
#include <map>
#include <fstream>
using std::string;
using std::ostream;
using std::map;
using std::cout;
using std::endl;
using std::cin;
namespace student_record // Defines the namespace student_record in which the classes are defined
{
class Course { /* Create class Course for individual courses, is this better than incorporating
all the data separately into the Record class below? Class contains course name, mark achieved and mark weight and course ID */
protected:
string course_name;
double course_mark;
int course_Id;
public:
Course() {course_name= "Null"; // Default constructor for null course
course_mark=0;
}
Course(string course_namein, double course_markin, int course_Idin) {course_name=course_namein; // Parametrized constructor to create course with set name, mark, weight and course ID
course_mark=course_markin;
course_Id=course_Idin;}
~Course() {course_name.erase(0,course_name.size());} // Destructor to delete the course name
// Access functions to get name, mark and weight //
double getmark() const {return course_mark;}
string getname() const {return course_name;}
int getid() const {return course_Id;}
friend ostream & operator << (ostream &os, const Course &c); // Friend function to overload the insertion operator for courses
};
class Record
{ // Create class Record as abstract base class for all inherited degree classes
protected:
string student_name;
int studentid;
int years;
public:
Record() {student_name="Casper";
studentid=0;
years=0;} // Default constructor for class Record, produces empty record
Record(string name, int number, int time) {student_name=name;
studentid=number;
years=time;} // Parametrized constructor for class Record
~Record() {student_name.erase(0, student_name.size());} // Destructor to delete the student name
virtual int getid()const=0;
virtual int getyears()const=0;
virtual void show_record()const=0;
virtual void print_record(string *filename)const=0;
virtual void degree_class()const=0;
virtual void insert_class()=0;
/* Virtual functions defined to be used in the derived classes (subjects ie, Physics, stamp collecting, etc...)
Thus the base class Record is abstract*/
};
class Physics: public Record
{
private:
string degree_name;
typedef map <int, Course> course_map;
course_map modules;
void searchdatabase (course_map &courses, int coursecode)const; // Uses iterator to search map for corresponding course to inputted key ( remember to move to function definitions)
string get_name (const int i, course_map &temp) const{ return temp[i].getname();}
double get_mark(const int i, course_map &temp)const{ return temp[i].getmark();} // Functions to return the mark, weight and name of a given course corresponding to inputed course code
int getid()const{return studentid;}
int getyears()const{return years;}
void show_record()const;
void print_record( string *filename) const;
void degree_class()const;
void insert_class();
// Function to insert record into map
public:
Physics():Record(){degree_name= "Physics ";}
Physics(string name,int Id, int time):Record( name, Id, time){degree_name= "Physics";}
~Physics() {degree_name.erase(0, degree_name.size());}
};
}
#endif
function definitions:
#include <iostream>
#include <string>
#include <utility>
#include <map>
#include <fstream>
#include <vector>
#include "Database_header.h"
using namespace std;
using namespace student_record;
ostream & student_record::operator<< (ostream &os, const Course &c)
{
os<< "Course code" << c.course_Id << " \n Course name: " <<c.course_name << " \n Mark " << c.course_mark <<endl;
return os;
}
// Function to insert classes //
void Physics::insert_class()
{
int courseid;
string coursename;
double mark;
cout << " Enter course code " << endl;
cin >> courseid;
cout << " \n Enter course name " << endl;
cin >> coursename;
cout << " \n Enter mark achieved " << endl;
cin >> mark;
Course temp (coursename, mark, courseid);
modules.insert(pair<int, Course>(courseid, temp));
}
void Physics::searchdatabase(course_map &courses, int coursecode) const // Function to search for specific course mark based on course code, need to modify this!!!!
//takes in a map as its argument, although i suppose can use student.modules?
{
course_map::iterator coursesIter;
coursesIter=courses.find(coursecode);
if(coursesIter != courses.end())
{
cout << " Course Code " <<
coursecode << " corresponds to " <<
coursesIter ->second << endl;
}
else { cout << " Sorry, course not found " << endl; }
}
void Physics::print_record( string *filename) const // Function for printing record to the file
{
ofstream myoutputfile;
myoutputfile.open(*filename,ios::app);
if(!myoutputfile.good())
{
// Print error message and exit
cerr<<"Error: file could not be opened"<<endl;
}
if(myoutputfile.good())
{
myoutputfile << "Student name: " << student_name << endl
<< "\n Student ID: " << studentid << endl
<< "\n Year: " << years << endl;
course_map::iterator modulesiter; // Iterator to print out courses using overloaded << function (I think?)
for(modulesiter==modules.begin();modulesiter!=modules.end();modulesiter++)
{
myoutputfile<<modulesiter->second << endl;
}
}
}
void Physics::show_record() const // Function for showing specific student record on screen ( with iterator for map of courses)
{
cout << "Student name: " << student_name;
cout << "\n Student ID: " << studentid;
cout << "\n Years on course: " << years;
cout << "\n Courses and grades: ";
course_map::iterator modulesiter; // Iterator to print out courses using overloaded << function (I think?)
for(modulesiter==modules.begin();modulesiter!=modules.end();modulesiter++)
{
cout<<modulesiter->second << endl;
}
}
void Physics::degree_class()const
{
double temp;
vector<double> dynarr; // Create a vector array to store the grades extracted from the course map for each student
course_map::iterator modulesiter;
for(modulesiter==modules.begin();modulesiter!=modules.end();modulesiter++) // Iterate through map and push values into each vector
{
Course ghost;
ghost=modulesiter->second;
dynarr.push_back(ghost.getmark());
}
double sum(0);
for(int i(0);i<=dynarr.size();i++)
{
sum+=dynarr[i];
}
temp=sum/dynarr.size();
if( temp>=40 && temp <=49.9)
{
cout << "The student has achieved a 3rd class degree with an average of: \n "
<< temp;
}
else if( temp>=50 && temp <=59.9)
{
cout << "The student has achieved a 2:2 degree with an average of: \n "
<< temp;
}
else if( temp>=60 && temp <=69.9)
{
cout << "The student has achieved a 2:1 degree with an average of: \n "
<< temp;
}
else if( temp>=70)
{
cout << "The student has achieved a 1st class degree with an average of: \n "
<< temp;
}
else { cout << "The student has failed the degree " << endl;}
}
and main cpp file:
#include <iostream>
#include <utility>
#include <map>
#include <iomanip>
#include <vector>
#include "Database_header.h"
#include <fstream>
using namespace std;
using namespace student_record;
void main()
{
// Create map to store students with ID keys //
string full_name;
int id;
int time;
string degree_name;
vector<Record*> prec;
// Vector of base class pointers to store all the different records first. No need to specify length as it is a vector! (Advantage over dynamic array?)
char student_test('y'); // Condition for adding students to the record //
int q(0);
while (student_test=='y' || student_test=='Y')
{
// Counter for while loop
cout<< " \n Please enter the student name " << endl;
getline(cin, full_name);
// Enter student name, check it is a string? //
cout<< "\n Please enter student ID " << endl;
cin >> id;
// Check if not integer or number, if not need error message //
cout << "\n Please enter the number of years on the course " << endl;
cin >> time;
// Check if not integer or number, if not need error message //
cout<< "\n Please enter degree type " << endl;
cin>>degree_name;
if(degree_name=="Physics" || degree_name=="physics") // create object of appropriate derived class ( Physics, Chem, Maths, Bio)
{
prec.push_back(new Physics(full_name, id, time));
}
char class_test('y'); // test condition for class insertion loop
while(class_test=='y') // Add courses+marks into course map
{
cout << " \n Add classes to student record " << endl;
prec[q]->insert_class();
cout << " \n Add another class? Y/N" << endl;
cin>>class_test;
}
cout << "Enter another student? Y/N " << endl;
cin >> student_test;
if(student_test=='N' && student_test=='n')
{
cout << "\n Thank you for using the student database, Goodbye !" << endl;
}
q++; // increment counter, to keep track of of vectors of base class pointers, and also be able to output number of students
}
// Next insert all records into map //
typedef map<int, Record*> studentlist;
studentlist studentmap;
for(int i(0); i<=prec.size(); i++)
{
studentmap.insert(pair<int, Record*> (prec[i]->getid(), prec[i]));
}
}
Thanks so much!
for(int i(0); i<=prec.size(); i++)
{
studentmap.insert(pair<int, Record*> (prec[i]->getid(), prec[i]));
}
Should be i < prec.size() instead of <=