Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1100941
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T00:57:39+00:00 2026-05-17T00:57:39+00:00

My program is to print the queue of information from a file but i

  • 0

My program is to print the queue of information from a file but i have problem with my following code. When i run the program it keep loop. I cant figure out the problem. Any help?

#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
#include <queue>
#include <list>
using namespace std;

void simulation(ifstream &infile);
void processArrival(int *newEvent, ifstream &inFile, list<int> eventList,queue<int> printQueue);
void processDeparture(int *newEvent, list<int> eventList,queue<int> printQueue);
    string name[100];
    int timeAccepted[100];
    int fileSize[100];
    int i = 1;
    int j = 1;

int currentTime;
bool checker = true;
int main(void)
{

    ifstream inFile;
    string fileName;

    int i = 0;
    inFile.open("123.txt", ios::in);

    simulation(inFile);
    /*while(inFile.peek() != EOF )

    {
        inFile>>name[i]>>timeAccepted[i]>>fileSize[i];
        i++;
    }

    for(int s = 0; s < i; s++)
    {
        cout << name[s] << timeAccepted[s] <<  fileSize[s] <<endl;
    }*/
    return 0;
}


void simulation(ifstream &inFile)
{
    queue<int> printQueue;
    list<int> eventList;

    int *newEvent;
    while(inFile.peek() != '\n')
    {
        inFile>>name[0]>>timeAccepted[0]>>fileSize[0];



    }



    eventList.push_front(timeAccepted[0]);
    int checkEmpty = eventList.empty();
    newEvent = &eventList.front();
    while(checkEmpty ==0)
    {

         newEvent = &eventList.front();


        if(checker)
        {
            processArrival(newEvent, inFile, eventList, printQueue);

        }

        else
        {
            processDeparture(newEvent, eventList, printQueue);

        }
        checkEmpty = eventList.empty();
    }


}

void processArrival(int *newEvent, ifstream &inFile, list<int> eventList,queue<int> printQueue)
{
    int atFront=0;
    atFront = printQueue.empty();
    cout << atFront <<endl;
    printQueue.push(*newEvent);
    cout << printQueue.front() <<endl;
    eventList.remove(*newEvent);

    int temp;

    if(atFront==1)
    {
        currentTime = *newEvent + fileSize[0];
        cout << name[0] << " @@ " << *newEvent << " @@ " << currentTime << endl;
        eventList.push_back(currentTime);



    }
    checker = false;
    if(inFile.peek() != EOF )
    {

        inFile>>name[i]>>timeAccepted[i]>>fileSize[i];

        eventList.push_back( timeAccepted[i] );
        i++;

        checker = false;
        if(eventList.back() <= eventList.front())
        {
            temp = eventList.back();
            eventList.back() = eventList.front();
            eventList.front() = temp;
            checker = true;
        }
    }





}

void processDeparture(int *newEvent, list<int> eventList,queue<int> printQueue)
{
    printQueue.pop();
    eventList.pop_front();
    int checkEmpty = 1;
    checkEmpty = printQueue.empty();
    int temp;
    if(checkEmpty ==0)
    {
        currentTime = *newEvent + fileSize[j];
        cout << name[j] << " " << *newEvent << " " << currentTime << endl;
        eventList.push_back(currentTime);
        checker = true;
        if(eventList.back() < eventList.front())
        {
            temp = eventList.back();
            eventList.back() = eventList.front();
            eventList.front() = temp;
            checker = false;
        }
        j++;
    }

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T00:57:40+00:00Added an answer on May 17, 2026 at 12:57 am

    Your processArrival and processDeparture functions are taking their eventList and printQueue arguments by value. This means that when you call them, for example in this line:

    processArrival(newEvent, inFile, eventList, printQueue);
    

    Copies of eventList and printQueue are made and passed into the processArrival function. The processArrival function then operates on those copies, and the original data is never modified. In particular, this means that the original eventList will never have any items removed from it, so it will never be empty — it will just keep trying to process the first event over and over again.

    The solution is to pass these parameters by reference. i.e. change the definition of processArrival to

    void processArrival(int *newEvent, ifstream &inFile, list<int>& eventList, queue<int>& printQueue)
    

    Note the & characters that I have inserted before eventList and printQueue. These cause references to the original data, rather than copies of the original data, to be passed into the processArival function. This means that processArrival will operate directly on the original data as you intend it to. Don’t forget to make the corresponding change to processDeparture as well.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to understand the practical difference during the execution of a program

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.