error in the program

Reply

Join Date: Oct 2006
Posts: 8
Reputation: lavicool is an unknown quantity at this point 
Solved Threads: 0
lavicool lavicool is offline Offline
Newbie Poster

error in the program

 
0
  #1
Dec 10th, 2006
Hi All,

Thanks for all your help and advise. Here is a code for printing sales record. I don't understand what is the error in this code. When I try to complie and run, it says it cannot open the "sper.dat" file. Please help. Here is the 2 input files

sales.dat file

943 BusterBurger 4650000.00 1432567.88
245 PetRocks 8130000.00 95679.10

Salesper.dat file

Johnsonbaugh Epp IL 98450.00 29324.18 # 127 812
Cheessare Cruz TX 102300.00 15671.90 # 245 943

The output should be

Sales report by salesperson:

Salesperson: Chessare
Sales coach: Cruz
Sales region: TX
Base salary and bonus: 102300.00 15671.90
2 Account(s):

Name: PetRocks
Id: 245
Projected: 8130000.00
Actual: 95679.10

Name: BusterBurger
Id: 943
Projected: 4650000.00
Actual: 1432567.88

Salesperson: Johnsonbaugh
Sales coach: Epp
Sales region: IL
Base salary and bonus: 98450.00 29324.18
2 Account(s):

Name: AceRecords
Id: 127
Projected: 6780000.00
Actual: 2762890.50

Name: TacosAndThings
Id: 812
Projected: 2345000.00
Actual: 1412987.50

Here is the code
  1. /***** BEGIN sales.h *****/
  2.  
  3. /** structure member macros **/
  4. #define MaxName (100) /* last name */
  5. #define MaxReg (2) /* 2 character region code */
  6. #define MaxYear (4) /* current year */
  7. #define MaxAccs (100) /* maximum number of accounts */
  8.  
  9. /** array sizes **/
  10. #define MaxSales (100)
  11. #define MaxSalesPer (100)
  12.  
  13. /** input file names **/
  14. #define SalesFile "sales.dat"
  15. #define SalesPerFile "sper.dat"
  16.  
  17. /** miscellany **/
  18. #define MaxBuffer (500) /* input buffer */
  19. #define AccDel '#' /* delimits accounts from other fields */
  20.  
  21. typedef struct sales {
  22. int accId; /* account id */
  23. char accName[ MaxName + 1 ]; /* account name */
  24. float act; /* actual sales */
  25. float proj; /* projected sales */
  26. } Sales;
  27.  
  28. typedef struct salesPer {
  29. Sales* accs[ MaxAccs ]; /* accounts */
  30. int accCnt; /* accounts count */
  31. char name[ MaxName + 1 ]; /* salesperson */
  32. char boss[ MaxName + 1 ]; /* supervisor */
  33. char reg[ MaxReg + 1 ]; /* sales region */
  34. float base; /* base salary */
  35. float bonus; /* year to date */
  36. } SalesPer;
  37.  
  38. /***** END sales.h *****/
  39.  
  40. /***** BEGIN sales.c *****/
  41. #include <stdio.h>
  42. #include <stdlib.h>
  43. #include <string.h>
  44.  
  45.  
  46. /** arrays **/
  47. Sales salesTbl[ MaxSales ]; /* sales by account */
  48. Sales* salesPtr[ MaxSales ]; /* pointers to salesTbl */
  49. SalesPer salesPerTbl[ MaxSalesPer ]; /* sales personnel */
  50. SalesPer* salesPerPtr[ MaxSalesPer ]; /* pointers to salesPerTbl */
  51.  
  52. /** array counts **/
  53. int salesCnt, salesPerCnt;
  54.  
  55. /** function declarations **/
  56. void
  57. readSales( const char* infile ),
  58. readSalesPer( const char* infile ),
  59. quicksort( void* array,
  60. size_t count,
  61. size_t size,
  62. int ( *comp )( const void* e1, const void* e2 ) ),
  63. report( void );
  64. void*
  65. findPivot( const void* array,
  66. size_t count,
  67. size_t size,
  68. int ( *comp )( const void* e1, const void* e2 ) );
  69. int
  70. partition( void* array,
  71. size_t count,
  72. size_t size,
  73. int ( *comp )( const void* e1, const void* e2 ),
  74. void* pivot ),
  75. compAccId( const void* e1, const void* e2 ),
  76. compName( const void* e1, const void* e2 );
  77. FILE*
  78. openOrExit( const char* file, const char* mode );
  79.  
  80. main()
  81. {
  82. /* read sales data and sort */
  83. readSales( SalesFile );
  84. quicksort( salesPtr,
  85. salesCnt,
  86. sizeof ( salesPtr[ 0 ] ),
  87. compAccId );
  88.  
  89. /* read salesperson data and sort */
  90. readSalesPer( SalesPerFile );
  91. quicksort( salesPerPtr,
  92. salesPerCnt,
  93. sizeof ( salesPerPtr[ 0 ] ),
  94. compName );
  95.  
  96. /* print sales report */
  97. report();
  98.  
  99. return EXIT_SUCCESS;
  100. }
  101.  
  102. /* Read data into array salesTbl and set SalesCnt */
  103. void readSales( const char* infile )
  104. {
  105. FILE* fptr;
  106. int i = 0;
  107. char buffer[ MaxBuffer + 2 ]; /* input buffer */
  108.  
  109. fptr = openOrExit( infile, "rb" );
  110. salesCnt = 0;
  111.  
  112. while ( fgets( buffer, MaxBuffer, fptr ) ) {
  113. sscanf( buffer, "%d %s %f %f",
  114. &salesTbl[ i ].accId,
  115. salesTbl[ i ].accName,
  116. &salesTbl[ i ].act,
  117. &salesTbl[ i ].proj );
  118. i++;
  119. }
  120. fclose( fptr );
  121. salesCnt = i;
  122.  
  123. /* Set pointers in salesPtr to addresses of
  124.   * corresponding elements in salesTbl.
  125.   */
  126. for ( i = 0; i < salesCnt; i++ )
  127. salesPtr[ i ] = &salesTbl[ i ];
  128. }
  129.  
  130. /* Read data into array salesPerTbl and set SalesPerCnt */
  131. void readSalesPer( const char* infile )
  132. {
  133. FILE *fptr;
  134. int i = 0, j;
  135. char* del;
  136. char id[ MaxName + 1 ];
  137. char buffer[ MaxBuffer + 2 ]; /* input buffer */
  138. Sales temp, *tempPtr = &temp, **ret;
  139.  
  140. fptr = openOrExit( infile, "rb" );
  141. salesPerCnt = 0;
  142.  
  143. while ( fgets( buffer, MaxBuffer, fptr ) ) {
  144. /* Read basic fields. */
  145. sscanf( buffer, "%s %s %s %f %f",
  146. salesPerTbl[ i ].name,
  147. salesPerTbl[ i ].boss,
  148. salesPerTbl[ i ].reg,
  149. &salesPerTbl[ i ].base,
  150. &salesPerTbl[ i ].bonus );
  151.  
  152. /*** read account ids and set pointers to Sales structures ***/
  153. salesPerTbl[ i ].accCnt = 0;
  154. j = 0;
  155. /* find accounts delimiter within buffer */
  156. del = strrchr( buffer, AccDel );
  157. del++;
  158. while ( *del && sscanf( del, "%s", id ) != EOF ) {
  159. tempPtr -> accId = atoi( id );
  160. del += strlen( id ) + 1; /* move beyond current id */
  161. /* Search for sales record by account id. */
  162. if ( ret = bsearch( &tempPtr,
  163. salesPtr,
  164. salesCnt,
  165. sizeof ( salesPtr[ 0 ] ),
  166. compAccId ) ) {
  167. salesPerTbl[ i ].accCnt++;
  168. salesPerTbl[ i ].accs[ j++ ] = *ret;
  169. }
  170. }
  171. i++;
  172. }
  173. fclose( fptr );
  174. salesPerCnt = i;
  175.  
  176. /* Copy address of sales table entries
  177.   * into pointer array.
  178.   */
  179. for ( i = 0; i < salesPerCnt; i++ )
  180. salesPerPtr[ i ] = &salesPerTbl[ i ];
  181. }
  182.  
  183. /* Open file in specified mode, returning
  184.  * file pointer if successfull; otherwise,
  185.  * issue warning message and exit program.
  186.  */
  187. FILE *openOrExit( const char* file, const char* mode )
  188. {
  189. FILE *fptr;
  190. if ( NULL == ( fptr = fopen( file, mode ) ) ) {
  191. fprintf( stderr,
  192. "!! Can't open %s in mode %s. Exiting. !!\n",
  193. file, mode );
  194. exit( EXIT_SUCCESS );
  195. }
  196. return fptr;
  197. }
  198.  
  199. void quicksort( void* array,
  200. size_t count,
  201. size_t size,
  202. int ( *comp )( const void* e1, const void* e2 ) )
  203. {
  204. void* pivot;
  205. int i;
  206.  
  207. /* If array not yet sorted:
  208.   * (1) find a pivot
  209.   * (2) partition into two subarrays, left and right
  210.   * (3) quicksort the left subarray
  211.   * (4) quicksort the right subarray
  212.   */
  213. if ( pivot = findPivot( array, count, size, comp ) ) {
  214. i = partition( array, count, size, comp, pivot );
  215. quicksort( array, i, size, comp );
  216. quicksort( ( char* ) array + i * size, count - i, size, comp );
  217. }
  218. }
  219.  
  220. /* Return the larger of the first element and any
  221.  * other that differs from it. If all elements the
  222.  * same, return NULL.
  223.  */
  224. void* findPivot( const void* array,
  225. size_t count,
  226. size_t size,
  227. int ( *comp )( const void* e1, const void* e2 ) )
  228. {
  229. int i = 1, flag;
  230. void* next;
  231.  
  232. while ( i < count ) {
  233. next = ( char* ) array + i * size;
  234. flag = comp( array, next );
  235. if ( flag > 0 ) /* array[ 0 ] > array[ i ] */
  236. return array;
  237. else if ( flag < 0 ) /* array[ i ] > array[ 0 ] */
  238. return next;
  239. else /* array[ 0 ] == array[ i ] */
  240. i++;
  241. }
  242. return NULL; /* all elements the same */
  243. }
  244.  
  245. /* Partition an array into two subarrays, the first
  246.  * holding elements <= pivot and the second holding
  247.  * elements > pivot.
  248.  */
  249. int partition( void* array,
  250. size_t count,
  251. size_t size,
  252. int ( *comp )( const void* e1, const void* e2 ),
  253. void* pivot )
  254. {
  255. int left = 0, right = count - 1, n = count;
  256. void* next;
  257. char temp[ MaxBuffer ];
  258.  
  259. if ( size >= MaxBuffer ) {
  260. fprintf( stderr,
  261. "\n!! MaxBuffer too small. Exiting. !!\n\n" );
  262. exit( EXIT_SUCCESS );
  263. }
  264.  
  265. while ( left <= right ) {
  266.  
  267. /* Search from left to right for 1st element >= pivot. */
  268. next = ( char* ) array + left * size;
  269. while ( left < n && comp( next, pivot ) < 0 ) {
  270. left++;
  271. next = ( char* ) array + left * size;
  272. }
  273.  
  274. /* Search from right to left for 1st element < pivot. */
  275. next = ( char* ) array + right * size;
  276. while ( right >= 0 && comp( next, pivot ) >= 0 ) {
  277. right--;
  278. next = ( char* ) array + right * size;
  279. }
  280.  
  281. /* Swap current left and right elements to put them in
  282.   * relative order with respect to each other and pivot.
  283.   */
  284. if ( left < right ) {
  285. char *p1 = ( char* ) array + left * size,
  286. *p2 = ( char* ) array + right * size;
  287. memcpy( temp, p1, size );
  288. memcpy( p1, p2, size );
  289. memcpy( p2, temp, size );
  290. left++;
  291. right--;
  292. }
  293. }
  294. return left;
  295. }
  296.  
  297. int compAccId( const void* e1, const void* e2 )
  298. {
  299. Sales
  300. *t1 = *( ( Sales** ) e1 ),
  301. *t2 = *( ( Sales** ) e2 );
  302. return t1 -> accId - t2 -> accId;
  303. }
  304.  
  305. int compName( const void* e1, const void* e2 )
  306. {
  307. SalesPer
  308. *t1 = *( ( SalesPer** ) e1 ),
  309. *t2 = *( ( SalesPer** ) e2 );
  310. return strcmp( t1 -> name, t2 -> name );
  311. }
  312.  
  313. void report( void )
  314. {
  315. int i, j;
  316.  
  317. printf( "\nSales report by salesperson:\n\n" );
  318. for ( i = 0; i < salesPerCnt; i++ ) {
  319. printf( "\n\tSalesperson: %s\n",
  320. salesPerPtr[ i ] -> name );
  321. printf( "\tSales coach: %s\n",
  322. salesPerPtr[ i ] -> boss );
  323. printf( "\tSales region: %s\n",
  324. salesPerPtr[ i ] -> reg );
  325. printf( "\tBase salary and bonus: %.2f %.2f\n",
  326. salesPerPtr[ i ] -> base, salesPerPtr[ i ] -> bonus );
  327. printf( "\t%d Account(s): \n",
  328. salesPerPtr[ i ] -> accCnt );
  329. for ( j = 0; j < salesPerPtr[ i ] -> accCnt; j++ ) {
  330. printf( "\n" );
  331. printf( "\t\tName: %s\n",
  332. salesPerPtr[ i ] -> accs[ j ] -> accName );
  333. printf( "\t\tId: %d\n",
  334. salesPerPtr[ i ] -> accs[ j ] -> accId );
  335. printf( "\t\tProjected: %.2f\n",
  336. salesPerPtr[ i ] -> accs[ j ] -> proj );
  337. printf( "\t\tActual: %.2f\n",
  338. salesPerPtr[ i ] -> accs[ j ] -> act );
  339. }
  340. }
  341. }
  342. /***** END sales.c *****/
Last edited by ~s.o.s~; Dec 10th, 2006 at 12:46 pm. Reason: Added code tags learn to use them yourself.
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: error in the program

 
0
  #2
Dec 10th, 2006
> #define SalesPerFile "sper.dat"
Seems pretty obvious, this is the name you used inside the program. Simply change it to match the actual name you've given the file.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 8
Reputation: lavicool is an unknown quantity at this point 
Solved Threads: 0
lavicool lavicool is offline Offline
Newbie Poster

Re: error in the program

 
0
  #3
Dec 10th, 2006
Thanks for the reply. The file name given in the program is same as that I created. But still it gives me the same error
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: error in the program

 
0
  #4
Dec 11th, 2006
Did you recompile it?
Is the file in the right directory?

Typically when running from the IDE, the programs idea of "current directory" can be different to what you get when running the program from the command line.
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 13
Reputation: jaguar founder is an unknown quantity at this point 
Solved Threads: 0
jaguar founder jaguar founder is offline Offline
Newbie Poster

Re: error in the program

 
0
  #5
Dec 11th, 2006
OH! look now only if it were for a mmo And it could be sent to the server

/cheer!
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 13
Reputation: jaguar founder is an unknown quantity at this point 
Solved Threads: 0
jaguar founder jaguar founder is offline Offline
Newbie Poster

Re: error in the program

 
0
  #6
Dec 11th, 2006
Dont u need return 0;? or is that only for console ah?
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 5,050
Reputation: John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold 
Solved Threads: 332
Team Colleague
John A's Avatar
John A John A is offline Offline
Vampirical Lurker

Re: error in the program

 
0
  #7
Dec 11th, 2006
Originally Posted by jaguar founder View Post
Dont u need return 0;? or is that only for console ah?
Returning EXIT_SUCCESS is usually 0, which is the value that should be returned to the operating system by a program to indicate success. However, since EXIT_SUCCESS is not guaranteed to be 0, the only way to be sure that your code is completely portable is to return 0 when the program exits.

Many people still try to use void main, which doesn't mean that the program doesn't return a value, it just returns some random junk. So void main() has never been correct.
"Technological progress is like an axe in the hands of a pathological criminal."
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: error in the program

 
1
  #8
Dec 12th, 2006
> However, since EXIT_SUCCESS is not guaranteed to be 0
No, but whatever it's value it, it will be interpreted as success in the calling environment, even if that value is non-zero.

Even return 0; in your program can be converted into some non-zero success value in the host environment (if you're using a VAX say).
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,591
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 710
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: error in the program

 
1
  #9
Dec 12th, 2006
>the only way to be sure that your code is completely portable is to return 0 when the program exits.
EXIT_SUCCESS is completely portable. The language standard defines it as such, along with EXIT_FAILURE and 0.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 5,050
Reputation: John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold 
Solved Threads: 332
Team Colleague
John A's Avatar
John A John A is offline Offline
Vampirical Lurker

Re: error in the program

 
0
  #10
Dec 12th, 2006
Crap, that's what I get for believing someone on another forum rather than using an official source.
"Technological progress is like an axe in the hands of a pathological criminal."
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC