Pushing string array on to vector queue

Hi,

I was trying to read a certain text file (using i/o redirection) into a string type array. My file looks like as such:

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
NDISKS  3
DISKTIME 10
NBOXES   32
START    7
CPU      100
TTY      5000
CPU      50
TTY      100
CPU      90
SEND     12
CPU      50
TTY      100
CPU      50
DISK     0
CPU      20
START    124
CPU      80
DISK     1
CPU      50
RECEIVE  12
CPU      100
TTY      150
CPU      200
DISK     1
CPU      100
DISK     1
CPU      100
DISK     1
CPU      100
DISK     1
CPU      100
TTY      500
CPU      100
SEND	 7
CPU      150
START    205
CPU      100
TTY      3000
CPU      50
TTY      100
CPU      50
TTY      2000
CPU      50
DISK     1
CPU      20
DISK     1
CPU      60
DISK     1
CPU      50
DISK     1
CPU      50
DISK     1
CPU      50
TTY      100
CPU      50

Long story short, given the requirements for part of my assignment to separate each type of processes (the left column) I created arrays to store the file input and then every time I read a certain 'key' word I would create a new queue. Some one from one of my earlier posts suggested to harbor my queues in vectors and that would make work much easier. I read about vectors so and I tried implementing in my program and have ran into weird trouble.
Everytime my compilers gets to the part where I push stuff into the vector<queue> it gives me a runtime error.
Please note: the program compiles without erors and I have verified that the only reason for runtime error is when I try pushing stuff on to vector<queue>.The statement that causes the error is inside the else[] and is in bold.


Please if some one can look through my code and point me out what am I doing wrong and how can I resolve it.

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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
                       //Header file
#ifndef ProcessSimulation_H
#define ProcessSimulation_H


#include <iostream>

using namespace std;
      

class ProcessSimulation
{
      private:
              string inputFile;
              
      public:
             int ReadInput(string inputFile);
             string CreateQueues(string*, string*,int );
};
#endif
 
                  //Implementation file
#include "ProcessSimulation.h"
#include <queue>
#include <vector>

int ProcessSimulation::ReadInput(string inputFile)
{ 
     int i = 0; //Increment i for every new cin. This way process time will be stored in Time array
     string ProcessName[1000]={"/0"};
     string ProcessTime[1000]={"/0"};
     string File;
     int loop = 0; //for array index increment
     int loop1 = 0;
     
    
         
    
     while(!cin.eof())
     {
         
         cin>>File;
         
                   if(i%2==0)
                   {
                    ProcessName[loop] = File;
                    i++;
                  // cout<<"ProcessName: "<<ProcessName[loop]<<endl;
                    loop++;
                   }
                   else if(i%2 !=0)
                   {
                        ProcessTime[loop] = File;
                        i++;
                        loop++;
                   }
                   
    }
    
    while(loop1<loop)
    {                
                     if(ProcessName[loop1] != "\0")
                     {
                     cout<<"ProcessName: "<<ProcessName[loop1]<<endl;
                     }
                     
                     if(ProcessTime[loop1] != "\0")
                     {
                     cout<<"ProcessTime: "<<ProcessTime[loop1]<<endl;
                     }
                     loop1++;
    }
     
     
               CreateQueues( ProcessName,ProcessTime,loop);   
                   
                                  
    
 return 0;               
 }
 
  string ProcessSimulation::CreateQueues(string *ProcName,string *ProcTime,int x)
  {
         
         
         int newloop = 0;
         
         vector<queue<string> >Process;
         
        
         
         
         
         cout<<"In Create Queue:"<<endl;
         
        while(newloop<x)
    {                
                     cout<<ProcName[newloop]<<endl;
                     
                    if(ProcName[newloop] == "START")
                     {
                       cout<<"Test 1"<<endl;
                       Process.push_back(queue<string>());
                       Process[0].push(ProcName[newloop]);
                       
                     }
                       
                       
                     else
                     {
                         cout<<"Test 2"<<endl;
                       
                     Process[0].push("Crap"); //CAUSE RUN TIME ERROR
                          
                     
                     }
                      
                     
                  
                     newloop++;
                     
                     
    }
    cout<<"Testing..."<<endl;
    
    return (*ProcName, *ProcTime) ;
         
  }

 
     //main.cpp
#include "ProcessSimulation.h"
#include <cstdlib>
#include <iostream>
#include <string>
#include <fstream>
#include <stdio.h>

using namespace std;
 int BUFSIZE = 500;

int main()
{
    
   string File = "\0";
   ProcessSimulation Proc;
   
  
   
   //getline(cin,File);

   Proc.ReadInput(File);
   
  
                         
   
  
    system("PAUSE");
    return EXIT_SUCCESS;
}


Thank you.









If the first line of the input is not started from "START", the "ProcName[newloop] == "START"" and #103 could be ignored.

Then in #113, you will face an undeclared queue of vector, I guess.

Try to do more protection on your codes.


b2ee
To clarify, on line 113 you access the first element of a vector that has no elements. That's not good.

The whole design looks suspect. I suspect CreateQueues should be doing something other than creating a local vector of queues that will go out of scope when the function returns.
I agree with cire , design looks suspect
Topic archived. No new replies allowed.