-1

I am new in C++ and writing a program in C++ using array and queue. I have a problem. while i am inserting data in to array there is no problem but when i try to display data it shows no of array elements correctly but not show data it shows empty. Please help me about this. whether i declaring array in wrong way or any thing else here my both functions below. thanks in advance

    #include <iostream>
#include<conio.h>
#include<stdlib.h>
#include<Windows.h>
#include<string>
#include<bits/stdc++.h>
#define MAX_SIZE 100

using namespace std;

class Candidate {
private:
    int roll_no, i;
    string name,last_degree;
    char gender;
    int arr_roll[MAX_SIZE];
    string arr_name[MAX_SIZE];
    string arr_last_degree[MAX_SIZE];
    char arr_gender[MAX_SIZE];
    int rear;
    int front;

public:

    Candidate() {
        rear = 0;
        front = 0;
    }

    void insertIntoQ() {
        char a;
        do{

            cout << "\nEnter The Roll No : ";
            cin>>roll_no;
            cout << "\nEnter Name of Student";
            cin>>name;
            cout<< "\n Enter Gender M/F";
            cin>>gender;
            cout<<"\nEnter Last Degree";
            cin>>last_degree;            
            arr_roll[rear++] = roll_no;
            arr_name[rear++] = name;
            arr_gender[rear++] = gender;
            arr_last_degree[rear++] = last_degree;
        cout<<"\n Do you want to continue y/n: ";
        cin>>a;
    }while(a=='y'||a=='Y');    

    }


    void display_invigilator_m() {
        cout << "\n## Queue Size : " << (rear - front);
        for (i = front; i < rear; i++){        
            if(arr_gender[i] == 'm'){
                cout<<"\nRoll No : "<<arr_roll[i];
                cout<<"\nName : "<<arr_name[i];
                cout<<"\nGender : "<<arr_gender[i];
                cout<<"\nLast Degree : "<<arr_last_degree[i];
            }            
        }
    }
};

these functions for insert and display data. i am calling these functions in main function please help me

melpomene
  • 84,125
  • 8
  • 85
  • 148
Nadeem Ijaz
  • 585
  • 1
  • 6
  • 14
  • 1
    [Why should I not `#include `?](https://stackoverflow.com/q/31816095/1848654) – melpomene May 29 '18 at 05:18
  • Are you sure the number of array elements is correct? In your code for every entry you incremented rear 4 times... – Edy May 29 '18 at 05:26
  • Please [edit] your code to reduce it to a [mcve] of your problem. Your current code includes much that is peripheral to your problem - a minimal sample normally looks similar to a good unit test: only performing one task, with input values specified for reproducibility. – Toby Speight May 31 '18 at 08:55

2 Answers2

1

You should increase rear once per iteration, not on every assignment.

The following:

        arr_roll[rear++] = roll_no;
        arr_name[rear++] = name;
        arr_gender[rear++] = gender;
        arr_last_degree[rear++] = last_degree;

should be:

        arr_roll[rear] = roll_no;
        arr_name[rear] = name;
        arr_gender[rear] = gender;
        arr_last_degree[rear] = last_degree;
        ++rear;

(or equivalent.)

NPE
  • 486,780
  • 108
  • 951
  • 1,012
1

If you are into learning C++ I suggest few improvements to your code.

1st use C++ containers, such as std::vector or std:queue instead of C style arrays (You are using an old approach).

2nd you should take advantage of object-oriented better. a Candidate should be a single candidate, a your queue should be a queue of candidates. A good OOP design is the buiding block of your program.

Here is an example:

#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();
}
Nir
  • 1,608
  • 1
  • 17
  • 29