program conversion.

This is a discussion on program conversion. within the C++ Programming forums, part of the General Programming Boards category; I have a sequential queue program that was supposed to be a linked queue program. How do I change it ...

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    66

    program conversion.

    I have a sequential queue program that was supposed to be a linked queue program.

    How do I change it to be correct.

    Code:
    #include <iostream.h>
    
    void enqueue (char queue[maxqueue], int& front, int& rear, char item)
    	{
       rear=(rear+1)%maxqueue;
       if(front!=rear)
       	{
          queue[rear]=item;
          }
       else
       	{
          cout<<"OVERFLOW\n";
          rear=rear-1;
          if (rear<0)
          	rear=rear+maxqueue;
          };
       };
    
    
    void dequeue (char queue[maxqueue], int& front, int& rear, char& item)
    	{
       if (front!=rear)
       	{
          front=(front+1)%maxqueue;
          item=queue[front];
          //queue[front]=' ';
          }
       else
       	{
          cout<<"UNDERFLOW--FILLERUP\n";
          item='0';
          };
       };
    
    //lists items on a sequential queue
    void list (char queue[maxqueue], int front, int rear)
       {
       int count=0,where;
       //from front to rear and cout
       where=front;
       while (where!=rear)
       	{
          where=(where+1)%maxqueue;
          cout<<queue[where];
          count++;
          };
       /*//rearplus=(rear+1)%maxqueue;
       for (where=(front+1)%maxqueue;where!=rearplus;where=(where+1)%maxqueue)
       	{
          count++;
          cout<<queue[where];
          }; */
       cout<<endl<<count<<" items on queue"<<endl;
       };
    
    //menu for queue routines
    void queuedriver(char queue[maxqueue], int& front, int& rear);
    
    void main ()
    {
    char answer,table[maxqueue];
    int front,rear;
    
    cout<<"We will maintain a queue for you.\n";
    cout<<"At your instruction, we will enqueue, dequeue, list, or halt.\n";
    
    do//The whole program as often as needed
    {
    
    //initialize counter for task
    front=0;
    rear=0;
    
    //basic menu driver
    queuedriver(table,front,rear);
    
    
    //allows repeat of whole program
    cout<<"Would you like to do another queue simulation('Y' to continue)?\n";
    cin>>answer;
    }while ((answer=='Y') || (answer=='y'));
    };
    
    
    
    
    
    
    //menu for queue routines
    void queuedriver(char queue[maxqueue], int& front, int& rear)
    {
    const int maxcommand=4;
    char command[maxcommand], thing;
    
    
    cout<<"the options are as follows:\n";
    cout<<"1. enqueue\n";
    cout<<"2. dequeue \n";
    cout<<"3. list\n";
    cout<<"4. halt\n";
    do//queue menu options
    	{
       cout<<"type command:(en,de,li,ha)\n";
       cin>>command;
       if ((strcmp(command,"enqu")==0) || (strcmp(command,"en")==0) ||
           (strcmp(command,"1")==0)    || (strcmp(command,"e")==0) )
           {
           cout<<"item to put on queue:";
           cin>>thing;
           enqueue(queue,front,rear, thing);
           cout<<thing<<" enqueued on queue \n";
           }
       else if ((strcmp(command,"dequeue")==0) || (strcmp(command,"de")==0) ||
                (strcmp(command,"2")==0)    || (strcmp(command,"d")==0) )
           {
           dequeue(queue,front,rear,thing);
           cout<<thing<<" dequeued from queue\n"<<endl;
           }
       else if  ((strcmp(command,"list")==0) || (strcmp(command,"li")==0) ||
                  (strcmp(command,"3")==0)    || (strcmp(command,"l")==0) )
           {
           list(queue,front,rear);
           }
       else if  ((strcmp(command,"halt")==0) || (strcmp(command,"ha")==0) ||
                 (strcmp(command,"4")==0)    || (strcmp(command,"h")==0) )
           {
           cout<<"goodbye\n";
           strcpy(command,"h");
           }
       else
       	cout<<"unrecognized command\n";
    
       } while (strcmp(command,"h")!=0);
    };

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Does it compile?

    This is outdated.
    Code:
    #include <iostream.h>
    Use <iostream> instead, and either add std:: to all your couts etc or use the namespace.
    Code:
    #include <iostream>
    using namespace std;
    Code:
    void main()
    Don't use this -- use int main() and probably a return 0 statement.

    You don't need semicolons after every closing brace after every function.

    strcmp() is in <cstring> (or <string.h> if your compiler's really old); be sure to include it.

    Code:
    const int maxcommand=4;
    char command[maxcommand], thing;
    // ...
       else if ((strcmp(command,"dequeue")==0) || (strcmp(command,"de")==0) ||
                (strcmp(command,"2")==0)    || (strcmp(command,"d")==0) )
    command[4] can't hold "dequeue". It needs to be at least [8].
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Registered User
    Join Date
    Nov 2006
    Posts
    66
    My compiler is really old.

  4. #4
    Registered User Queatrix's Avatar
    Join Date
    Apr 2005
    Posts
    1,342
    Then get a new one:
    www.bloodshed.net/devcpp.html

  5. #5
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    So download a new one. This code won't even compile on some modern compilers. Clicky.
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Issue with program that's calling a function and has a loop
    By tigerfansince84 in forum C++ Programming
    Replies: 9
    Last Post: 11-12-2008, 12:38 PM
  2. Need help with my program...
    By Noah in forum C Programming
    Replies: 2
    Last Post: 03-11-2006, 06:49 PM
  3. Decimal to Binary Conversion program
    By acidbeat311 in forum C Programming
    Replies: 5
    Last Post: 01-12-2006, 09:26 PM
  4. question about the loop in case conversion program
    By Elhaz in forum C++ Programming
    Replies: 8
    Last Post: 09-20-2004, 04:06 PM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21