Creating a loop that checks for characters in a doc

I'm suppose to write a code that checks all the data in a text file but every time i check each data file the result ends up equaling 283 and I don't know why

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
#include <iostream>
#include <cmath>
#include <fstream>
#include <iomanip>
#include <cstdlib>
#include <string>

using namespace std;

int main()
{
	int ib = 0, is = 0, a = 0, b = 0, c = 0, d = 0, e = 0, f = 0, g = 0, h = 0, i = 0, j = 0, k = 0, l = 0, m = 0, n = 0, o = 0, p = 0, q = 0, r = 0, s = 0, t = 0, u = 0, v = 0, w = 0, x = 0, y = 0, z = 0;
	char filename[50];
	ifstream inFile;
	cout << "Please enter in the file name (50 char max) : ";
	cin.getline(filename, 50);
	inFile.open(filename);

	if (!inFile.is_open()){
		exit(EXIT_FAILURE);

	}

	string word;
	inFile >> word;

	if (inFile.good()){
		inFile >> word;

		for (is = 0; is <= word.length(); is++){

			if (word[is] = 'a'){
				a = a + 1;
				cout << "hi" << is << endl;
			}

			if (word[is] = 'b'){
				b = b + 1;
			}

			if (word[is] = 'c'){
				c = c + 1;
			}

			if (word[is] = 'd'){
				d = d + 1;
			}

			if (word[is] = 'e'){
				e = e + 1;
			}

			if (word[is] = 'f'){
				f = f + 1;
			}

			if (word[is] = 'g'){
				g = g + 1;
			}

			if (word[is] = 'h'){
				h = h + 1;
			}

			if (word[is] = 'i'){
				i = i + 1;
			}

			if (word[is] = 'j'){
				j = j + 1;
			}

			if (word[is] = 'k'){
				k = k + 1;
			}

			if (word[is] = 'l'){
				l = l + 1;
			}

			if (word[is] = 'm'){
				m = m + 1;
			}

			if (word[is] = 'n'){
				n = n + 1;
			}

			if (word[is] = 'o'){
				o = o + 1;
			}

			if (word[is] = 'p'){
				p = p + 1;
			}

			if (word[is] = 'q'){
				q = q + 1;
			}

			if (word[is] = 'r'){
				r = r + 1;
			}

			if (word[is] = 's'){
				s = s + 1;
			}

			if (word[is] = 't'){
				t = t + 1;
			}

			if (word[is] = 'u'){
				u = u + 1;
			}

			if (word[is] = 'v'){
				v = v + 1;
			}

			if (word[is] = 'w'){
				w = w + 1;
			}

			if (word[is] = 'x'){
				x = x + 1;
			}

			if (word[is] = 'y'){
				y = y + 1;
			}

			if (word[is] = 'z'){
				z = z + 1;
			}


		}



	}

	cout << "a = " << a << "  -  " << b << endl;


	return 0;

}
(word[is] = 'a') This is not comparsion. This is assigment.
now that i've changed it it doesn't run though the compare anymore o.o
How did you cahnge it? What do your file contains?

is <= word.length(); It is off-by-one error. Can lead to crashes or unexpected behavior. Change to is < word.length();
This would be easier if you used an array instead:
Create an array with 26 integers.
Initialize the items to zero.
Store the number of A's in array[0], the number of B's in array[1], etc.
Before incrementing one of the numbers you need to ensure that the character read is a letter, and then convert the letter (A-Z) into a number (0-25).

Topic archived. No new replies allowed.