my programi s compiling but its not right result. i suppose to display stu recor
ID: 3542289 • Letter: M
Question
my programi s compiling but its not right result. i suppose to display stu records ,pushedall records and poped list also has to remaining student records .......but not sure why? 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;
}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:"<<endl;
getline(cin,s.name);
cout<<"Enter the Student Id: "<<endl;
cin>>s.id;
cout<<"Enter the Address: "<<endl;
getline(cin,s.address);
cout<<"Enter the GPA: "<<endl;
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()
{
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
#include<iostream>
#include"hw2.h"
#include<string>
using namespace std;
int main()
{
Student student[20];
int i;
for(i=0; i<20;i++)
{
student[i].read();
student[i].push(i);
student[i].pop();
student[i].print();
}
return 0;
}
Explanation / Answer
please rate - thanks
I'm only posting the header fiile since that's all that needs changing
the problem
when you input a number the whitespace remains in the input buffer, so if you
input a string immediately after with a getline it stops an a whitespace, which is the first thing it
finds
the solution is to flush the input buffer, after using a cin, followed by a getline (ignore everything in the buffer up to and including the newline)
any question, ask
the solution is to
#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:"<<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(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
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.