Hello,
I have the following code all written and working properly:
What I want to do is to take the parts of the code in bold and make them into their own functions. I tried declaring the queue objects outside of the main function, but that gave me a compiler error. Can someone show me the code to make the functions out of those lines of code in bold?Code:#include <iostream> #include <fstream> #include <string> #include <list> #include <queue> using namespace std; void updateRun(); void updateReady(); void updateWait(); int main () { ifstream infile("in_file.txt"); ifstream event("events.txt"); list<string> vec; list<string> tempPro; queue<string> run; queue<string> ready; queue<string> wait; if (!infile || !event) { cerr << "error: unable to open input file!\n"; return -1; } string word; while (infile >> word) { vec.push_back(word); string tempEl; if (word == "running") { tempEl = tempPro.front(); tempPro.pop_front(); run.push(tempEl); } else if (word == "ready") { tempEl = tempPro.front(); tempPro.pop_front(); ready.push(tempEl); } else if (word == "waiting") { tempEl = tempPro.front(); tempPro.pop_front(); wait.push(tempEl); } else tempPro.push_front(word); if (word == "running" || word == "ready" || word == "waiting") { int runSize = run.size(); int readySize = ready.size(); int waitSize = wait.size(); cout << "Running: "; while (runSize != 0) { string running = run.front(); cout << running << " "; run.pop(); run.push(running); runSize--; } cout << endl; cout << "Ready: "; while (readySize != 0) { string readyToGo = ready.front(); cout << readyToGo << " "; ready.pop(); ready.push(readyToGo); readySize--; } cout << endl; cout << "Waiting: "; while (waitSize != 0) { string waiting = wait.front(); cout << waiting << " "; wait.pop(); wait.push(waiting); waitSize--; } cout << endl; cout << endl; } } string word2; int eventNum = 1; while (event >> word2) { if (word2 == "1") { string elem1 = run.front(); if (ready.size() != 0 ) { string toRun1 = ready.front(); ready.pop(); run.push(toRun1); } } else if (word2 == "2") { string elem2 = run.front(); run.pop(); wait.push(elem2); if (ready.size() != 0) { string toRun2 = ready.front(); ready.pop(); run.push(toRun2); } else { string toRun2 = wait.front(); wait.pop(); run.push(toRun2); } } else if (word2 == "3") { string elem3 = wait.front(); wait.pop(); ready.push(elem3); if (ready.size() == 0 || run.size() == 1) { string toRun3 = ready.front(); ready.pop(); run.pop(); run.push(toRun3); } } int runSize = run.size(); int readySize = ready.size(); int waitSize = wait.size(); cout << endl; cout << "Event #" << eventNum << endl; cout << "Running: "; while (runSize != 0) { string running = run.front(); cout << running << " "; run.pop(); run.push(running); runSize--; } cout << endl; cout << "Ready: "; while (readySize != 0) { string readyToGo = ready.front(); cout << readyToGo << " "; ready.pop(); ready.push(readyToGo); readySize--; } cout << endl; cout << "Waiting: "; while (waitSize != 0) { string waiting = wait.front(); cout << waiting << " "; wait.pop(); wait.push(waiting); waitSize--; } cout << endl; eventNum++; } return 0; }