### Queue not printing properly Problem:

Introduction

You will create a C++ program calculate the time needed when organizing the library bookshelf and find the order of book on the shelf after organizing. When organizing, the library manager decides to put the books into two queues and select one book from each queue to fill the shelf in sequence. These books are mixed with different topic. However, the manager wants to fill the shelf with Comp Sci book first before adding the rest into the shelf. If a book is not a Comp Sci book, it will be discarded into a third queue, waiting to be added to the shelf. Compute the time it takes to organize all the books, assuming that putting a book into the shelf take 20 sec and discarding a book take 10 sec.

Example of Operation (input 1):

Queue 1: math,compsci A,compsci B
Queue 2: art,novel,compsci C

Begin with queue 1, the first book is math so it will be discarded into a third
queue (since it took 10 sec to discard, total time = 10)

Queue 3: math

Then following the sequence, the manager takes a book from queue 2, which is
art so it will also be discarded into the third queue (total time = 20)

Queue 3: math,art
The next book will be taken from queue 1, this time it’s a comp sci book so it will took 20 sec to organize it into the shelf (total time = 40)
...

This operation will take total of 150 sec And the order of book is: compsci A,compsci B,compsci C,math,art,novel

Note: queue 1 and 2 must be empty before you can proceed with queue 3.
You must use queue structure to implement your solution

Input and Output

a. Input file

The first line of input will contain the list of books in queue 1.
The second line will contain the list of books in queue 2.
Each book will be separated by a comma.
Book name can have spaces in between the string.
There will be no duplicate book name.
No empty input will be given.

b. Output file
The first line of output should display the total time.
The second line should display the order of book after organizing, with each
book separated by a comma.

input1.txt
math,compsci A,compsci B
art,novel,compsci C

ans1.txt
150
compsci A,compsci B,compsci C,math,art,novel

input2.txt
chemistry,compsci A,bio A,english B
compsci B,compsci D,compsci C

ans2.txt
170
compsci B,compsci A,compsci D,compsci C,chemistry,bio A,english B

input3.txt
novel,magazine,science
math,physic,english

ans3.txt
180
novel,math,magazine,physic,science,english

My Code:
 ``12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394`` ``````#include #include #include #include #include #include #include "ArgumentManager.h" using namespace std; queue splitStringUsingComma(string text) { queue sol; stringstream ss(text); while(ss.good()) { string x; getline(ss,x,','); sol.push(x); } return sol; } int main(int argc, char* argv[]) { ArgumentManager am(argc, argv); ifstream input; string infileName = am.get("input"); string outfileName = am.get("output"); input.open(infileName); string text; queue queue1,queue2; getline(input,text); queue1 = splitStringUsingComma(text); text.clear(); getline(input,text); queue2 = splitStringUsingComma(text); queue queue3; int cost{0}; vector sol; while(!queue1.empty()) { if(queue1.front().find("compsci")!=string::npos) { sol.push_back(queue1.front()); cost+=20; } else { queue3.push(queue1.front()); cost+=10; } queue1.pop(); } while(!queue2.empty()) { if(queue2.front().find("compsci")!=string::npos) { sol.push_back(queue2.front()); cost+=20; } else { queue3.push(queue2.front()); cost+=10; } queue2.pop(); } while(!queue3.empty()) { sol.push_back(queue3.front()); queue3.pop(); cost+=20; } ofstream output; output.open(outfileName); output<

Output1.txt matches what the answer should be, but with output2.txt and output3.txt, it prints them in not the correct order, like for output2.txt it outputs this: compsci A,compsci B,compsci D,compsci C,chemistry,bio A,english B
Last edited on Problem: Same as the other Topic.
https://www.cplusplus.com/forum/general/280425/
Last edited on
Topic archived. No new replies allowed.