943,358 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 4901
  • C++ RSS
Aug 3rd, 2003
0

** Need Help ** in a small C++ problem

Expand Post »
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****

C++ Syntax (Toggle Plain Text)
  1.  
  2. #include <string>
  3. #include <cstring>
  4. #include <iostream>
  5. #include <fstream>
  6. #include <cstdlib>
  7. #include <iomanip>
  8.  
  9.  
  10. #define TRUE 1
  11. #define FALSE 0
  12. #define ZERO 0
  13. #define ONE 1
  14. #define TWO 2
  15. #define THREE 3
  16. #define FOUR 4
  17. #define FIVE 5
  18.  
  19. void readingArguments( int argc, char *[FIVE] );
  20. void readFile( ifstream & );
  21. void swapObject( int j );
  22. void swapBack( int );
  23. void insert( char *, int );
  24. void Delete( char* );
  25.  
  26. int totalMemory;
  27. int operatingSystem;
  28. int firstFit;
  29. int worstFit;
  30. int i = 1;
  31.  
  32. struct memoryList{
  33. int size;
  34. int location;
  35. char *name;
  36. } object[100];
  37.  
  38. void main( int argc, char * argv[] )
  39. {
  40.  
  41. cout << "\n" << endl;
  42. cout << "This is line number " << __LINE__ << " of the file "
  43. << __FILE__ << " which was compiled on " << __DATE__
  44. << "\nat time " << __TIME__ << ".\n\n";
  45.  
  46. readingArguments( argc, argv );
  47.  
  48. }
  49.  
  50. void readingArguments( int argc, char * argv[FIVE] )
  51. {
  52.  
  53. if( argc != FIVE )
  54. {
  55. cerr << "Usage : a.out argv[1] argv[2] argv[3] argv[4] " << endl;
  56. exit(1);
  57. }
  58.  
  59. ifstream inFile( argv[ONE], ios::in );
  60.  
  61. if( !inFile )
  62. {
  63. cerr << "INPUT FILE CANNOT BE OPEN FOR INPUT" << endl;
  64. exit(2);
  65. }
  66. else
  67. {
  68. cout << "INPUT FILE : " << argv[ONE] << " OPEN FOR INPUT. \n " << endl;
  69. }
  70.  
  71. if( atoi(argv[TWO]) == atoi("F") || atoi(argv[TWO]) == atoi("f") )
  72. {
  73. firstFit = TRUE;
  74. cout << "*** First-Fit Is Selected ***\n" << endl;
  75. }
  76. else if ( atoi(argv[TWO]) == atoi("W") || atoi(argv[TWO]) == atoi("w") )
  77. {
  78. worstFit = TRUE;
  79. cout << "*** Worst-Fit Is Selected ***\n" << endl;
  80. }
  81. else
  82. {
  83. cout << " Usage argv[2] : F for First-Fit or W for Worst-Fit " << endl;
  84. exit(3);
  85. }
  86.  
  87. totalMemory = atoi( argv[THREE] );
  88. operatingSystem = atoi( argv[FOUR]);
  89.  
  90. readFile( inFile );
  91. }
  92.  
  93.  
  94. void readFile ( ifstream &inFile )
  95. {
  96.  
  97. char name[40], Status[17];
  98. char *status;
  99. char *enter = "Enter", *exit = "Exit";
  100. int memorySize, holder, cmp;
  101.  
  102. object[1].name = "OS";
  103. object[1].size = operatingSystem;
  104. object[1].location = 0;
  105.  
  106. for( int k = 2; k < 10; k++ )
  107. object[k].name = "Hole";
  108.  
  109. while ( inFile >> name >> Status >> memorySize )
  110. {
  111. status = Status;
  112. i++;
  113. cmp = strcmp(status,enter);
  114. if ( cmp == ZERO )
  115. insert( name, memorySize);
  116. cmp = strcmp(status,exit);
  117. // if ( cmp == ZERO )
  118. // Delete ( name );
  119.  
  120. for(int u = 1; u < 10; u++ )
  121. cout << object[u].name << " is loaded at address = " << object[u].location
  122. << " with size = " << object[u].size << "K\n" << endl;
  123.  
  124. }
  125.  
  126.  
  127. }
  128.  
  129. void insert( char *Name, int size )
  130. {
  131. int holder, cmp;
  132. char *hole = "Hole";
  133. char *holderName;
  134.  
  135. for( int j = 1; j < 10; j++ )
  136. {
  137. holderName = object[j].name;
  138. cmp = strcmp(holderName,hole);
  139. cout << object[j].name <<endl;
  140.  
  141. if( cmp == 0)
  142. {
  143. holder = totalMemory;
  144.  
  145. for( int k = 1; k < i; k++ )
  146. {
  147. if( k != j || k < i )
  148. holder = holder - object[k].size;
  149. }
  150. if( holder > size )
  151. {
  152. object[j].name = Name;
  153. object[j].size = size;
  154. object[j].location = object[j-1].location + object[j-1].size;
  155.  
  156. if( object[j+1].location - object[j].location - object[j].size > 1 )
  157. swapObject( j );
  158. cout << "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++" <<endl;
  159. cout << object[j].name << " is being loaded at location "
  160. << object[j].location << ". Memory looks like: " << endl;
  161.  
  162. j = 1000;
  163. }
  164. }
  165. }
  166. }
  167.  
  168. void swapObject( int j )
  169. {
  170. char * name, * name2;
  171. int size, location, size2, location2;
  172.  
  173. name = object[j+1].name;
  174. size = object[j+1].size;
  175. location = object[j+1].location;
  176.  
  177. object[j+1].name = "Hole";
  178. object[j+1].location = object[j].location + object[j].size;
  179.  
  180. j++;
  181.  
  182. for( int k = j; k == i+1; k++ )
  183. {
  184. name2 = object[k+1].name;
  185. size2 = object[k+1].size;
  186. location2 = object[k+1].location;
  187.  
  188. object[k+1].name = name;
  189. object[k+1].size = size;
  190. object[k+1].location = location;
  191.  
  192. }
  193.  
  194. i++;
  195. }
  196.  
  197. void Delete( char * name )
  198. {
  199. char *processName;
  200. int cmp;
  201.  
  202. for ( int l = 2; l < 10; l++ )
  203. {
  204. processName = object[l].name;
  205. cmp = strcmp(name,processName);
  206.  
  207. if( cmp == ZERO )
  208. {
  209. object[l].name = "Hole";
  210.  
  211. if( object[l+1].name == "Hole" )
  212. {
  213. object[l].size = object[l].size + object[l+1].size;
  214. }
  215. else if( object[l-1].name == "Hole" )
  216. {
  217. object[l-1].size = object[l-1].size + object[l].size;
  218. }
  219.  
  220. cout << "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++" <<endl;
  221. cout << object[l].name << "is being removed from location "
  222. << object[l].location << ". Memory looks like: " << endl;
  223. }
  224. }
  225.  
  226.  
  227. }
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Cardinal is offline Offline
1 posts
since Aug 2003

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: file unable to open
Next Thread in C++ Forum Timeline: About CD(Compact Disk) Writing





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC