-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathExercise 9.2 (Completed)
1146 lines (999 loc) · 34.1 KB
/
Exercise 9.2 (Completed)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1. (Practice and Modify) a. Enter and run Program 9.5.
#include <iostream>
#include <fstream>
#include <cstdlib> // needed for exit()
#include <string> // needed for the string class
using namespace std;
int main()
{
string filename = "prices.dat"; // put the filename up front
string descrip;
double price;
ifstream inFile;
inFile.open(filename.c_str());
if (inFile.fail()) // check for successful open
{
cout << "\nThe file was not successfully opened"
<< "\n Please check that the file currently exists."
<< endl;
system("PAUSE");
exit(1);
}
// read and display the file's contents
inFile >> descrip >> price;
while (inFile.good()) // check next character
{
cout << descrip << ' ' << price << endl;
inFile >> descrip >> price;
}
inFile.close();
system("PAUSE");
return 0;
}
b. Modify Program 9.5 to use the expression !inFile.eof() in place of the expression
inFile.good(), and run the program to see whether it operates correctly.
#include <iostream>
#include <fstream>
#include <cstdlib> // needed for exit()
#include <string> // needed for the string class
using namespace std;
int main()
{
string filename = "prices.dat"; // put the filename up front
string descrip;
double price;
ifstream inFile;
inFile.open(filename.c_str());
if (inFile.fail()) // check for successful open
{
cout << "\nThe file was not successfully opened"
<< "\n Please check that the file currently exists."
<< endl;
system("PAUSE");
exit(1);
}
// read and display the file's contents
inFile >> descrip >> price;
while (!inFile.eof()) // check next character
{
cout << descrip << ' ' << price << endl;
inFile >> descrip >> price;
}
inFile.close();
system("PAUSE");
return 0;
}
2. (Practice and Modify) a. Enter and run Program 9.6.
#include <iostream>
#include <fstream>
#include <cstdlib> // needed for exit()
#include <string> // needed for the string class
using namespace std;
int main()
{
string filename = "prices.dat"; // put the filename up front
string line;
ifstream inFile;
inFile.open(filename.c_str());
if (inFile.fail()) // check for successful open
{
cout << "\nThe file was not successfully opened"
<< "\n Please check that the file currently exists."
<< endl;
exit(1);
}
// read and display the file's contents
while (getline(inFile, line))
cout << line << endl;
inFile.close();
return 0;
}
b. Modify Program 9.6 by replacing cout with cerr, and verify that the output for the standard
error file stream is the screen.
#include <iostream>
#include <fstream>
#include <cstdlib> // needed for exit()
#include <string> // needed for the string class
using namespace std;
int main()
{
string filename = "prices.dat"; // put the filename up front
string line;
ifstream inFile;
inFile.open(filename.c_str());
if (inFile.fail()) // check for successful open
{
cerr << "\nThe file was not successfully opened"
<< "\n Please check that the file currently exists."
<< endl;
exit(1);
}
// read and display the file's contents
while (getline(inFile, line))
cerr << line << endl;
inFile.close();
return 0;
}
c. Modify Program 9.6 by replacing cout with clog, and verify that the output for the standard
log stream is the screen.
#include <iostream>
#include <fstream>
#include <cstdlib> // needed for exit()
#include <string> // needed for the string class
using namespace std;
int main()
{
string filename = "prices.dat"; // put the filename up front
string line;
ifstream inFile;
inFile.open(filename.c_str());
if (inFile.fail()) // check for successful open
{
clog<< "\nThe file was not successfully opened"
<< "\n Please check that the file currently exists."
<< endl;
system("PAUSE");
exit(1);
}
// read and display the file's contents
while (getline(inFile, line))
clog << line << endl;
inFile.close();
system("PAUSE");
return 0;
}
3. (Practice and Modify) a. Write a C++ program that accepts lines of text from the keyboard
and writes each line to a file named text.dat until an empty line is entered. An empty line
is a line with no text that’s created by pressing the Enter (or Return) key.
#include <iostream>
#include <fstream>
#include <cstdlib> // needed for exit()
#include <string> // needed for the string class
#include <iomanip> // needed for formatting
using namespace std;
int main()
{
string filename = "text.dat"; // put the filename up front
ofstream outFile;
ifstream inFile;
string i;
outFile.open(filename.c_str());
inFile.open(filename.c_str());
if (outFile.fail())
{
cout << "The file was not successfully opened" << endl;
exit(1);
}
// set the output file stream formats
inFile >> setiosflags(ios::fixed)
>> setiosflags(ios::showpoint)
>> setprecision(2);
// send data to the file
cout << "Enter the words" << endl;
cin >> i;
inFile >> i;
outFile << i;
inFile.close();
outFile.close();
cout << "The file " << filename
<< " has been successfully written." << endl;
system("PAUSE");
return 0;
}
b. Modify Program 9.6 to read and display the data stored in the text.dat file created in
Exercise 3a.
#include <iostream>
#include <fstream>
#include <cstdlib> // needed for exit()
#include <string> // needed for the string class
#include <iomanip> // needed for formatting
using namespace std;
int main()
{
string filename = "text.dat"; // put the filename up front
ofstream outFile;
ifstream inFile;
string i;
outFile.open(filename.c_str());
inFile.open(filename.c_str());
if (outFile.fail())
{
cout << "The file was not successfully opened" << endl;
exit(1);
}
// set the output file stream formats
inFile >> setiosflags(ios::fixed)
>> setiosflags(ios::showpoint)
>> setprecision(2);
// send data to the file
cout << "Enter the words" << endl;
cin >> i;
inFile >> i;
cout << i << endl;
outFile << i;
inFile.close();
outFile.close();
cout << "The file " << filename
<< " has been successfully written." << endl;
system("PAUSE");
return 0;
}
4. (Practice) Determine the OS command or procedure your computer provides to display the
contents of a saved file.
The OS comand for windows is "type hope.txt" or "start hope.txt" they will open the contents of a saved file.
5. (Program) a. Create a text file named employee.dat containing the following data:
Anthony A 10031 11.82 12/18/2010
Burrows W 10067 12.14 6/9/2011
Fain B 10083 10.79 5/18/2011
Janney P 10095 12.57 9/28/2008
Smith G 10105 9.50 12/20/2006
#include <iostream>
#include <fstream>
#include <cstdlib> // needed for exit()
#include <string> // needed for the string class
#include <iomanip> // needed for formatting
using namespace std;
int main()
{
string filename = "employee.dat"; // put the filename up front
ofstream outFile;
outFile.open(filename.c_str());
if (outFile.fail())
{
cout << "The file was not successfully opened" << endl;
exit(1);
}
// send data to the file
outFile << "Anthony " << setw(5) << 'A' << setw(10) << 10031 << setw(5) << 11.32 << setw(5) << 12 <<'/'<< 18 <<'/'<< 2010 << endl
<< "Burrows " << setw(5) << 'W' << setw(10) << 10067 << setw(5) << 12.14 << setw(5) << 6 <<'/'<< 9 <<'/'<< 2011 << endl
<< "Fain " << setw(5) << 'B' << setw(10) << 10083 << setw(5) << 10.79 << setw(5) << 5 << '/'<< 18 <<'/'<< 2011 << endl
<< "Janney" << setw(5) << 'P' << setw(10) << 10095 << setw(5) << 12.57 << setw(5) << 9 << '/'<< 28 <<'/'<< 2008 << endl
<< "Smith" << setw(5) << 'G' << setw(10) << 10105 << setw(5) << 9.50 << setw(5) << 12 <<'/'<< 20 <<'/'<< 2006 << endl;
outFile.close();
cout << "The file " << filename
<< " has been successfully written." << endl;
system("PAUSE");
return 0;
}
b. Write a C++ program to read the employee.dat file created in Exercise 5a and produce a
duplicate copy of the file named employee.bak.
#include <iostream>
#include <fstream>
#include <cstdlib> // needed for exit()
#include <string> // needed for the string class
#include <iomanip> // needed for formatting
using namespace std;
int main()
{
string filename2 = "employee.dak";
string filename = "employee.dat"; // put the filename up front
string line;
ifstream inFile;
ifstream inFile2;
ofstream outFile;
inFile.open(filename.c_str());
inFile2.open(filename2.c_str());
outFile.open(filename2.c_str());
if (inFile.fail()) // check for successful open
{
cout << "\nThe file was not successfully opened"
<< "\n Please check that the file currently exists."
<< endl;
exit(1);
}
// read and copy the file's contents
while (getline(inFile, line))
outFile << line;
inFile2.close();
inFile.close();
outFile.close();
system("PAUSE");
return 0;
}
c. Modify the program written in Exercise 5b to accept the names of the original and duplicate
files as user input.
#include <iostream>
#include <fstream>
#include <cstdlib> // needed for exit()
#include <string> // needed for the string class
#include <iomanip> // needed for formatting
using namespace std;
int main()
{
//string filename2 = "employee.dak";
//string filename = "employee.dat"; // put the filename up front
string filename, filename2;
cout << "Enter original file name" << endl;
cin >> filename;
cout << "Enter duplicate file name" << endl;
cin >> filename2;
string line;
ifstream inFile;
ifstream inFile2;
ofstream outFile;
inFile.open(filename.c_str());
inFile2.open(filename2.c_str());
outFile.open(filename2.c_str());
if (inFile.fail()) // check for successful open
{
cout << "\nThe file was not successfully opened"
<< "\n Please check that the file currently exists."
<< endl;
exit(1);
}
// read and copy the file's contents
while (getline(inFile, line))
outFile << line;
inFile2.close();
inFile.close();
outFile.close();
system("PAUSE");
return 0;
d. The program written for Exercise 5c always copies data from an original file to a duplicate
file. What’s a better method of accepting the original and duplicate filenames, other than
prompting the user for them each time the program runs?
Hard coding the files names works better because it removes the step of inputing the names manually.
6. (Program) a. Write a C++ program that opens a file and displays its contents with line numbers.
That is, the program should print the number 1 before displaying the first line, print the
number 2 before displaying the second line, and so on for each line in the file.
#include <iostream>
#include <fstream>
#include <cstdlib> // needed for exit()
#include <string> // needed for the string class
#include <iomanip> // needed for formatting
using namespace std;
int main()
{
string filename = "employee.dat"; // put the filename up front
string line;
ifstream inFile;
ofstream outFile;
inFile.open(filename.c_str());
outFile.open(filename.c_str());
if (inFile.fail()) // check for successful open
{
cout << "\nThe file was not successfully opened"
<< "\n Please check that the file currently exists."
<< endl;
exit(1);
}
// read and copy the file's contents
for (int i = 1; i < 5; i++){
cin >> line;
inFile >> i >> line;
outFile << i << line << endl;
}
inFile.close();
outFile.close();
system("PAUSE");
return 0;
}
b. Modify the program written in Exercise 6a to list the file’s contents on the printer assigned
to your computer.
#include <iostream>
#include <fstream>
#include <cstdlib> // needed for exit()
#include <string> // needed for the string class
#include <iomanip> // needed for formatting
using namespace std;
int main()
{
string filename = "employee.dat"; // put the filename up front
string line;
ifstream inFile;
ofstream outFile;
inFile.open(filename.c_str());
outFile.open(filename.c_str());
outFile.open("prn"); // This command is outdated.
if (inFile.fail()) // check for successful open
{
cout << "\nThe file was not successfully opened"
<< "\n Please check that the file currently exists."
<< endl;
exit(1);
}
// read and copy the file's contents
for (int i = 1; i < 5; i++){
cin >> line;
inFile >> i >> line;
outFile << i << line << endl;
}
inFile.close();
outFile.close();
system("PAUSE");
return 0;
}
7. (Program) a. Create a text file named info.dat containing the following data (without the
headings):
Name Social Security Number Hourly Rate Hours Worked
B Caldwell 555-88-2222 10.50 37
D Memcheck 555-77-4444 12.80 40
R Potter 555-77-6666 16.54 40
W Rosen 555-99-8888 11.80 35
#include <iostream>
#include <fstream>
#include <cstdlib> // needed for exit()
#include <string> // needed for the string class
#include <iomanip> // needed for formatting
using namespace std;
int main()
{
string filename = "info.dat"; // put the filename up front
ofstream outFile;
outFile.open(filename.c_str());
if (outFile.fail())
{
cout << "The file was not successfully opened" << endl;
exit(1);
}
// send data to the file
outFile << "B Caldwell" << setw(10) << 555<< "-" <<88<<"-" <<2222 << setw(10) << 10.50 << setw(10) << 37 << endl
<< "D Memcheck " << setw(10) << 555 << "-" << 77 << "-" << 4444 << setw(10) << 12.80 << setw(10) << 40 << endl
<< "R Potter " << setw(10) << 555 << "-" << 77 << "-" << 6666 << setw(10) << 16.54 << setw(10) << 40 << endl
<< "W Roser" << setw(10) << 555 << "-" << 99 << "-" << 8888 << setw(10) << 11.80 << setw(10) << 35 << endl;
outFile.close();
cout << "The file " << filename
<< " has been successfully written." << endl;
system("PAUSE");
return 0;
}
b. Write a C++ program that reads the data file created in Exercise 7a and computes and displays
a payroll schedule. The output should list the Social Security number, name, and gross
pay for each person, calculating gross pay as Hourly Rate × Hours Worked.
#include <iostream>
#include <fstream>
#include <cstdlib> // needed for exit()
#include <string> // needed for the string class
#include <iomanip> // needed for formatting
using namespace std;
int main()
{
string filename = "info.dat"; // put the filename up front
ofstream outFile;
outFile.open(filename.c_str());
if (outFile.fail())
{
cout << "The file was not successfully opened" << endl;
exit(1);
}
// send data to the file
double money1 = 10.50 * 37;
double money2 = 12.80 * 40;
double money3 = 16.54 * 40;
double money4 = 11.80 * 35;
outFile << "B Caldwell" << setw(10) << 555<< "-" <<88<<"-" <<2222 << setw(10) << money1 << endl
<< "D Memcheck " << setw(10) << 555 << "-" << 77 << "-" << 4444 << setw(10) << money2 << endl
<< "R Potter " << setw(10) << 555 << "-" << 77 << "-" << 6666 << setw(10) << money3 << endl
<< "W Roser" << setw(10) << 555 << "-" << 99 << "-" << 8888 << setw(10) << money4 << endl;
outFile.close();
cout << "The file " << filename
<< " has been successfully written." << endl;
system("PAUSE");
return 0;
}
8. (Program) a. Create a text file named car.dat containing the following data (without the
headings):
Car Number Miles Driven Gallons of Gas Used
54 250 19
62 525 38
71 123 6
85 1322 86
97 235 14
#include <iostream>
#include <fstream>
#include <cstdlib> // needed for exit()
#include <string> // needed for the string class
#include <iomanip> // needed for formatting
using namespace std;
int main()
{
string filename = "car.dat"; // put the filename up front
ofstream outFile;
outFile.open(filename.c_str());
if (outFile.fail())
{
cout << "The file was not successfully opened" << endl;
exit(1);
}
// send data to the file
outFile << 55 << setw(10) << 250 << setw(10) << 19 << endl
<< 62 << setw(10) << 525 << setw(10) << 38 << endl
<< 71 << setw(10) << 123 << setw(10) << 6 << endl
<< 85 << setw(10) << 97 << setw(10) << 86 << endl
<< 97 << setw(10) << 235 << setw(10) << 14 << endl;
outFile.close();
cout << "The file " << filename
<< " has been successfully written." << endl;
system("PAUSE");
return 0;
}
b. Write a C++ program that reads the data in the file created in Exercise 8a and displays the
car number, miles driven, gallons of gas used, and miles per gallon (mpg) for each car. The
output should contain the total miles driven, total gallons of gas used, and average mpg for
all cars. These totals should be displayed at the end of the output report.
#include <iostream>
#include <fstream>
#include <cstdlib> // needed for exit()
#include <string> // needed for the string class
#include <iomanip> // needed for formatting
using namespace std;
int main()
{
string filename = "car.dat"; // put the filename up front
ofstream outFile;
outFile.open(filename.c_str());
if (outFile.fail())
{
cout << "The file was not successfully opened" << endl;
exit(1);
}
// send data to the file
double mpg1 = (250 / 19);
double mpg2 = (525 / 38);
double mpg3 = (123 / 6);
double mpg4 = 97 / 86;
double mpg5 = 235 / 14;
double totalmile = 250 + 525 + 123 + 97 + 235;
double totalgal = 19 + 38 + 6 + 86 + 14;
double total = mpg1 + mpg2 + mpg3 + mpg4 + mpg5;
double average = (total / 5);
outFile << 55 << setw(10) << 250 << setw(10) << 19 <<setw(10) << mpg1 << endl
<< 62 << setw(10) << 525 << setw(10) << 38 <<setw(10) << mpg2 << endl
<< 71 << setw(10) << 123 << setw(10) << 6 <<setw(10)<< mpg3 << endl
<< 85 << setw(10) << 97 << setw(10) << 86 <<setw(10)<< mpg4 << endl
<< 97 << setw(10) << 235 << setw(10) << 14 << setw(10)<<mpg5 << endl
<<" Total Miles Driven" << setw(10) << totalmile << endl
<< " Totla Gallons of Gas Used" <<setw(10) << totalgal << endl
<< " Average mpg for all cars" << setw(10) <<average << endl;
outFile.close();
cout << "The file " << filename
<< " has been successfully written." << endl;
system("PAUSE");
return 0;
}
9. (Program) a. Create a text file named parts.dat with the following data (without the
headings):
Part Number Initial Amount Quantity Sold Minimum Amount
QA310 95 47 50
CM145 320 162 200
MS514 34 20 25
EN212 163 150 160
#include <iostream>
#include <fstream>
#include <cstdlib> // needed for exit()
#include <string> // needed for the string class
#include <iomanip> // needed for formatting
using namespace std;
int main()
{
string filename = "parts.dat"; // put the filename up front
ofstream outFile;
outFile.open(filename.c_str());
if (outFile.fail())
{
cout << "The file was not successfully opened" << endl;
exit(1);
}
// send data to the file
outFile << "QA310" << setw(10) << 95 << setw(10) << 45 << setw(10) << 50 << endl
<< "CM145"<< setw(10) << 320 << setw(10) << 162 << setw(10) << 200 << endl
<< "MS514" << setw(10) << 34 << setw(10) << 20 << setw(10) << 25 << endl
<< "EN212" << setw(10) << 163 << setw(10) << 150 << setw(10) << 160 << endl;
outFile.close();
cout << "The file " << filename
<< " has been successfully written." << endl;
system("PAUSE");
return 0;
}
b. Write a C++ program to create an inventory report based on the data in the file created in
Exercise 9a. The display should consist of the part number, current balance, and the amount
needed to bring the inventory to the minimum level. The current balance is the initial
amount minus the quantity sold.
#include <iostream>
#include <fstream>
#include <cstdlib> // needed for exit()
#include <string> // needed for the string class
#include <iomanip> // needed for formatting
using namespace std;
int main()
{
string filename = "parts.dat"; // put the filename up front
ofstream outFile;
outFile.open(filename.c_str());
if (outFile.fail())
{
cout << "The file was not successfully opened" << endl;
exit(1);
}
// send data to the file
double currentbal1 = 95 - 45;
double currentbal2 = 320 - 162;
double currentbal3 = 34 - 20;
double currentbal4 = 163 - 150;
outFile << "QA310" << setw(10) << currentbal1<< setw(10) << 50 << endl
<< "CM145"<< setw(10) << currentbal2<< setw(10) << 200 << endl
<< "MS514" << setw(10) <<currentbal3<< setw(10) << 25 << endl
<< "EN212" << setw(10) << currentbal4 << setw(10) << 160 << endl;
outFile.close();
cout << "The file " << filename
<< " has been successfully written." << endl;
system("PAUSE");
return 0;
}
10. (Program) a. Create a text file named pay.dat containing the following data (without the
headings):
Name Rate Hours
Callaway, G. 16.00 40
Hanson, P. 15.00 48
Lasard, D. 16.50 35
Stillman, W. 12.00 50
#include <iostream>
#include <fstream>
#include <cstdlib> // needed for exit()
#include <string> // needed for the string class
#include <iomanip> // needed for formatting
using namespace std;
int main()
{
string filename = "pay.dat"; // put the filename up front
ofstream outFile;
outFile.open(filename.c_str());
if (outFile.fail())
{
cout << "The file was not successfully opened" << endl;
exit(1);
}
// send data to the file
outFile << "Callaway, G." << setw(10) << 16.00 << setw(10) << 40 << endl
<< "Hanson, P."<< setw(10) << 15.00 << setw(10) << 48 << endl
<< "Lasard, D." << setw(10) << 16.50<< setw(10) << 35 << endl
<< "Stillman, W." << setw(10) << 12.50 << setw(10) << 50 << endl;
outFile.close();
cout << "The file " << filename
<< " has been successfully written." << endl;
system("PAUSE");
return 0;
}
b. Write a C++ program that uses the information in the file created in Exercise 10a to produce
the following pay report for each employee:
Name Pay Rate Hours Regular Pay Overtime Pay Gross Pay
Compute regular pay as any hours worked up to and including 40 hours multiplied by the pay
rate. Compute overtime pay as any hours worked above 40 hours at a pay rate of 1.5 multiplied
by the regular rate. The gross pay is the sum of regular and overtime pay. At the end of the
report, the program should display the totals of the regular, overtime, and gross pay columns.
#include <iostream>
#include <fstream>
#include <cstdlib> // needed for exit()
#include <string> // needed for the string class
#include <iomanip> // needed for formatting
using namespace std;
int main()
{
string filename = "pay.dat"; // put the filename up front
ofstream outFile;
outFile.open(filename.c_str());
if (outFile.fail())
{
cout << "The file was not successfully opened" << endl;
exit(1);
}
// send data to the file
double regularpay = 16 * 40;
double regularpay2 = 15 * 40;
double regularpay3 = 16.5 * 35;
double regularpay4 = 12.50 * 40;
double OvertimePay = 0;
double OvertimePay2 = (15 * 8)*1.5;
double OvertimePay3 = 0;
double OvertimePay4 = (10 * 12.50)*1.5;
double GrossPay = regularpay + OvertimePay;
double GrossPay2 = regularpay2 + OvertimePay2;
double GrossPay3 = regularpay3 + OvertimePay3;
double GrossPay4 = regularpay4 + OvertimePay4;
outFile << "Callaway, G." << setw(10) << 16.00 << setw(10) << 40 <<setw(10) << regularpay << setw(10) << OvertimePay <<setw(10) << GrossPay<< endl
<< "Hanson, P." << setw(10) << 15.00 << setw(10) << 48 << setw(10) << regularpay2 << setw(10) << OvertimePay2 << setw(10) << GrossPay2 << endl
<< "Lasard, D." << setw(10) << 16.50 << setw(10) << 35 << setw(10) << regularpay3 << setw(10) << OvertimePay3 << setw(10) << GrossPay3 << endl
<< "Stillman, W." << setw(10) << 12.50 << setw(10) << 50 << setw(10) << regularpay4 << setw(10) << OvertimePay4 << setw(10) << GrossPay4 << endl;
outFile.close();
cout << "The file " << filename
<< " has been successfully written." << endl;
system("PAUSE");
return 0;
}
11. (Program) a. Store the following data in a file named numbers.dat:
5 96 87 78 93 21 4 92 82 85 87 6 72 69 85 75 81 73
#include <iostream>
#include <fstream>
#include <cstdlib> // needed for exit()
#include <string> // needed for the string class
#include <iomanip> // needed for formatting
using namespace std;
int main()
{
string filename = "numbers.dat"; // put the filename up front
ofstream outFile;
outFile.open(filename.c_str());
if (outFile.fail())
{
cout << "The file was not successfully opened" << endl;
exit(1);
}
// send data to the file
outFile << 5 << setw(10) << 96 << setw(10) << 87 << setw(10) << 78 << setw(10)
<< 93 << setw(10) << 21 << setw(10) << 4 << setw(10) << 92 << setw(10)
<< 82 << setw(10) << 85 << setw(10) << 87 << setw(10) << 6 << setw(10)
<< 72 << setw(10) << 69 << setw(10) << 85 << setw(10) << 75 << setw(10)
<< 81 << setw(10) << 73;
outFile.close();
cout << "The file " << filename
<< " has been successfully written." << endl;
system("PAUSE");
return 0;
}
b. Write a C++ program to calculate and display the average of each group of numbers in the
file created in Exercise 11a. The data is arranged in the file so that each group of numbers
is preceded by the number of data items in the group. Therefore, the first number in the
file, 5, indicates that the next five numbers should be grouped together. The number 4
indicates that the following four numbers are a group, and the 6 indicates that the last six
numbers are a group. (Hint: Use a nested loop. The outer loop should terminate when the
end of file has been encountered.)
#include <iostream>
#include <fstream>
#include <cstdlib> // needed for exit()
#include <string> // needed for the string class
#include <iomanip> // needed for formatting
using namespace std;
int main()
{
string filename = "numbers.dat"; // put the filename up front
ofstream outFile;
ifstream inFile;
inFile.open(filename.c_str());
outFile.open(filename.c_str());
if (outFile.fail())
{
cout << "The file was not successfully opened" << endl;
exit(1);
}
// send data to the file
//outFile << 5 << 96 << 87 << 78 << 93 << 21 << 4 << 92 << 82
// << 85 << 87 << 6 << 72 << 69 << 85 << 75 << 81 << 73;
while (inFile.good())
{
double InPut[] = { 5, 96, 87, 78, 93, 21, 4, 92, 82, 85, 87, 6, 72, 69, 85, 75, 81, 73 };
double total = 0;
inFile >> InPut[0];
cout << "The number of elements in this group is " << InPut[0] << endl;
cout << "The data in this group is: ";
for (int i = 0; i <= 5; i++)
{
inFile >> InPut[i];
total = total + InPut[i];
cout << InPut[i] << " ";
outFile << InPut[i] << " ";
}
cout << endl << "Average = " << (total - InPut[0]) / InPut[0] << endl; // Sum divided by InPut
outFile << endl << "Average = " << (total - InPut[0]) / InPut[0] << endl;
total = 0; // Reset Total
inFile >> InPut[6];
cout << "The number of elements in this group is " << InPut[6] << endl;
cout << "The data in this group is: ";
for (int j = 6; j <= 10; j++)
{
inFile >> InPut[j];
total = total + InPut[j];
cout << InPut[j] << " ";
outFile << InPut[j] << " ";
}
cout << endl << "Average = " << (total - InPut[6]) / InPut[6] << endl; // Sum divided by InPut
outFile << endl << "Average = " << (total - InPut[6]) / InPut[6] << endl;
total = 0; // Reset Total
inFile >> InPut[11];
cout << "The number of elements in this group is " << InPut[11] << endl;
cout << "The data in this group is: ";
for (int k = 11; k < 18; k++)
{
inFile >> InPut[k];
total = total + InPut[k];
cout << InPut[k] << " ";
outFile << InPut[k] << " ";
}
cout << endl << "Average = " << (total - InPut[11]) / InPut[11] << endl; // Sum divided by InPut
outFile << endl << "Average = " << (total - InPut[11]) / InPut[11] << endl;
total = 0; // Reset Total
}
outFile.close();
cout << "The file " << filename
<< " has been successfully written." << endl;
system("PAUSE");
return 0;
}
12. (Program) Write a C++ program that allows users to enter the following information from the
keyboard for each student in a class (up to 20 students) and stores the data in a text file named
grade.dat:
Name Exam 1 Grade Exam 2 Grade Homework Grade Final Exam Grade
For each student, your program should first calculate a final grade, using this formula:
Final Grade = 0.20 × Exam 1 + 0.20 × Exam 2 + 0.35 × Homework + 0.25 × Final Exam
Then assign a letter grade on the basis of 90–100 = A, 80–89 = B, 70–79 = C, 60–69 = D, and
less than 60 = F. All the information, including the final grade and the letter grade, should then
be displayed and written to a file.
#include <iostream>
#include <fstream>
#include <cstdlib> // needed for exit()
#include <string> // needed for the string class
#include <iomanip> // needed for formatting
using namespace std;
int main()
{
string filename = "grade.dat"; // put the filename up front
ofstream outFile;
ifstream inFile;
inFile.open(filename.c_str());
outFile.open(filename.c_str());
if (outFile.fail())
{
cout << "The file was not successfully opened" << endl;
exit(1);
}
// send data to the file
double Exam1;
double Exam2;
double Homework;
double FinalExam;
string name;
for (int i = 1; i <= 20; i++){
cout << "Enter student's name" << endl;
cin >> name;
cout << "Enter Exam 1 Grade for student " << endl;
cin >> Exam1;
cout << "Enter Exam 2 Grade " << endl;
cin >> Exam2;
cout << "Enter Homework Grade " << endl;
cin >> Homework;
cout << "Enter Final Exam Grade " << endl;
cin >> FinalExam;
outFile << "Name " << setw(20) << "Exam 1 Grade" << setw(20) << "Exam 2 Grade" << setw(20) << "Homework Grade" << setw(20) << "Final Exam Grade" << endl;
outFile << name << setw(20) << Exam1 << setw(20) << Exam2 << setw(20) << Homework << setw(20) << FinalExam << endl;
double finalgrad = .20 *Exam1 + 0.20 * Exam2 + 0.35 * Homework + 0.25 * FinalExam;
outFile << "Your Final Grade is" << endl;
outFile << finalgrad << endl;
if (finalgrad >= 90){
outFile << "A" << endl;
}
else if (finalgrad >= 80 && finalgrad <= 89){
outFile << "B" << endl;
}
else if (finalgrad >= 70 && finalgrad <= 79){
outFile << "C" << endl;
}
else if (finalgrad >= 60 && finalgrad <= 69){
outFile << "D" << endl;
}
else if (finalgrad < 60){
outFile << "F" << endl;
}