I have a hw prob here that im done coding w/.. i just get the wrong output about halfway and i know kind of wat it is but i cant fix it. my thought is im makin my function to complex.
http://cis.poly.edu/cs1124/Homework/hw01/hw01.html
thats the site that explains the hw prob and heres my code..
Code:#include <vector> #include <iostream> #include <fstream> #include <string> using namespace std; struct Process //Structure will hold all process information from the procces file { //CHECK THESE TYPES!!! int procId; // process ID ... 1000.. 1001 etc string name; // holds the name of the process int cpuTime; // Total CPU time needed for process int timeBurst; // Burst for each process }; //will gather contents from stream and enter them into a vector of structs void fillVector(ifstream&, vector<Process>&); //Processes the schedule and outputs as it creates the schedules. void procSched(vector<Process>&); //---------------------------------------------- int main() { vector<Process> proc; //Vector of process structs ifstream ifs; // filestream for process text ifs.open("process.txt"); // open file and check for error while opening if (!ifs) { cerr << "Error opening 'process.txt' \n "; exit(1); //exit if error is found } fillVector(ifs, proc); //fill the vector with information from the stream procSched(proc); //output the scheduling as it is calculated return 0; } //----------------------------------------------- void fillVector(ifstream& inputFile, vector<Process>& procVec) { Process tempProc; //temporary variable to fill process vector //Loop through the file checking for each struct member while (inputFile >> tempProc.procId >> tempProc.name >> tempProc.cpuTime >> tempProc.timeBurst) { procVec.push_back(tempProc); // fill the vector with each temp member } } void procSched(vector<Process>& procVector) { // totProc is the total number of Proccess that need to run //size_t totProc = procVector.size(); int time(0); //will print the time each process began while (!procVector.empty()) // Continue to process until vector is empty { for (size_t j = 0; j < procVector.size(); j++) // Loop through the vecotr { //The time is decreased by each processes burst procVector[j].cpuTime -= procVector[j].timeBurst; if (procVector[j].cpuTime <= 0) // If time is below 0, the process is exhausted { //output the process information with 0 being the final time left cout << time << ' ' << procVector[j].procId << ' ' << procVector[j].name << ' ' << "0" << endl; //The time the next process begins is incremented by the burst //of the process just completed time += procVector[j].timeBurst; //delete the element of the vector //that no longer runs as a process procVector.erase(procVector.begin() + j); } else // if its not 0 the process is ongoing { //output the process information cout << time << ' ' << procVector[j].procId << ' ' << procVector[j].name << ' ' << procVector[j].cpuTime << endl; //The time the next process begins is incremented by the burst //of the process just completed time += procVector[j].timeBurst; } } } }
I know the prob is in my last function.. i think im either deleting something 2 quick or displaying 2 late. something to that effect... if u think i can do this more effectively plz tell me.