Question:
problem with this C++ program related tp queues..?
Ice S
2011-01-26 04:06:42 UTC
http://codepad.org/DT9VSqAy

above contains the code of my program.. it is supposed to work as a queue. Please run it and see for yourself what the problem is . I cant really esplain it :(
Three answers:
cja
2011-01-26 06:20:52 UTC
You say you have no compile errors, but you should have gotten a warning for this in your add function:



  if(r==0)

  {f==0;

  }



f==0; is a statement that has no effect. So that's one thing that's probably causing you trouble. I'm not sure your queue handling logic is correct. When you have a full queue then delete the entry at the front, you should have room for one more entry, but it doesn't look like your logic supports that. You should make your queue circular.



Reformatting for readability, and to make it more C++-ish, and to make the queue circular, here's my update to your code:



#include

#include

#include

#include

#include



using namespace std;



const int Q_SIZE = 3;

struct ppl {

    string name;

    int age;

};



class Q {

private:

    int N, f, r, len;

    ppl *P;



public:

    Q() : N(0), f(0), r(0), len(0), P(NULL) { }

    Q(int n) : N(n), f(0), r(0), len(0) {

        P = new ppl[N];

    }

    ~Q() {

        if (P != NULL) {

            delete [] P;

        }

    }

    void add() {

        string in;

        stringstream ss;



        if (len == N) {

            cout << endl << "Full, can't insert";

        } else {

            cout << "Enter name: ";

            getline(cin,P[r].name);

            do {

                cout << "Enter age: ";

                getline(cin, in);

                ss.clear(); ss.str(in);

            } while (!(ss >> P[r].age));

            r = (r + 1) % N;

            ++len;

        }

    }

    void remove() {

        if (len == 0) {

            cout << "Empty";

        } else {

            cout << "removing " << P[f].name;

            f = (f + 1) % N;

            --len;

        }

    }

    void display() {

        if (len == 0) {

            cout << "Empty";

        } else {

            for (int i = f, j = 0; j < len; j++, i = ((i + 1) % N)) {

                cout << endl << "NAME: " << P[i].name;

                cout << endl << "AGE : " << P[i].age << endl;

            }

        }

    }

};



int main(int argc, char *argv[]) {

    Q q(Q_SIZE);

    char ch;

    string in;



    while (true) {

        cout << endl << "ENTER CHOICE (A/R/D/Q): ";

        getline(cin, in);

        ch = toupper(in[0]);



        switch(ch) {

        case 'A':

            q.add();

            break;

        case 'R':

            q.remove();

            break;

        case 'D':

            q.display();

            break;

        case 'Q' :

            exit(0);

            break;

        }

    }

    return 0;

}
PAUL
2011-01-26 04:22:29 UTC
You seem to have a basic compilation error.



You are including which isn't in your include path, so the compiler is generating an error, plus you are calling a function or macro (clrscr()) which is not defined, so you are getting a second error. The compiler has become confused over the location of the first include error, but this is not uncommon.
anonymous
2016-04-26 10:07:45 UTC
wat


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...