Guest User

SO - http://stackoverflow.com/questions/27477416/c-error-wit

a guest
Dec 14th, 2014
351
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.96 KB | None | 0 0
  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include <stdio.h>
  3. #include <stdio_ext.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #define SIZE 2
  7.  
  8. struct Person
  9. {
  10.     char name[16], dep[16];
  11.     float cyi, ra, rp, npa, tyi, tra, tpa;
  12. };
  13.  
  14. void load(struct Person s[], int n)
  15. {
  16.     int i;
  17.     for (i = 0; i < n; i++)
  18.     {
  19.         printf("Enter your name   ");
  20.         fgets(s[i].name, 16, stdin);
  21.     printf("Enter your department   ");
  22.     fgets(s[i].dep, 16, stdin);
  23.     printf("Enter your current yearly income   $");
  24.     scanf("%f", &s[i].cyi);
  25.     printf("Enter your raise percentage   ");
  26.     scanf("%f", &s[i].rp);
  27.  
  28.     s[i].ra = (s[i].cyi * s[i].rp) / (float)100;
  29.     s[i].npa = (s[i].cyi) + (s[i].ra);
  30.     printf("\n");
  31.     __fpurge(stdin);
  32.     //fflush(stdin);
  33.     }
  34. }
  35.  
  36. void sort(struct Person s[], int n)
  37. {
  38.     int i, j;
  39.     struct Person t;
  40.     for (i = 0; i < n - 1; i++)
  41.     for (j = 0; j < n - 1; j++)
  42.         if (strcmp(s[j].name, s[j + 1].name) > 0)
  43.         {
  44.         t = s[j];
  45.         s[j] = s[j + 1];
  46.         s[j + 1] = t;
  47.         }
  48. }
  49.  
  50. void print(struct Person s[], int n)
  51. {
  52.     int i;
  53.     printf("\n\n");
  54.     for (i = 0; i < n; i++)
  55.     {
  56.         printf("%s in department %s\n", s[i].name, s[i].dep);
  57.         printf("The current yearly income is $%0.2f the raise percentage is %0.2f%%\n", s[i].cyi, s[i].rp);
  58.         printf("The raise amount is $%0.2f, the new pay amount is $%0.2f\n\n", s[i].ra, s[i].npa);
  59.     }
  60. }
  61.  
  62. void calc(struct Person s[], int n)
  63. {
  64.     int i;
  65.     float tyi = 0, tra = 0, tpa = 0;
  66.     for (i = 0; i < n; i++)
  67.     {
  68.         tyi += s[i].cyi;
  69.         tra += s[i].ra;
  70.         tpa += s[i].npa;
  71.     }
  72.     printf("The total current yearly income is $%0.2f\n", tyi);
  73.     printf("The total raise amount is $%0.2f\n", tra);
  74.     printf("The total new pay amount is $%0.2f\n", tpa);
  75. }
  76.  
  77. void savetext(struct Person s[], int n)
  78. {
  79.     int i;
  80.     FILE *f;
  81.     f = fopen("G:\\College\\CS 36\\Projects\\Final Program\\FinalHomework\\info.txt", "w");
  82.     for (i = 0; i < n; i++)
  83.     {
  84.         fprintf(f, "%s\n", s[i].name);
  85.         fprintf(f, "%s\n", s[i].dep);
  86.         fprintf(f, "%f %f %f\n", s[i].cyi, s[i].rp, s[i].npa);
  87.     }
  88.     fclose(f);
  89. }
  90.  
  91. void retrievetext(struct Person s[], int n)
  92. {
  93.     int i;
  94.     FILE *f;
  95.     f = fopen("G:\\College\\CS 36\\Projects\\Final Program\\FinalHomework\\info.txt", "r");
  96.     for (i = 0; i < n; i++);
  97.     {
  98.         fgets(s[i].name, sizeof(s[i].name), f);
  99.         fgets(s[i].dep, sizeof(s[i].dep), f);
  100.         fscanf(f, "%f%f%f\n", &s[i].cyi, &s[i].rp, &s[i].npa);
  101.     }
  102.     fclose(f);
  103. }
  104.  
  105. void savebin(struct Person s[], int n)
  106. {
  107.     FILE *f;
  108.     f = fopen("G:\\College\\CS 36\\Projects\\Final Program\\FinalHomework\\info.bin", "wb");
  109.     fwrite(&s, sizeof(s[0]), n, f);
  110.     fclose(f);
  111. }
  112.  
  113. void retrievebin(struct Person s[], int n)
  114. {
  115.     FILE *f;
  116.     f = fopen("G:\\College\\CS 36\\Projects\\Final Program\\FinalHomework\\info.bin", "rb");
  117.     fread(&s, sizeof(s[0]), n, f);
  118.     fclose(f);
  119. }
  120.  
  121. int main()
  122. {
  123.     struct Person s[SIZE];
  124.     load(s, SIZE);
  125.     sort(s, SIZE);
  126.     print(s, SIZE);
  127.     calc(s, SIZE);
  128.     savetext(s, SIZE);
  129.     retrievetext(s, SIZE);
  130.     printf("\nAfter the text file is retrieved\n");
  131.     print(s, SIZE);
  132.     savebin(s, SIZE);
  133.     retrievebin(s, SIZE);
  134.     printf("\nAfter the binary file is retrieved\n");
  135.     print(s, SIZE);
  136.     system("PAUSE");
  137. return 0;
  138. }
  139.  
  140.  
  141.  
  142.  
  143.  
  144. /* Output */
  145. /*
  146. Enter your name   Joe
  147. Enter your department   Sales
  148. Enter your current yearly income   $1000
  149. Enter your raise percentage   10
  150.  
  151. Enter your name   Alex
  152. Enter your department   IT
  153. Enter your current yearly income   $2000
  154. Enter your raise percentage   12
  155.  
  156.  
  157.  
  158. Alex
  159.  in department IT
  160.  
  161. The current yearly income is $2000.00 the raise percentage is 12.00%
  162. The raise amount is $240.00, the new pay amount is $2240.00
  163.  
  164. Joe
  165.  in department Sales
  166.  
  167. The current yearly income is $1000.00 the raise percentage is 10.00%
  168. The raise amount is $100.00, the new pay amount is $1100.00
  169.  
  170. The total current yearly income is $3000.00
  171. The total raise amount is $340.00
  172. The total new pay amount is $3340.00
  173.  
  174. After the text file is retrieved
  175.  
  176.  
  177. Alex
  178.  in department IT
  179.  
  180. The current yearly income is $2000.00 the raise percentage is 12.00%
  181. The raise amount is $240.00, the new pay amount is $2240.00
  182.  
  183. Joe
  184.  in department Sales
  185.  
  186. The current yearly income is $1000.00 the raise percentage is 10.00%
  187. The raise amount is $100.00, the new pay amount is $1100.00
  188.  
  189.  
  190. After the binary file is retrieved
  191.  
  192.  
  193. Alex
  194.  in department IT
  195.  
  196. The current yearly income is $2000.00 the raise percentage is 12.00%
  197. The raise amount is $240.00, the new pay amount is $2240.00
  198.  
  199. Joe
  200.  in department Sales
  201.  
  202. The current yearly income is $1000.00 the raise percentage is 10.00%
  203. The raise amount is $100.00, the new pay amount is $1100.00
  204.  
  205.  
  206. //Cat Output
  207. $ cat G\:\\College\\CS\ 36\\Projects\\Final\ Program\\FinalHomework\\info.txt
  208. Alex
  209.  
  210. IT
  211.  
  212. 2000.000000 12.000000 2240.000000
  213. Joe
  214.  
  215. Sales
  216.  
  217. 1000.000000 10.000000 1100.000000
  218.  
  219.  
  220.  
  221.  
  222. */
Add Comment
Please, Sign In to add comment