c++ fixing queue

So I have this problem:

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.

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.


If the input is:

chemistry,compsci A,bio A,english B

compsci B,compsci D,compsci C


The output should be:

170

compsci B,compsci A,compsci D,compsci C,chemistry,bio A,english B


But when I run my code, the output I get is this:

170

compsci A,compsci B,compsci D,compsci C,chemistry,bio A,english B

I think I am supposed to check queue 1 and 2 in sequence, queue1.front then queue2.front then back to queue1.front then queue2.front, but the way my code is its running queue1 first then moving on to queue2 which is why its putting it in the wrong order, but I am not sure how to rewrite the loops so it checks them in sequence. Please help as I am completely lost on how to do this.

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;
}
Last edited on
@TinnyMintz,
Your explanation of the problem makes no sense. It would be better if you posted the original problem verbatim.



The first line of output should display the total time

Neither your "explanation" nor your input mention "time". What has "time" got to do with it? How do you get 170 from that input?


Apparently, your intended output:
compsci B,compsci A,compsci D,compsci C,chemistry,bio A,english B

is "organised". Err, on what basis?


The fact that your question uses the English word "queue" ... doesn't mean that you have to use the computational entity std::queue. In fact, I can't see any good reason for doing so.


Last edited on
time isn't a time but a 'cost'. From the code, when items are 'pushed' to another queue there is a fixed cost associated with the operation.

Before even thinking about coding, an algorithm should be produced that details/shows how the output is obtained from the inputs - without using any coding. Once you have this, then you produce a program design and then you write the code from the program design. The algorithm should be such that only using pen/paper and the input anyone should be able to produce the output without knowing anything about what is required - just the steps to perform.
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
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
using namespace std;

istringstream in( "chemistry,compsci A,bio A,english B\ncompsci B,compsci D,compsci C\n" );

int main()
{
// ifstream in( "input" )   // for demo uses the stringstream above
   stringstream q[2];
   string s;
   getline( in, s );   q[0] << s;
   getline( in, s );   q[1] << s;

   int t = 0;
   string CS, NOTCS;
   while ( q[0] || q[1] )
   { 
      for ( int i : { 0, 1 } )
      {
         getline( q[i], s, ',' );
         if ( q[i] )
         {
            if ( s.find( "compsci" ) != string::npos )
            {
               CS += ( CS.empty() ? "" : "," ) + s;
               t += 20;
            }
            else
            {
               NOTCS += ( NOTCS.empty() ? "" : "," ) + s;
               t += 10 + 20;
            }
         }
      }
   }
   cout << t << '\n';
   cout << CS + ( CS.empty() || NOTCS.empty() ? "" : "," ) + NOTCS << '\n';
}


170
compsci B,compsci A,compsci D,compsci C,chemistry,bio A,english B

Last edited on
Topic archived. No new replies allowed.