this is my code and I dont know what is problem. i have to write student records
ID: 3542279 • Letter: T
Question
this is my code and I dont know what is problem. i have to write student records with array of stack...!! help me please..~
#ifndef HW2_H
#define HW2_H
#include<iostream>
#include<string>
using namespace std;
class student
{
private:
struct record
{
double id;
string name;
string address;
double gpa;
};
int *stackArray;
int stackSize;
int top;
public:
student(int);
~student();
void push(int);
void pop(int&);
bool isFull() const;
bool isEmpty() const;
void read();
void print();
};
student::student(int size)
{
stackArray=new int[size];
stackSize=size;
top=-1;
}
student::~student()
{
delete [] stackArray;
}
void student::read()
{
record. s;
cout<<"Enter the name:";
getline(cin,s.name);
cout<<"Enter the Student Id: ";
cin>>id;
cout<<"Enter the Address: ";
getline(cin,address);
cout<<"Enter the GPA: ";
cin>>gpa;
}
void student::print()
{
cout<<"name: "<<name<<endl;
cout<<"ID: "<<id<<endl;
cout<<"address: "<<address<<endl;
cout<<"GPA: "<<gpa<<endl;
}
void student::push(int num)
{
if(isFull())
{
cout<<"THe stack is full.";
}
else
{
top++;
stackArray[top]=num;
}
}
void student::pop(int &num)
{
if(isEmpty()
{
cout<<"The stack is empty."
}
else
{
nam=stackArray[top];
num=stackArray[top];
home=stackArray[top];
grade=stackArray[top];
}
}
bool student::isFull() const
{
bool status;
if(top==stackSize-1)
status=true;
else
status=false;
return status;
}
bool student::isEmpty() const
{
bool status;
if(top==-1)
status=true;
else
status=false;
return status;
}
#endif
int main()
{
Student student[20];
int i;
for(i=0; i<20;i++)
{
student[i].read();
student.push(student[i]);
student.pop();
student.record();
}
return 0;
}
Explanation / Answer
#ifndef HW2_H
#define HW2_H
#include<iostream>
#include<string>
using namespace std;
class Student
{
private:
struct record
{
double id;
string name;
string address;
double gpa;
}s;
int *stackArray;
int stackSize;
int top;
public:
Student();
~Student();
void push(int);
void pop();
bool isFull() const;
bool isEmpty() const;
void read();
void print();
};
Student::Student()
{
stackArray=new int[20];
stackSize=20;
top=-1;
}
Student::~Student()
{
delete [] stackArray;
}
void Student::read()
{
cout<<"Enter the name:";
getline(cin,s.name);
cout<<"Enter the Student Id: ";
cin>>s.id;
cout<<"Enter the Address: ";
getline(cin,s.address);
cout<<"Enter the GPA: ";
cin>>s.gpa;
}
void Student::print()
{
cout<<"name: "<<s.name<<endl;
cout<<"ID: "<<s.id<<endl;
cout<<"address: "<<s.address<<endl;
cout<<"GPA: "<<s.gpa<<endl;
}
void Student::push(int num)
{
if(isFull())
{
cout<<"THe stack is full.";
}
else
{
top++;
stackArray[top]=num;
}
}
void Student::pop()
{
int nam,home,grade,num;
if(isEmpty())
{
cout<<"The stack is empty.";
}
else
{
nam=stackArray[top];
num=stackArray[top];
home=stackArray[top];
grade=stackArray[top];
}
}
bool Student::isFull() const
{
bool status;
if(top==stackSize-1)
status=true;
else
status=false;
return status;
}
bool Student::isEmpty() const
{
bool status;
if(top==-1)
status=true;
else
status=false;
return status;
}
#endif
int main()
{
Student student[20];
int i;
for(i=0; i<20;i++)
{
student[i].read();
student.push(student[i]);
student.pop();
student.record();
}
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.