-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathMa_Prog_Practice_FileIO_A.cpp~
144 lines (136 loc) · 4.07 KB
/
Ma_Prog_Practice_FileIO_A.cpp~
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
// Name: Mavey Ma
// Due: Thursday, October 22, 2015
// FILE I/O PROGRAM PRACTICE A, GITHUB
#include <iostream>
#include <fstream> //ALLOWS YOU TO READ AND WRITE TO FILES
#include <cstdlib>
#include <cctype> //toupper()
using namespace std;
int main()
{
ifstream fin; //gba.txt
ofstream fout; //result.txt
ofstream foutCap; //capitalize.txt
ofstream foutUp; //uppercase.txt
string word;
double average, sum = 0, count = 0;
int length1 = 0, length2 = 0, length3 = 0, length4 = 0;
int length5 = 0, length6 = 0, length7 = 0, length8 = 0;
int length9 = 0, length10 = 0, length11orMore = 0;
//OPEN SESAME, FILES!
fin.open("gba.txt");
fout.open("result.txt");
foutCap.open("capitalize.txt");
foutUp.open("uppercase.txt");
//IN CASE FILE DOESN'T OPEN PROPERLY
if (fin.fail())
{
cout << "ERROR OPENING INPUT FILE.\n";
exit(1);
}
if (fout.fail())
{
cout << "ERROR OPENING INPUT FILE.\n";
exit(1);
}
if (foutCap.fail())
{
cout << "ERROR OPENING INPUT FILE.\n";
exit(1);
}
if (foutUp.fail())
{
cout << "ERROR OPENING INPUT FILE.\n";
exit(1);
}
//LET'S READ IN THE gba.txt FILE WORD BY WORD
while (fin >> word)
{
string copyWord = word;
//1 DETERMINES THE AVERAGE NUMBER OF CHARACTERS PER WORD
count++; //3 TOTAL NUMBER OF WORDS
sum += word.length(); //TOTAL NUMBER OF CHARACTERS
//2 PROVIDE A COUNT OF THE NUMBER OF WORDS PER EACH LENGTH
switch (word.length())
{
case 1:
length1++;
break;
case 2:
length2++;
break;
case 3:
length3++;
break;
case 4:
length4++;
break;
case 5:
length5++;
break;
case 6:
length6++;
break;
case 7:
length7++;
break;
case 8:
length8++;
break;
case 9:
length9++;
break;
case 10:
length10++;
break;
default:
length11orMore++;
break;
}//END SWITCH
//4 CAPITALIZE ALL LETTERS IN gba.txt
for (int ix = 0; ix < word.length(); ix++)
{
if (word[ix] >= 'a' && word[ix] <= 'z')
{
word[ix] -= 32;
foutCap << word[ix];
}
else
{
foutCap << word[ix];
}
}//END FOR LOOP
//4 INCLUDE SPACES BETWEEN EACH CAPITALIZED WORD
//WRITE TO capitalize.txt
foutCap << " ";
//5 UPPERCASE FIRST LETTER OF EACH WORD IN gba.txt
char firstLetter = toupper(copyWord[0]);
foutUp << firstLetter << copyWord.substr(1);
//5 INCLUDE SPACES BETWEEN EACH CAPITALIZED WORD
//WRITE TO uppercase.txt
foutUp << " ";
}//END WHILE
//1 PRINTS RESULT TO result.txt
average = sum / count;
fout << "average characters per word: " << average << endl;
//2 PRINTS RESULT TO result.txt
fout << length1 << " words of length 1\n";
fout << length2 << " words of length 2\n";
fout << length3 << " words of length 3\n";
fout << length4 << " words of length 4\n";
fout << length5 << " words of length 5\n";
fout << length6 << " words of length 6\n";
fout << length7 << " words of length 7\n";
fout << length8 << " words of length 8\n";
fout << length9 << " words of length 9\n";
fout << length10 << " words of length 10\n";
fout << length11orMore << " words of length 11 or longer\n";
//3 TOTAL NUMBER OF WORDS IN gba.txt. PRINTS RESULT TO result.txt
fout << "Total number of words: " << count << endl;
//CLOSE FILES
fin.close();
fout.close();
foutCap.close();
foutUp.close();
return 0;
}//END INT MAIN