forked from MaveyMa/fileIO_A
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlomeli_FileIO_A.cpp
More file actions
161 lines (140 loc) · 4.16 KB
/
Copy pathlomeli_FileIO_A.cpp
File metadata and controls
161 lines (140 loc) · 4.16 KB
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
//File I/O practice
#include <iostream>
#include <fstream> // read and write files
#include <cstdlib>
using namespace std;
void countWords(string strA, int& words, int& chars);
string upperAll(string strA);
void countChar(string strA, int& sum);
int main()
{
ifstream fin;
ofstream fout;
ofstream foutB;
ofstream foutC;
string words;
string newString;
string space = " ";
fin.open("gba.txt");//opens document gba.txt
fout.open("results.txt");//creates document
foutB.open("capitalize.txt");
foutC.open("uppercase.txt");
int chars = 0;
int wordCount = 0;
int length = 0;
int length1(0), length2(0), length3(0),length4(0),length5(0), length6(0), length7(0), length8(0),length9(0),length10(0), length11(0);
string newPara;
string capitals;
if(fin.fail())//if it fails to import data from document
{
cout << "error opening import file" << endl;
exit(1);
}//if
if(fout.fail())//if it fails to output to new document
{
cout << "error opening output file" << endl;
exit(1);
}//if
while (fin >> words)
{
length = words.length();
switch (length)//counting the number of characters in a word and keeping count of each word
{
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:
length11++;
break;
}//switch
//countChar(words, length);
words[0] = toupper(words[0]);// capitalize char at position [[0]]
newString += words + space;
newPara = newString;
capitals = upperAll(newPara);
}// while
cout << length2;
foutB << capitals;
foutC << newString;
countWords(newPara, wordCount, chars);
fout << "average characters per word: " << (chars / wordCount) << endl;
fout << length1 << " words of length 1" << endl;
fout << length2 << " words of length 2" << endl;
fout << length3 << " words of length 3" << endl;
fout << length4 << " words of length 4" << endl;
fout << length5 << " words of length 5" << endl;
fout << length6 << " words of length 6" << endl;
fout << length7 << " words of length 7" << endl;
fout << length8 << " words of length 8" << endl;
fout << length9 << " words of length 9" << endl;
fout << length10 << " words of length 10" << endl;
fout << length11 << " words of length 11 or longer" << endl;
fout << "Total number of words: " << wordCount << endl;
fin.close();
fout.close();
foutB.close();
foutC.close();
return 0;
}
void countWords(string strA, int& words, int& chars)//count the number of words
{
words = 0;
for (int ix(0); ix < strA.length(); ix++)
{
if ((strA[ix] == ' ') && (strA[ix +1] >= '0')&& (strA[ix +1] <= '}'))//accounts for the next char after the space not being an alphabet character
{
words ++;
}//if
if ((strA[ix] >= 'A') || (strA[ix] >= 'z'))
{
chars++;
}
}//for
return;
}//countWords
string upperAll(string strA)
{
string upper;
for(int ix = 0; ix < strA.length(); ix++)
{
string letter;
letter = toupper(strA[ix]);
upper += letter;
}
return upper;
}//upperAll
void countChar(string strA, int& length)
{
for(int ix(0); ix < strA.length(); ix++)
{
length++;
}
return;
}