Im trying to push my student object onto my stack from my array of student objec
ID: 3859132 • Letter: I
Question
Im trying to push my student object onto my stack from my array of student objects. When I use the push function i get an error saying cant convert int to a student. How do i fix this problem? Below is my complete project with my main cpp, staticStack.h,student.cpp, and student.h
main cpp:
#include <iostream>
#include "student.cpp"
#include "StaticStack.h"
using namespace std;
int main() {
student StudentRec[15];
staticStack studentStack(15);
StudentRec[0].setName("Mary");
StudentRec[1].setName("James");
StudentRec[2].setName("Adam");
StudentRec[3].setName("Phillip");
StudentRec[4].setName("Haley");
StudentRec[5].setName("Marc");
StudentRec[6].setName("Payton");
StudentRec[7].setName("Rob");
StudentRec[8].setName("Chris");
StudentRec[9].setName("Holly");
StudentRec[10].setName("Morgan");
StudentRec[11].setName("Carson");
StudentRec[12].setName("Zac");
StudentRec[13].setName("Logan");
StudentRec[14].setName("Sarah");
//set GPA of students
StudentRec[0].setTestavg();
StudentRec[1].setTestavg();
StudentRec[2].setTestavg();
StudentRec[3].setTestavg();
StudentRec[4].setTestavg();
StudentRec[5].setTestavg();
StudentRec[6].setTestavg();
StudentRec[7].setTestavg();
StudentRec[8].setTestavg();
StudentRec[9].setTestavg();
StudentRec[10].setTestavg();
StudentRec[11].setTestavg();
StudentRec[12].setTestavg();
StudentRec[13].setTestavg();
StudentRec[14].setTestavg();
//set Address of students
StudentRec[0].setAddress("123 WonderWorld Dr");
StudentRec[1].setAddress("123 Craddock Ave");
StudentRec[2].setAddress("123 Ashley St");
StudentRec[3].setAddress("123 Ranch Rd");
StudentRec[4].setAddress("123 Hughson Dr.");
StudentRec[5].setAddress("123 Hopkins St");
StudentRec[6].setAddress("123 Thorpe Ln");
StudentRec[7].setAddress("123 Guadalupe St");
StudentRec[8].setAddress("123 Frost Ln");
StudentRec[9].setAddress("123 Chisolm St");
StudentRec[10].setAddress("123 Gordon St");
StudentRec[11].setAddress("123 Hunter Rd");
StudentRec[12].setAddress("123 Holland Rd");
StudentRec[13].setAddress("123 Aquarena Rd");
StudentRec[14].setAddress("123 Uhland Rd");
//randomly generated student IDs
for (int i = 0; i < 15; i++) {
StudentRec[i].setId();
}
cout << " " << endl;
cout << "ARRAY LIST OF STUDENT RECORDS:";
cout << endl;
cout << endl;
for (int i = 0; i < 15; i++) {
cout << "Student Name: " << StudentRec[i].getName() << " " << "Test Avg: "
<< StudentRec[i].getTestavg() << " " << "Address: "
<< StudentRec[i].getAddress() << " " << "ID: "
<< StudentRec[i].getId() << endl;
}
cout << StudentRec[3].getTestavg();
for (int i = 0; i < studentStack.stackSize; i++){
studentStack.push(StudentRec[i]);
}
return 0;
}
/////////////////////////////////////////////////////////
StaticStack.h:
#ifndef STATICSTACK_H_
#define STATICSTACK_H_
#include "student.h"
using namespace std;
class staticStack
{
private:
int *stackArray;
public:
int stackSize;
int top;
//Constructor
staticStack(int);
// Copy constructor
staticStack(const staticStack &);
//Destructor
~staticStack();
//stack operations
void push (student);
void pop (int &);
bool isFull() const;
bool isEmpty() const;
};
staticStack::staticStack(int size){
stackArray = new int[size];
stackSize = size;
top = -1;
}
staticStack::staticStack(const staticStack &obj){
//create the stack array
if(obj.stackSize > 0)
stackArray = new int[obj.stackSize];
else
stackArray = nullptr;
//copy the stackSize atribute.
stackSize = obj.stackSize;
//Copythe stack contents
for (int count = 0; count < stackSize; count++){
stackArray[count] = obj.stackArray[count];
}
//set the top of the stack
top = obj.top;
}
//destructor
staticStack::~staticStack(){
delete [] stackArray;
}
void staticStack::push(student x){//??
if (isFull()){
cout << "The stack is full. ";
}
else{
top++;
stackArray[top]= x ; //??
}
}
void staticStack::pop(int &num){
if (isEmpty()){
cout << "The stack is empty. ";
}
else{
num = stackArray[top];
top--;
}
}
bool staticStack::isFull() const {
bool status;
if (top == stackSize - 1){
status = true;
}else{
status = false;
}
return status;
}
bool staticStack::isEmpty() const{
bool status;
if(top == -1){
status = true;
}else{
status = false;
}
return status;
}
#endif /* STATICSTACK_H_ */
/////////////////////////////////////////////////////////
student.cpp:
#include <iostream>
#include <cstdlib> //For rand and srand
#include <ctime> //For the time function
#include "student.h"
using namespace std;
void student::setName(string name) { //creates student name
sName = name;
}
string student::getName() { //returns student name
return sName;
}
void student::setAddress(string address) { //sets students address
sAddress = address;
}
string student::getAddress() { //returns students address
return sAddress;
}
void student::setId() {
int MIN_VALUE = 1000;
int MAX_VALUE = 9999;
sId = (rand() % (MAX_VALUE - MIN_VALUE + 1)) + MIN_VALUE;
}
int student::getId() {
return sId;
}
//this function creates a test avg for each student
void student::setTestavg() {
int MIN_VALUE = 60;
int MAX_VALUE = 100;
int gradeTotal;
int tests[10];
testScores = tests;
for(int i = 0; i<10;i++){
grade = (rand() % (MAX_VALUE - MIN_VALUE + 1)) + MIN_VALUE;
tests[i] = grade;
}
for (int i = 0; i<10; i++){
gradeTotal += *(testScores + i);
}
avgTest = gradeTotal/10;
}
int student::getTestavg(){
return avgTest;
}
/////////////////////////////////////////////
student.h:
#ifndef STUDENT_H_
#define STUDENT_H_
using namespace std;
class student {
private:
int sId;
string sName, sAddress;
int grade;
int avgTest;
int *testScores;
public:
void setName(string);
string getName();
void setAddress(string);
string getAddress();
void setId();
int getId();
void setTestavg();
int getTestavg();
student(string n,string addr, int average, int id){
sName = n;
sAddress = addr;
avgTest = average;
sId = id;
}
};
#endif /* STUDENT_H_ */
Explanation / Answer
Yes I got the error here..
stackArray[top]= x ;
This is the code in the push() method right.
In the class staticStack, you've defined an array named stackArray as int *stackArray.
So, the type of stackArray is integer.
When you are calling push() method, you are pushing a student object which is referred by 'x' in push() method.
And you're trying to insert it at the top of the stack. But as the stack is defined as it will contain only int elements, you can't enter student class object there.
So, as per the program, I think you're pushing the id of the student on the stack because in pop() only int value is returned, so in push() also int value should be inserted in the stack.
And if you wish to enter complete student object, then define your stackArray as student *stackArray; instead of int *stackArray; in the staticStack class.
So, this will solve the error mentioned over here. Then you'll encounter 1 more error here so let me help you with that also.
The error is related to the constructor.
You've written the constructor as : staticStack(int);
This is the parameterized constructor so if the parameterize constructor is defined, the default constructor must be there. But in class staticStack() there's no default constructor seen. So you can enter it as an empty body constructor so that it won't change any code like...
staticStack : staticStack()
{
}
This will solve another error as well.
Do comment if there is any query. Thank you. :)