HI,
My project is due tommorrow, I am stuck at this error which i have no clue. I have tried all my best to understand what is its cause but couldn't. I really need help.
All I am saying is if you can explain why its doing this.
***Problem***
We are trying to read processes, it sucessufully stores every process and its size and location. But what it dose is that when it reads the new process and i am trying to store every process using seprate objects[ object] of structure. It changes its name to the next process. e.g process[j] and process[j-1] and process[j-2] are getting the same names.
It deals with only function [ readFile & insert ].
I couldn't understand why?
***Project***
What projetc does is that we read all these processes enter them and exit them and do memory management through first-fit or worst-fit.
***input file***
e.g
Editor Enter 100
Enroll Enter 300
Scheduler Enter 100
Editor Exit 100
***Source Code****
#include <string>
#include <cstring>
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <iomanip>
#define TRUE 1
#define FALSE 0
#define ZERO 0
#define ONE 1
#define TWO 2
#define THREE 3
#define FOUR 4
#define FIVE 5
void readingArguments( int argc, char *[FIVE] );
void readFile( ifstream & );
void swapObject( int j );
void swapBack( int );
void insert( char *, int );
void Delete( char* );
int totalMemory;
int operatingSystem;
int firstFit;
int worstFit;
int i = 1;
struct memoryList{
int size;
int location;
char *name;
} object[100];
void main( int argc, char * argv[] )
{
cout << "\n" << endl;
cout << "This is line number " << __LINE__ << " of the file "
<< __FILE__ << " which was compiled on " << __DATE__
<< "\nat time " << __TIME__ << ".\n\n";
readingArguments( argc, argv );
}
void readingArguments( int argc, char * argv[FIVE] )
{
if( argc != FIVE )
{
cerr << "Usage : a.out argv[1] argv[2] argv[3] argv[4] " << endl;
exit(1);
}
ifstream inFile( argv[ONE], ios::in );
if( !inFile )
{
cerr << "INPUT FILE CANNOT BE OPEN FOR INPUT" << endl;
exit(2);
}
else
{
cout << "INPUT FILE : " << argv[ONE] << " OPEN FOR INPUT. \n " << endl;
}
if( atoi(argv[TWO]) == atoi("F") || atoi(argv[TWO]) == atoi("f") )
{
firstFit = TRUE;
cout << "*** First-Fit Is Selected ***\n" << endl;
}
else if ( atoi(argv[TWO]) == atoi("W") || atoi(argv[TWO]) == atoi("w") )
{
worstFit = TRUE;
cout << "*** Worst-Fit Is Selected ***\n" << endl;
}
else
{
cout << " Usage argv[2] : F for First-Fit or W for Worst-Fit " << endl;
exit(3);
}
totalMemory = atoi( argv[THREE] );
operatingSystem = atoi( argv[FOUR]);
readFile( inFile );
}
void readFile ( ifstream &inFile )
{
char name[40], Status[17];
char *status;
char *enter = "Enter", *exit = "Exit";
int memorySize, holder, cmp;
object[1].name = "OS";
object[1].size = operatingSystem;
object[1].location = 0;
for( int k = 2; k < 10; k++ )
object[k].name = "Hole";
while ( inFile >> name >> Status >> memorySize )
{
status = Status;
i++;
cmp = strcmp(status,enter);
if ( cmp == ZERO )
insert( name, memorySize);
cmp = strcmp(status,exit);
// if ( cmp == ZERO )
// Delete ( name );
for(int u = 1; u < 10; u++ )
cout << object[u].name << " is loaded at address = " << object[u].location
<< " with size = " << object[u].size << "K\n" << endl;
}
}
void insert( char *Name, int size )
{
int holder, cmp;
char *hole = "Hole";
char *holderName;
for( int j = 1; j < 10; j++ )
{
holderName = object[j].name;
cmp = strcmp(holderName,hole);
cout << object[j].name <<endl;
if( cmp == 0)
{
holder = totalMemory;
for( int k = 1; k < i; k++ )
{
if( k != j || k < i )
holder = holder - object[k].size;
}
if( holder > size )
{
object[j].name = Name;
object[j].size = size;
object[j].location = object[j-1].location + object[j-1].size;
if( object[j+1].location - object[j].location - object[j].size > 1 )
swapObject( j );
cout << "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++" <<endl;
cout << object[j].name << " is being loaded at location "
<< object[j].location << ". Memory looks like: " << endl;
j = 1000;
}
}
}
}
void swapObject( int j )
{
char * name, * name2;
int size, location, size2, location2;
name = object[j+1].name;
size = object[j+1].size;
location = object[j+1].location;
object[j+1].name = "Hole";
object[j+1].location = object[j].location + object[j].size;
j++;
for( int k = j; k == i+1; k++ )
{
name2 = object[k+1].name;
size2 = object[k+1].size;
location2 = object[k+1].location;
object[k+1].name = name;
object[k+1].size = size;
object[k+1].location = location;
}
i++;
}
void Delete( char * name )
{
char *processName;
int cmp;
for ( int l = 2; l < 10; l++ )
{
processName = object[l].name;
cmp = strcmp(name,processName);
if( cmp == ZERO )
{
object[l].name = "Hole";
if( object[l+1].name == "Hole" )
{
object[l].size = object[l].size + object[l+1].size;
}
else if( object[l-1].name == "Hole" )
{
object[l-1].size = object[l-1].size + object[l].size;
}
cout << "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++" <<endl;
cout << object[l].name << "is being removed from location "
<< object[l].location << ". Memory looks like: " << endl;
}
}
}