Если вы изучаете C ++, я предлагаю несколько улучшений вашего кода.
1-е использование контейнеров C ++, таких как std :: vector или std: queue, вместо массивов в стиле C (вы используете старый подход).
2-й, вы должны лучше воспользоваться объектно-ориентированным.Кандидат должен быть единственным кандидатом, ваша очередь должна быть очередью кандидатов.Хороший дизайн ООП - это строительный блок вашей программы.
Вот пример:
#include <stdlib.h>
#include <iostream>
#include <queue>
#include <string>
using namespace std;
class Candidate
{
public:
Candidate(int roll_no, string name, string last_degree, char gender)
: _roll_no(roll_no), _name(name), _last_degree(last_degree), _gender(gender)
{}
void print()
{
cout << "\nCandidate:";
cout << "\n Roll No : " << _roll_no;
cout << "\n Name : " << _name;
cout << "\n Gender : " << _gender;
cout << "\n Last Degree : " << _last_degree;
}
char getGender() { return _gender; }
private:
int _roll_no;
string _name;
string _last_degree;
char _gender;
};
class App
{
public:
void getCandidatesFromUser()
{
char choice;
do
{
int roll_no;
string name;
char gender;
string last_degree;
cout << "\nEnter The Roll No: ";
cin >> roll_no;
cout << "\nEnter Name of Student: ";
cin >> name;
cout << "\nEnter Gender M/F: ";
cin >> gender;
cout << "\nEnter Last Degree: ";
cin >> last_degree;
// Create candidate instance
Candidate candidate(roll_no, name, last_degree, gender);
// Store in queue
_candidates.push(candidate);
cout << "\n Do you want to continue y/n: ";
cin >> choice;
} while (choice == 'y' || choice == 'Y');
}
void display()
{
cout << "\n## Queue Size : " << _candidates.size();
while (!_candidates.empty())
{
Candidate candidate = _candidates.front();
if (candidate.getGender() == 'm' || candidate.getGender() == 'M')
{
candidate.print();
}
_candidates.pop();
}
}
private:
queue<Candidate> _candidates;
};
int main()
{
App myApp;
myApp.getCandidatesFromUser();
myApp.display();
}