C++ why does my array not pop into my stack? - c++

I created an array for studentrecords and am supposed to pop it into my stack.. well everything works except for my stack.pops and stack.pushes in MAIN...I am so close to finishing this program I am wondering if anyone knows any solutions?
#include <iostream>
#include <list>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <string>
using namespace std;
class Studentrecords
{
private:
struct student
{
string name;
string address;
int ID;
double gpa;
};
student *stackArray;
int stackSize;
int top;
public:
Studentrecords();
Studentrecords(int size);
~Studentrecords();
void push(string name, string address, int id, double gpa);
void pop();
bool isFull() const;
bool isEmpty() const;
void display();
};
Studentrecords::Studentrecords(int size)
{
stackArray = new student[size];
top = -1;
}
Studentrecords::Studentrecords()
{
stackSize = 20;
stackArray = new student[stackSize];
top = -1;
}
Studentrecords::~Studentrecords()
{
delete [] stackArray;
}
void Studentrecords::push (string name, string address, int id, double gpa)
{
if (isFull())
{
cout << "The stack is full!" << endl;
}
else
{
student newStudent;
newStudent.name = name;
newStudent.address= address;
newStudent.ID = id;
newStudent.gpa = gpa;
stackArray[top] = newStudent;
top++;
}
}
void Studentrecords::pop ()
{
if (isEmpty())
{
cout << "The stack is empty!" << endl;
}
else
{
cout << stackArray[top-1].name << endl;
cout << stackArray[top-1].address << endl;
cout << stackArray[top-1].ID << endl;
cout << stackArray[top-1].gpa << endl;
top--;
}
}
bool Studentrecords::isFull() const
{
bool status;
if (top == stackSize - 1)
status = true;
else
status = false;
return status;
}
bool Studentrecords::isEmpty() const
{
bool status;
if (top == -1)
status = true;
else
status = false;
return status;
}
void Studentrecords::display()
{
for (int i = 0; i< top; i++)
{
cout << stackArray[i].name << endl;
cout << stackArray[i].address << endl;
cout << stackArray[i].ID << endl;
cout << stackArray[i].gpa << endl << endl;
}
}
int main()
{
int catchVar;
Studentrecords stack();
cout << "Pushing 1st";
stack.push("Jonny", "123 ave", 2343, 3.2);
cout << "pushing 2nd";
stack.push("Robby", "123 ave", 2343, 3.2);
cout << "Popping ";
stack.pop(catchVar);
cout << catchVar << endl;
cout << "Popping ";
stack.pop(catchVar);
cout << catchVar << endl;
return 0;
}

Studentrecords stack();
Does not declare a Studentrecords named stack, it declares a function named stack that returns a Studentrecords. Change it to
Studentrecords stack;
Also your class needs at least a copy constructor and assignment operator.

Can you post the compiler's error?
Or the produced output VS the expected output?
Without that, I'll have to say that your pop function doesn't take arguments and you are passing it catchVar... that would be a compiler error.

Related

How can I implement a linked list and allow users to choose a specific node to remove with C++?

I've spent pretty much all day trying to do this, I understand pointers and what a linked list does, but I don't know how to actually code it and all I've found are Java and C examples which don't help because I'm using C++.
Thanks in advance for taking a look at my code and helping me, I really appreciate it considering how many days I've spent stressed out and confused about this. I'm not going to lie so far most of my deleteNode function is probably trash. But like I've said I'm just lost I don't even know where to start or how to progress because I only understand the concepts.
This is my data file.
John Doe 80
Jane Smith 70
Bill Jones 50
Pat Hughes 90
Sam Sosa 40
This is my Header file
#include<string>
using namespace std;
class student {
public:
student(); // constructor method
void st_fn(string fn);
string st_fn();
void st_ln(string ln);
string st_ln();
void st_score(float s);
float st_score();
string st_pass_or_fail();
// need a pointer to the next object
student *nxt_ptr;
protected: // protected can be inherited
float m_score;
string m_ln;
string m_fn;
string m_pf_msg;
};
student::student() //constructor
{
nxt_ptr = NULL;
m_score = 0;
}
void student::st_fn(string fn)
{
m_fn = fn;
}
string student::st_fn()
{
return m_fn;
}
void student::st_ln(string ln)
{
m_ln = ln;
}
string student::st_ln()
{
return m_ln;
}
void student::st_score(float s)
{
m_score = s;
}
float student::st_score()
{
return m_score;
}
string student::st_pass_or_fail()
{
if (m_score >= 60)
m_pf_msg = "PASSED";
else
m_pf_msg = "FAILED";
return m_pf_msg;
}
This is my .cpp file.
#include<iostream>
#include<string>
#include<fstream>
#include "Header.h"
using namespace std;
int display_menu()
{
int option;
cout << endl << endl;
cout << "1. Display List" << endl;
cout << "2. Add a student" << endl;
cout << "3. Delete first student" << endl;
cout << "4. Search by Last Name" << endl;
cout << "5. Exit" << endl;
cin >> option;
return option;
}
student * search_last_name(student *h)
{
student *f = NULL;
student *t = NULL;
string s_ln;
// prompt for last name to search for
cout << "Enter Last Name of the Student";
cin >> s_ln;
if (h != NULL)
{
t = h;
while (t != NULL)
{
if (t->st_ln() == s_ln)
f = t; // found the last name so save t
t = t->nxt_ptr;
}
}
else
cout << "List is empty" << endl;
return f;
}
void add_student(student *&head) // h is the head of the list
{
student *new_st, *r;
string fn, ln;
float s;
cout << "Enter new students first name, last name and score";
cin >> fn >> ln >> s;
// instantiate a new node, use new_st
new_st = new student;
new_st->st_fn(fn);
new_st->st_ln(ln);
new_st->st_score(s);
if (head == NULL)
head = new_st;
else
{
// find the last node, use r for this
// write code
r = head;
while (r->nxt_ptr != nullptr)
r = r->nxt_ptr;
// add to the back of the list
// write code
r->nxt_ptr = new_st;
} // end of else
} // end of add student
student * delete_front(student * head)
{
student *t;
// delete front node
// check for empty list
if (head != NULL)
{
// delete first node
t = head;
head = head->nxt_ptr;
delete(t);
}
else
cout << "List is empty - nothing to delete" << endl;
return head;
}
void deleteNode(struct Node *head, struct Node *n)
{
// When node to be deleted is head node
if (head == n)
{
n = head->next;
//Remove the link of next node
head->next = head->next->next;
return;
}
//When not first node, folow the normal deletion process
//Find the previous node
struct Node *prev = head;
while (prev->next != NULL && prev->next != n)
prev = prev->next;
// Check if node really exists in Linked List
if (prev->next == NULL)
{
cout << endl << "Given node is not present in Linked List";
return;
}
//Remove node from linked list should it exist
prev->next = prev->next->next;
return;
}
void display_list(student *t)
{
if (t == NULL)
cout << "List is Empty!";
else
{
while (t != NULL)
{
cout << "******Student******" << endl;
cout << t->st_ln() << endl;
cout << t->st_fn() << endl;
cout << t->st_score() << endl << endl;
t = t->nxt_ptr;
}
}
}
int main()
{
string ln, fn;
float s;
int n;
ifstream infile;
student *head = NULL; //pointer to a student object
student *cp = NULL; // pointer to end of the list
student *new_st = NULL; // pointer to a new student object
student *f = NULL; // pointer to found node
int option; // the numbe of menu item the user selects
infile.open("lab8d.txt");
while (!infile.eof())
{
infile >> fn >> ln >> s;
//instantiate a student object
new_st = new student;
//load the object with data
new_st->st_fn(fn);
new_st->st_ln(ln);
new_st->st_score(s);
// check for empty list - its a special case
if (head == NULL)
{
head = new_st;
cp = new_st;
}
else // list is not empty
{
cp->nxt_ptr = new_st;
cp = new_st;
}
} // end of loop
// loop to give the user some options
option = display_menu();
while (option != 5)
{
if (option == 1)
display_list(head);
else if (option == 2)
add_student(head);
else if (option == 3)
head = delete_front(head);
else if (option == 4)
{
f = search_last_name(head);
if (f != NULL)
{
cout << f->st_fn() << endl;
cout << f->st_ln() << endl;
cout << f->st_score() << endl;
cout << f->st_pass_or_fail() << endl;
}
else
cout << "Name not in the list" << endl;
}
else if (option == 6)
{
cout << "Enter the number of the node you would like to delete: " << endl;
cin >> n;
}
option = display_menu();
}
system("pause");
return 0;
}
The simpler and self-contained your functions are, the easier they are to test and debug, and the less like they are to fail.
Try something more like this:
Student.h:
#include <string>
class student_list;
class student
{
public:
student();
student(const std::string &fn, const std::string &ln, float s, student *nxt = NULL);
void next(student *s);
student* next() const;
void firstName(const std::string &fn);
std::string firstName() const;
void lastName(const std::string &ln);
std::string lastName() const;
void score(float s);
float score() const;
std::string pass_or_fail() const;
void display(bool showPassOrFail = false) const;
friend class student_list;
protected: // protected can be inherited
student *m_next;
std::string m_fn;
std::string m_ln;
float m_score;
};
Student.cpp:
#include "Student.h"
#include <iostream>
student::student()
: m_next(NULL), m_score(0)
{
}
student::student(const std::string &fn, const std::string &ln, float s, student *nxt)
: m_next(nxt), m_fn(fn), m_ln(ln), m_score(s)
{
}
void student::next(student *s)
{
m_next = s;
}
student* student::next() const
{
return m_next;
}
void student::firstName(const std::string &fn)
{
m_fn = fn;
}
std::string student::firstName() const
{
return m_fn;
}
void student::lastName(const std::string &ln)
{
m_ln = ln;
}
std::string student::lastName() const
{
return m_ln;
}
void student::score(float s)
{
m_score = s;
}
float student::score() const
{
return m_score;
}
std::string student::pass_or_fail() const
{
if (m_score >= 60)
return "PASSED";
else
return "FAILED";
}
void student::display(bool showPassOrFail) const
{
std::cout << lastName() << std::endl;
std::cout << firstName() << std::endl;
std::cout << score() << std::endl;
if (showPassOrFail)
std::cout << pass_or_fail() << std::endl;
}
StudentList.h:
#include <string>
class student;
class student_list
{
public:
student_list();
~student_list();
student* search_last_name(const std::string &ln);
student* insert(const std::string &fn, const std::string &ln, float s, student* after = NULL);
bool remove(int idx);
void display() const;
private:
student *m_head;
};
StudentList.cpp:
student_list::student_list()
: m_head(NULL)
{
}
student_list::~student_list()
{
while (m_head)
remove(0);
}
student* student_list::search_last_name(const std::string &ln)
{
student *t = m_head;
while (t)
{
if (t->lastName() == ln)
break; // found the last name
t = t->next();
}
return t;
}
student* student_list::insert(const std::string &fn, const std::string &ln, float s, student* after)
{
student **r;
if (after)
{
// add to list after the specified node
// get pointer to next node
r = &(after->m_next);
}
else
{
// add to the back of the list
// find pointer to last node
r = &m_head;
while (*r)
r = &((*r)->m_next);
}
// instantiate a new node
student *new_st = new student(fn, ln, s);
if (after)
new_st->next(*r);
// add to list
*r = new_st;
return new_st;
}
bool student_list::remove(int idx)
{
// delete a node at index
// find pointer to node
student **r = &m_head;
while ((*r) && (idx-- > 0))
r = &((*r)->m_next);
if (*r)
{
// delete node
student *t = *r;
*r = t->next();
delete t;
return true;
}
return false;
}
void student_list::display() const
{
student *t = m_head;
if (!t)
std::cout << "List is Empty!" << std::endl;
else
{
do
{
std::cout << "******Student******" << std::endl;
t->display();
t = t->next();
}
while (t);
}
}
Main.cpp:
#include "Student.h"
#include "StudentList.h"
#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
#include <limits>
#include <cstdlib>
int read_number()
{
int value;
while (!(std::cin >> value))
{
std::cout << "Must be a number, try again: ";
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}
return value;
}
int display_menu()
{
int option;
std::cout << std::endl << std::endl;
std::cout << "1. Display List" << std::endl;
std::cout << "2. Add a student" << std::endl;
std::cout << "3. Delete first student" << std::endl;
std::cout << "4. Search by Last Name" << std::endl;
std::cout << "5. Delete student by index" << std::endl;
std::cout << "6. Exit" << std::endl << std::endl;
std::cout << "Choice: ";
do
{
option = read_number();
if ((option >= 1) && (option <= 6))
break;
std::cout << "Must be 1..6, try again: ";
}
while (true);
return (option != 6) ? option : -1;
}
int main()
{
student_list students;
std::ifstream infile("lab8d.txt");
if (infile.is_open())
{
student *cp = NULL;
std::string ln, fn;
float s;
while (infile >> fn >> ln >> s)
cp = students.insert(fn, ln, s, cp);
infile.close();
}
// loop to give the user some options
int option; // the number of menu item the user selects
while ((option = display_menu()) != -1)
{
switch (option)
{
case 1:
{
students.display();
break;
}
case 2:
{
// prompt for student info
std::string info;
std::cout << "Enter new student's first name, last name, and score: ";
std::string ln, fn;
float s;
if (std::cin >> fn >> ln >> s)
students.insert(fn, ln, s);
break;
}
case 3:
{
if (!students.remove(0))
std::cout << "List is empty" << std::endl;
break;
}
case 4:
{
// prompt for last name to search for
std::string ln;
std::cout << "Enter Last Name of the Student: ";
if (std::cin >> ln)
{
student *f = students.search_last_name(ln);
if (f)
f->display(true);
else
std::cout << "Name not found in List" << std::endl;
}
break;
}
case 5:
{
std::cout << "Enter the index of the Student to Delete: " << std::endl;
int idx = read_number();
if (!students.remove(idx))
std::cout << "Index not found in List" << std::endl;
break;
}
}
}
std::system("pause");
return 0;
}
That being said, you really should be using the std::list container instead, eg:
Student.h:
#include <string>
class student_list;
class student
{
public:
student();
student(const std::string &fn, const std::string &ln, float s);
void firstName(const std::string &fn);
std::string firstName() const;
void lastName(const std::string &ln);
std::string lastName() const;
void score(float s);
float score() const;
std::string pass_or_fail() const;
void display(bool showPassOrFail = false) const;
friend class student_list;
protected: // protected can be inherited
std::string m_fn;
std::string m_ln;
float m_score;
};
Student.cpp:
#include "Student.h"
#include <iostream>
student::student()
: m_score(0)
{
}
student::student(const std::string &fn, const std::string &ln, float s)
: m_fn(fn), m_ln(ln), m_score(s)
{
}
void student::firstName(const std::string &fn)
{
m_fn = fn;
}
std::string student::firstName() const
{
return m_fn;
}
void student::lastName(const std::string &ln)
{
m_ln = ln;
}
std::string student::lastName() const
{
return m_ln;
}
void student::score(float s)
{
m_score = s;
}
float student::score() const
{
return m_score;
}
std::string student::pass_or_fail() const
{
if (m_score >= 60)
return "PASSED";
else
return "FAILED";
}
void student::display(bool showPassOrFail) const
{
std::cout << lastName() << std::endl;
std::cout << firstName() << std::endl;
std::cout << score() << std::endl;
if (showPassOrFail)
std::cout << pass_or_fail() << std::endl;
}
Main.cpp:
#include "Student.h"
#include <list>
#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
#include <limits>
#include <algorithm>
#include <cstdlib>
int read_number()
{
int value;
while (!(std::cin >> value))
{
std::cout << "Must be a number, try again: ";
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}
return value;
}
int display_menu()
{
int option;
std::cout << std::endl << std::endl;
std::cout << "1. Display List" << std::endl;
std::cout << "2. Add a student" << std::endl;
std::cout << "3. Delete first student" << std::endl;
std::cout << "4. Search by Last Name" << std::endl;
std::cout << "5. Delete student by index" << std::endl;
std::cout << "6. Exit" << std::endl << std::endl;
std::cout << "Choice: ";
do
{
option = read_number();
if ((option >= 1) && (option <= 6))
break;
std::cout << "Must be 1..6, try again: ";
}
while (true);
return (option != 6) ? option : -1;
}
int main()
{
std::list<student> students;
std::ifstream infile("lab8d.txt");
if (infile.is_open())
{
std::string ln, fn;
float s;
while (infile >> fn >> ln >> s)
students.push_back(student(fn, ln, s));
infile.close();
}
// loop to give the user some options
int option; // the number of menu item the user selects
while ((option = display_menu()) != -1)
{
switch (option)
{
case 1:
{
if (students.empty())
std::cout << "List is Empty!" << std::endl;
else
{
for(const auto &s : students)
{
std::cout << "******Student******" << std::endl;
s.display();
}
}
break;
}
case 2:
{
// prompt for student info
std::string info;
std::cout << "Enter new student's first name, last name, and score: ";
std::string ln, fn;
float s;
if (std::cin >> fn >> ln >> s)
students.push_back(student(fn, ln, s));
break;
}
case 3:
{
if (students.empty())
std::cout << "List is empty" << std::endl;
else
students.erase(students.begin());
break;
}
case 4:
{
// prompt for last name to search for
std::string ln;
std::cout << "Enter Last Name of the Student: ";
if (std::cin >> ln)
{
auto f = std::find(students.begin(), students.end(),
[&](const student &s){ return (s.lastName() == ln); }
);
if (f != students.end())
f->display(true);
else
std::cout << "Name not found in List" << std::endl;
}
break;
}
case 5:
{
std::cout << "Enter the index of the Student to Delete: " << std::endl;
int idx = read_number();
if ((idx < 0) || (idx >= students.size()))
std::cout << "Index not found in List" << std::endl;
else
students.erase(std::next(students.begin(), idx));
break;
}
}
}
std::system("pause");
return 0;
}

Doubly Linked List c++ implementing with a Class

I am trying to create a doubly linked list:
class DblLinkedBag
{
private:
struct node{
string data;
node* next;
node* prev;
}*start=NULL;
int itemCount;
string item;
node *head;
node *tail;
public:
DblLinkedBag();
~DblLinkedBag();
int getCurrentSize();
bool isEmpty();
string add(string value);
bool remove(string item);
void clear();
bool contains(string target);
int getFrequencyOf();
string toVector();
string getItem();
void display();
};
So far, I have gotten add, isEmpty, getCurrentSize and clear to work. Now I just need the rest to work and I am having a hard time. My professor gave us a requirement that the class had to be implemented like this:
#include <iostream>
#include <string>
#include "LinkedBag.h"
using namespace std;
void displayBag(LinkedBag<string>& bag)
{
cout << "The bag contains " << bag.getCurrentSize()
<< " items:" << endl;
vector<string> bagItems = bag.toVector();
int numberOfEntries = (int) bagItems.size();
for (int i = 0; i < numberOfEntries; i++)
{
cout << bagItems[i] << " ";
} // end for
cout << endl << endl;
} // end displayBag
void copyConstructorTester()
{
LinkedBag<string> bag;
string items[] = {"zero", "one", "two", "three", "four", "five"};
for (int i = 0; i < 6; i++)
{
cout << "Adding " << items[i] << endl;
bool success = bag.add(items[i]);
if (!success)
cout << "Failed to add " << items[i] << " to the bag." << endl;
}
displayBag(bag);
LinkedBag<string> copyOfBag(bag);
cout << "Copy of bag: ";
displayBag(copyOfBag);
cout << "The copied bag: ";
displayBag(bag);
} // end copyConstructorTester
void bagTester()
{
LinkedBag<string> bag;
cout << "Testing the Link-Based Bag:" << endl;
cout << "isEmpty: returns " << bag.isEmpty()
<< "; should be 1 (true)" << endl;
displayBag(bag);
string items[] = {"one", "two", "three", "four", "five", "one"};
cout << "Add 6 items to the bag: " << endl;
for (int i = 0; i < 6; i++)
{
bag.add(items[i]);
} // end for
displayBag(bag);
cout << "isEmpty: returns " << bag.isEmpty()
<< "; should be 0 (false)" << endl;
cout << "getCurrentSize: returns " << bag.getCurrentSize()
<< "; should be 6" << endl;
cout << "Try to add another entry: add(\"extra\") returns "
<< bag.add("extra") << endl;
cout << "contains(\"three\"): returns " << bag.contains("three")
<< "; should be 1 (true)" << endl;
cout << "contains(\"ten\"): returns " << bag.contains("ten")
<< "; should be 0 (false)" << endl;
cout << "getFrequencyOf(\"one\"): returns "
<< bag.getFrequencyOf("one") << " should be 2" << endl;
cout << "remove(\"one\"): returns " << bag.remove("one")
<< "; should be 1 (true)" << endl;
cout << "getFrequencyOf(\"one\"): returns "
<< bag.getFrequencyOf("one") << " should be 1" << endl;
cout << "remove(\"one\"): returns " << bag.remove("one")
<< "; should be 1 (true)" << endl;
cout << "remove(\"one\"): returns " << bag.remove("one")
<< "; should be 0 (false)" << endl;
cout << endl;
displayBag(bag);
cout << "After clearing the bag, ";
bag.clear();
cout << "isEmpty: returns " << bag.isEmpty()
<< "; should be 1 (true)" << endl;
} // end bagTester
int main()
{
copyConstructorTester();
bagTester();
return 0;
} // end main
So far this is what I have.
#include <iostream>
#include <string>
#include <vector>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
class DblLinkedBag
{
private:
struct node{
string data;
node* next;
node* prev;
}*start=NULL;
int itemCount;
string item;
node *head;
node *tail;
public:
DblLinkedBag();
~DblLinkedBag();
int getCurrentSize();
bool isEmpty();
string add(string value);
bool remove(string item);
void clear();
bool contains(string target);
int getFrequencyOf();
string toVector();
string getItem();
void display();
};
DblLinkedBag::DblLinkedBag()
{
itemCount=0;
item;
}
string DblLinkedBag::add(string value)
{
node* n;
cout<<itemCount<<endl;
if(itemCount==0)
{
n=new node;
n->data=value;
n->prev=NULL;
head=n;
tail=n;
}
if(itemCount>0 && itemCount<7)
{
n= new node;
n->data=value;
n->prev=tail;
tail->next=n;
tail=n;
}
itemCount++;
return value;
}
void DblLinkedBag::display()
{
struct node* temp=start;
while(temp != NULL)
{
cout<<temp->data<<endl;
temp=temp->next;
}
}
int DblLinkedBag::getCurrentSize()
{
return itemCount;
}
bool DblLinkedBag::contains(string target)
{
//need help here, supposed to tell if the linked list contains a certain //string
bool found= false;
node* curPtr=start;
int i=0;
while (!found && (curPtr!=NULL)&& (i<itemCount))
{
if(target==curPtr->data)
{
found=true;
}
else
{
i++;
curPtr=curPtr->next;
}
}
return found;
}
bool DblLinkedBag::isEmpty()
{
bool empty;
if (itemCount==0)
{
empty=true;
}
else
empty=false;
return empty;
}
void DblLinkedBag::clear()
{
node* nodeToDelete=start;
while (start != NULL)
{
start=start->next;
nodeToDelete->next=NULL;
delete nodeToDelete;
}
itemCount=0;
}
bool DblLinkedBag::remove(string item)
{
//need help here
}
string DblLinkedBag::toVector()
{
//need help here this is supposed to send the linked list to a vector
vector<string> vct;
node* curPtr= start;
int counter = 0;
while ((curPtr != NULL) && (counter < itemCount))
{
vct.push_back(curPtr->data);
curPtr = curPtr->next;
counter++;
}
}
int DblLinkedBag::getFrequency()
{//supposed to show how many of a certain item are in the linked list
DblLinkedBag::~DblLinkedBag()
{
}
Any help implementing these class functions to create the other functions my professor gave me would be appreciated, I have tried all different kinds of implementations and I cannot seem to figure it out.
First: your method DblLinkedBag::clear has a error, nodeToDelete never change (just deletes first node)
bool DblLinkedBag::remove(string item)
{
node* curPtr=start;
while (curPtr!=NULL)
{
if(item==curPtr->data)
{
if(curPtr->prev) curPtr->prev->next = curPtr->next;
if(curPtr->next) curPtr->next->prev = curPtr->prev;
delete curPtr;
itemCount--;
return true;
}
else
{
curPtr=curPtr->next;
}
}
return false;
}
What do you expect getFrequency() to do?

I cant print the name of my book but can print everything else

My cpp file, for some reason I can print everything else beside the name of the book i think its my main file. I don't get any errors when I build it but when I run it the little window comes ups and goes away
#include "BookRecord.h"
BookRecord::BookRecord()
{
m_sName[128]=NULL;
m_lStockNum=0;
m_iClassification = 0;
m_dCost = 0.0;
m_iCount = 0;
}
BookRecord::BookRecord(char *name, long sn, int cl, double cost)
{
m_iCount=1;
m_sName[128]=*name;
m_lStockNum=sn;
m_iClassification=cl;
m_dCost=cost;
}
BookRecord::~BookRecord()
{
}
void BookRecord::getName(char *name)
{
strcpy(name, m_sName);
}
void BookRecord::setName(char *name)
{
strcpy(m_sName, name);
}
long BookRecord::getStockNum()
{
return m_lStockNum;
}
void BookRecord::setStockNum(long sn)
{
m_lStockNum = sn;
}
void BookRecord::getClassification(int& cl)
{
cl=m_iClassification;
}
void BookRecord::setClassification(int cl)
{
m_iClassification= cl;
}
void BookRecord::getCost(double *c)
{
*c = m_dCost;
}
void BookRecord::setCost(double c)
{
m_dCost = c;
}
int BookRecord::getNumberInStock()
{
return m_iCount;
}
void BookRecord::setNumberInStock(int count)
{
count = m_iCount;
}
void BookRecord::printRecord()
{
//cout << "Name: " <<m_sName <<"\n";
printf("Name: %s", m_sName);
cout<<endl;
cout<<"Stock Number: "<<m_lStockNum<<"\n";
cout<<"Class: " <<m_iClassification<<"\n";
cout<<"Cost: $"<<m_dCost<<"\n";
cout<<"In Stock: "<<m_iCount<<"\n";
}
My main file
#include "BookRecord.h"
int main()
{
char *bName="C Plus Plus";
BookRecord *Ana = new BookRecord(bName, 1, 1, 50.9);
/*char * bookName="";
int clNo;
double c;
Ana->getClassification(clNo);
Ana->getCost(&c);
Ana->getName(bookName);*/
cout << "Printing using printRecord() Function" << endl;
Ana->printRecord();
/*cout << endl << "Printing using getter properties" <<endl;
cout << bookName << endl;
cout<< clNo << endl;
cout << c << endl;
cout << Ana->getNumberInStock() << endl;*/
return 0;
}
Hi I would have done this a bit different, not using char m_sName[128], I would rather use std::string or a pointer and just destroyed it in the desctructor. I base my code here on what you have already requested above, and I'm making it as simple as possible. I know things could be done better here, but here you go:
EDIT: Refactored a bit and question grew.
BookRecord.h
#ifndef __BOOKRECORD_H__
#define __BOOKRECORD_H__
#ifndef MAX_NAME_SIZE
#define MAX_NAME_SIZE 128
#endif
class BookRecord
{
private:
char m_sName[MAX_NAME_SIZE] = {}; // I would have used a pointer or std:string and not char
long m_lStockNum = 0;
int m_iClassification = 0;
double m_dCost = 0.0;
int m_iCount = 0;
public:
BookRecord();
BookRecord(char* name, long sn, int cl, double cost);
~BookRecord();
// EDIT: Your question grew
char* GetName();
void SetName(char *name);
long GetStockNum();
void SetStockNum(long sn);
int GetClassification();
void SetClassification(int cl);
double GetCost();
void SetCost(double c);
int GetNumberInStock();
void SetNumberInStock(int count);
void PrintRecord();
};
#endif
BookRecord.cpp
#include <stdlib.h>
#include <iostream>
#include <stdexcept>
#include "BookRecord.h"
using namespace std;
BookRecord::BookRecord()
{
}
BookRecord::BookRecord(char* name, long sn, int cl, double cost) :
m_iCount(1), m_lStockNum(sn), m_iClassification(cl), m_dCost(cost)
{
if (name == NULL)
throw std::invalid_argument("Name of book is null");
if (strlen(name)>MAX_NAME_SIZE)
throw std::invalid_argument("Name of book is to long");
memset(&m_sName, 0, sizeof(char)*MAX_NAME_SIZE);
memcpy(&m_sName, name, strlen(name)*sizeof(char));
}
BookRecord::~BookRecord()
{
}
// Edit: Question grew
char* BookRecord::GetName()
{
return static_cast<char*>(m_sName);
}
void BookRecord::SetName(char *name)
{
strcpy(m_sName, name); // TODO: handle null, and sizechecks here to your likings
}
long BookRecord::GetStockNum()
{
return m_lStockNum;
}
void BookRecord::SetStockNum(long sn)
{
m_lStockNum = sn;
}
int BookRecord::GetClassification()
{
return m_iClassification;
}
void BookRecord::SetClassification(int cl)
{
m_iClassification = cl;
}
double BookRecord::GetCost()
{
return m_dCost;
}
void BookRecord::SetCost(double c)
{
m_dCost = c;
}
int BookRecord::GetNumberInStock()
{
return m_iCount;
}
void BookRecord::SetNumberInStock(int count)
{
m_iCount = count;
}
void BookRecord::PrintRecord()
{
cout << static_cast<const char*>(m_sName) << endl;
cout << "Stock Number: " << m_lStockNum << endl;
cout << "Class: " << m_iClassification << endl;
cout << "Cost: $" << m_dCost << endl;
cout << "In Stock: " << m_iCount << endl;
}
If you are wondering about the exception throwing have a look at this post.
Hope it helps

Stack of char issue

I'm in the process of learning how to create Stacks and linked lists. The program that I'm writing right now focuses on a template stack class. Everything was going smoothly when I made a stack of int, but my program started crashing when I started implementing a stack of char. To be specific it started messing up when I tried to implement a pop action on the stack of char.
Can you guys please verify that I'm doing this correctly and also let me know what I'm doing wrong with char stack?
Here's my code:
#include<iostream>
using namespace std;
#ifndef STACK_H
#define STACK_H
//STACK CLASS
template<typename T>
class Stack
{
public:
Stack(int = 10);
~Stack(){ delete stackPtr;};
bool isEmpty() const
{ return top == -1; }
bool isFull() const
{ return top == size - 1; }
//push and pop
bool push(const T&);
bool pop(T&);
private:
int size;
int top;
T *stackPtr;
};
//CONSTRUCTOR
template<typename T>
Stack<T>::Stack(int newSize)
: top(-1), size(newSize),
stackPtr(new T[size]) //allocate array using ptr ********
{
//empty constructor
};
//PUSH VALUES ONTO STACK
template<typename T>
bool Stack<T>::push(const T &pushVal)
{
if(!isFull())
{
stackPtr[++top] = pushVal;
return true;
}
return false;
};
//POP VALUES OFF OF STACK
template<typename T>
bool Stack<T>::pop(T &popVal)
{
if(!isEmpty())
{
popVal = stackPtr[top--];
return true;
}
return false;
};
#endif
//DRIVER
int main()
{
//STACK OF INT
Stack<int> intStack(5);
int intValue = 1;
cout << "Pushing values onto intStack: " << endl;
while(intStack.push(intValue))
{
cout << intValue << ' ';
intValue++;
}
cout << "\nStack is full, cannot push..."
<< endl << endl;
cout << "Popping values off of intStack: " << endl;
while(intStack.pop(intValue))
cout << intValue << ' ';
cout << "\nStack is empty, cannot pop..."
<< endl;
//STACK OF CHAR
Stack<char> charStack(5);
string greeting = "hello";
int strSize = greeting.length();
cout << "\nPushing values onto charStack: " << endl;
for(int i = 0; i < strSize; i++)
{
charStack.push(greeting.at(i));
cout << greeting.at(i) << ' ';
}
cout << endl;
cout << "Popping values off of charStack: " << endl;
for(int i = (strSize - 1); i >= 0; i++) //PROBLEM OCCURS
{
charStack.pop(greeting.at(i));
cout << greeting.at(i) << ' ';
}
system("pause");
}
for(int i = (strSize - 1); i >= 0; **i--**) //PROBLEM not anymore
{
charStack.pop(greeting.at(i));
cout << greeting.at(i) << ' ';
}
Probably it's not the source of your particular problem, but you really should use
delete[] stackPtr
instead of delete stackPtr in your destructor. Wikipedia explains why

Disappearing of object inside variable of another object

I got strange program behaviour and crashing.
I created class 'List', with array of pointers to objects of class 'Student'. I observe that I succesfully called 'Student' object in 'List' constructor. But I'm unable to call 'Student' object from any other 'List' methods.
I even checked the same line for testing inside 'List' constructor and 'push' method, resulting in program crash.
Here is testing line: cout << (studentBox[numb] -> getRef()) << endl;
Here is the problematic part of code:
#include <iostream>
using namespace std;
class Student
{
private:
int referenceNumb;
int markNumb;
public:
Student();
Student(int, int);
~Student();
int getRef();
int getMark();
};
class List
{
private:
int numb;
Student* studentBox[1000];
public:
List();
~List();
void push(int, int);
int getAVG();
};
int main()
{
List* base = NULL;
int x, y;
char mod;
do
{
cout << "Waiting for orders." << endl;
cout << "1 - Quit," << endl;
cout << "2 - Add new student," << endl;
cout << "3 - Calculate average mark," << endl;
cout << "0 - Create new list." << endl;
cin >> mod;
switch(mod)
{
case '0': base = new List(); break;
case '1': cout << "Bye."; break;
case '2':
if(base != NULL)
{
cout << "Specify student's reference number: "; cin >> x;
cout << "Specify student's mark: "; cin >> y;
base->push(x, y);
}
else cout << "List does not exist!" << endl;
break;
case '3':
if(base != NULL)
{
cout << "Average mark is equal: " << base->getAVG();
}
else cout << "List does not exist!";
cout << endl;
break;
default: cout << "Correct command required!" << endl; break;
}
}
while(mod!='1');
return 0;
}
Student::Student()
{
referenceNumb = NULL;
markNumb = NULL;
}
Student::Student(int r, int m)
{
referenceNumb = r;
markNumb = m;
}
Student::~Student()
{
referenceNumb = NULL;
markNumb = NULL;
cout << "pusto." << endl;
}
int Student::getRef()
{
return referenceNumb;
}
int Student::getMark()
{
return markNumb;
}
List::List()
{
int numb = 0;
studentBox[numb] = new Student();
cout << (studentBox[numb] -> getRef()) << endl;
}
List::~List()
{
}
void List::push(int x, int y)
{
cout << (studentBox[numb] -> getRef()) << endl;
if(studentBox[numb] != NULL)
{
studentBox[numb] = new Student();
cout << (studentBox[numb] -> getRef()) << endl;
}
else cout << "Hujnia" << endl;
}
int List::getAVG()
{
int temp = 0;
for(int i=0; i<numb; i++)
{
temp += studentBox[i]->getMark();
}
return (temp / numb);
}
There are a couple of major problems that I see. The first is that the numb member of the List isn't initialized.
List::List()
{
int numb = 0; // this creates a new local variable called numb, it doesn't
// initialize the numb member.
studentBox[numb] = new Student();
cout << (studentBox[numb] -> getRef()) << endl;
}
Do this instead:
List::List()
: numb(0) // initialize the numb member to zero.
{
studentBox[numb] = new Student();
cout << (studentBox[numb] -> getRef()) << endl;
}
Another problem is that numb appears to be meant to indicate the size of the list, but it is never changed when a new student is added.