I suppose to display records in array , disply push all student records from the
ID: 3542462 • Letter: I
Question
I suppose to display records in array , disply push all student records from the array
alsp display 3stu records from the stack so display the poped list and remining student records .
but didnt play them... well..
so please help me i dont know what is wrong ...
#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;
};
private:
record s;
record *stackArray;
int stackSize;
int top;
public:
Student();
~Student();
void push();
void pop();
bool isFull() const;
bool isEmpty() const;
void read();
void print();
};
Student::Student()
{
stackArray=new record[20];
stackSize=20;
top=-1;
}
Student::~Student()
{
delete [] stackArray;
}
void Student::read()
{
cout<<"Enter the name:"<<endl;
getline(cin,s.name);
cout<<"Enter the Student Id: "<<endl;
cin>>s.id;
cin.ignore(20,' ');
cout<<"Enter the Address: "<<endl;
getline(cin,s.address);
cout<<"Enter the GPA: "<<endl;
cin>>s.gpa;
cin.ignore(20,' ');
}
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()
{
if(isFull())
{
cout<<"THe stack is full.";
}
else
{
top++;
stackArray[top]=s;
}
}
void Student::pop()
{
string nam;
string home;
double grade;
int num;
if(isEmpty())
{
cout<<"The stack is empty.";
}
else
{
s=stackArray[top];
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
#include<iostream>
#include"hw2.h"
#include<string>
using namespace std;
int main()
{
Student student;
int i;
for(i=0;i<20;i++)
{
student.read();
student.push();
student.print();
}
for(int j=0;j<5;j++)
{
student.pop();
student.print();
}
student.print();
system("PAUSE");
return 0;
}
Explanation / Answer
cout<<"Enter the GPA: "<<endl;
cin>>s.gpa;
cin.ignore(20,' ');
}
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()
{
string nam;
string home;
double grade;
int num;
if(isEmpty())
{
cout<<"The stack is empty.";
}
else
{
num=stackArray[top];
home=stackArray[top];
grade=stackArray[top];
num=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