Problems with a sort function :void SortAge(); calling the function to print an employee name : void print(int d), I only recive the name of the first employee i had pushed into the Stack... I don t have any sintax error thank for time
#include <iostream>
#include <cstdio>
using namespace std;
struct employee
{
string name;
int age;
};
class Stack
{
employee employeeList[1000];
int pos =-1;
public:
void push(employee e)
{
pos++;
employeeList[pos] = e;
}
employee pop(int n)
{
employee e;
if(n<= pos && pos >= 0 && n >=0)
{
e= employeeList[pos] ;
for(int j =n; j < pos; j++ )
{
employeeList[pos] = employeeList[pos + 1];
}
pos--;
}
return e;
}
void print(int d)
{
if(d > pos || d <0 || pos < 0)
{
cout<< "Error";
}
cout<<employeeList[d].name<<endl;
}
char menu()
{
char choice;
cout << "Press 1. to push an employee"<<endl;
cout << "Press 2. to pop an employee"<<endl;
cout << "Press 3. to show an employee"<<endl;
cout << "Press 4. to sort the list by age"<<endl;
cin>> choice;
return choice;
}
void SortAge()
{
int j;
for(j = 0;j < pos ; j++)
{
if(employeeList[j].age >employeeList[j+1].age)
{
employee e;
e = employeeList[j];
employeeList[j+1] = employeeList[j];
employeeList[j] = e;
j = 0;
} continue;
}
}
};
int main()
{
Stack s;
int j;
for(j=0;j<1000;j++)
{
char input = s.menu();
switch(input)
{
case '1' :
{
employee e;
cout<<"Enter Name :"<<endl;
cin>>e.name;
cout<<"Enter Age :"<<endl;
cin>>e.age;
s.push(e);
}
break;
case '2' :
{
int n;
cout<<"Enter at witch position you want to delete employee:" <<endl;
cin>>n;
s.pop(n);
}
break;
case '3' :
{
int n;
cout<<"Enter the position of the employee you want to visualize"<<endl;
cin>>n;
s.print(n);
}
break;
case '4' :
{
s.SortAge();
}
break;
}
}
return 0;
}
Aucun commentaire:
Enregistrer un commentaire