creating new queue dynamically

Hello,

I have an assignment in Operating System where I am suppose to simulate a process scheduler. The process are inputs through command line. A sample list of input would look something like:
START 10
DISK 12
SEND
START 5
The 'START' indicates arrival of a different process and each numerical value is the arrival time.

My approach:
Going 1 step at a time, I was first able to get input from command line, using i/o redirection.
Next I tried taking the user input from the command line and put in a queue,
And every time user in put is a 'START' create a new queue and put the rest in the new queue until another 'START' is found and so on.

But I am stuck on how do I make this new dynamic queue?

My initial 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
#include <cstdlib>
#include <stdio.h>
#include <iostream>
#include <queue>
#include <String.h>

using namespace std;

int main()
{
    //console input
    char *ch;
    
    //queue declaration
    queue<string> process;     
    
    
    do
    {
        *ch = fgetc(stdin); //gets user input from command line
        string procedure(ch); //convert the input to string 
       
        
        do
        {
        if(procedure == "START") //if input == START then make a new queue
        {
         cout<<"New process"<<endl;
         
         
        }
          else
          {
             process.push(procedure); //push stuff into queue
            //cout<<"Word entered"<<ch<<endl;
            cout<<"Reading queue"<<process.front()<<endl;
            process.pop();
          }
        }while(!process.empty());
    }while(ch != "\0");
       
    system("PAUSE");
    return 0;
}


I appreciate any help.
fgetc() is the wrong approach. See http://www.cplusplus.com/reference/clibrary/cstdio/fgetc/
it retrieves a single char instead of the whole line like you expect.
Line 20 leads to a crash since 'ch' is an invalid pointer.

to get the wole line use getline(). See http://www.cplusplus.com/reference/string/getline/
1
2
3
4
5
6
7
8
9
10
    //console input
    string line; // Note
    
    //queue declaration
    queue<string> process;     
    
    
    do
    {
        getline(cin, line);


In order to get the first word from that line use stringstream. See http://www.cplusplus.com/reference/iostream/stringstream/

Like so
1
2
3
4
5
6
7
8
9
10
#include <sstream>
...

stringstream ss;
string first_word;
ss >> first_word;
if(first_word == "START")
{
  ...
}


if you want a queue each time "START" you need another container like vector that harbors the queue:

1
2
3
4
5
6
7
8
9
10
11
12
vector<queue<string> > process;
...
if(first_word == "START")
{
  process.push_back(queue<string>());
  ...
}
else if(!process.empty()) // vector must not be empty / there must have been "START" before this
{
  process.back().push(line);
...
}


Topic archived. No new replies allowed.