All right. I added in some of the suggested things, and I added in an exit condition into rr(). The condition is triggeresd all the time, so I need to figure that out. Any suggestions?
Also, my arguments for my program are always going to be two arguments greater then 5.
I need to go over to the doctor. My sickness may be contributing to my sloppy coding. I'll be back in a few hours. :P Here's what I have now.
Code:
#include <iostream>
#include <fstream>
#include <cstdlib>
using namespace std;
/* Process Data Structure */
struct process {
int arrive; /* Process Arrival Time */
int burst; /* CPU Burst Time */
float working; /* Working time*/
float waiting; /* Waiting time*/
process *next;
};
process *init_process (int pid, int burst);
float rr (process *proc);
int main(int argc, char* argv[]) {
process *plist,*ptmp;
int init = 0;
int quanta = static_cast<int>(std::strtol(argv[1], NULL, 0));
int context_switch = static_cast<int>(std::strtol(argv[2], NULL, 0));
float cswitch = context_switch/100;
ifstream input;
input.open("rrdata.txt");
if (input.fail()) {
cerr << "unable to open file rrdata.txt for reading" << endl;
exit(1);
}
ofstream output;
output.open("output.txt");
if (output.fail()) {
cerr << "unable to open file output.txt" << endl;
exit(1);
}
//Initialize the list of processes
int arrive,burst;
input>>arrive>>burst;
output<<arrive<<' '<<burst<<endl;
plist = init_process(arrive,burst);
init++;
input>>arrive>>burst;
plist -> next = init_process(arrive,burst);ptmp = plist->next;
init++;
output<<arrive<<' '<<burst<<endl;
for (int i = 0; i < 97; i++) {
input>>arrive>>burst;
ptmp -> next = init_process(arrive,burst);ptmp = plist->next;
init++;
output<<arrive<<' '<<burst<<endl;
}
input>>arrive>>burst;
ptmp -> next = init_process(arrive,burst);
init++;
output<<arrive<<' '<<burst<<endl;
cout<<init<<endl;
cout<<argv[1]<<' '<<argv[2]<<endl;
float avgburst = rr(plist);
input.close();
output.close();
return 0;
}
struct process *init_process (int arrive, int burst) {
struct process *proc;
proc = new struct process;
if (proc == NULL) {
printf("Fatal error: memory allocation failure.\nTerminating.\n");
exit(1);
};
proc->arrive = arrive;
proc->burst = burst;
proc->working = 0;
proc->waiting = 0;
proc->next = NULL;
return(proc);
};
float rr (process *proc){
float burst = 0;
for(int i=0; i<5; i++){
burst = burst + proc->burst;
if (proc -> next == NULL){
cerr<<"CANNOT CONTINUE SUMMATION. ENDING."<<endl;
return 0;
}
proc = proc -> next;
}
return(burst);
};