Hello,
I'm getting an error when I try to print out a stack of objects. I want to have a stack that simulates the "Running Queue" in an operating system. I want to have one Process (represented by a number) and the IDLE process. Here is my code when I populate the stack and the two queues:
Code:ifstream infile("in_file.txt"); ifstream event("events.txt"); list<string> vec; list<string> tempPro; stack<string> run; queue<string> ready; queue<string> wait; if (!infile || !event) { cerr << "error: unable to open input file!\n"; return -1; } string word; string idle = "IDLE"; run.push(idle); 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); // print the contents 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.top(); 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; } }
The parts in bold are the parts that concern the stack. When I print the results, I get two things in the run stack, the process number twice. For some reason, it won't print the string "IDLE". However, when I manipulate the contents later in my code so that there is only one string in the stack, the word IDLE is displayed. Can someone tell me why two numbers print instead of a number and the word IDLE when I try to print the stack with two strings in it? Thanks.