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.

Example of Input and Answer:

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:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#include <iostream>
#include <string>
#include <sstream>
#include <fstream>
#include <vector>
#include <queue>
#include "ArgumentManager.h"

using namespace std;

queue<string> splitStringUsingComma(string text)
{
  queue<string> 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<string> queue1,queue2;
  getline(input,text);
  queue1 = splitStringUsingComma(text);
  text.clear();
  getline(input,text);
  queue2 = splitStringUsingComma(text);
  queue<string> queue3;
  int cost{0};
  vector<string> 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<<cost<<'\n';

  for(int i = 0; i < sol.size() -1; ++i)
  {
    output << sol.at(i)<<",";
  }
  output << sol.back();
  output<<endl;
  input.close();
  output.close();
  return 0;
}


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.