Problem with looping (for)

Hey, another question. In this program I'm asking the user to input a word at least 5 characters, then a single character to show how many times it repeats in the world. All though there are no errors while compiling my answer is something drastic. For example, if I entered the word 'hello' then I enter 'l' to see how many times it comes the answer is always between 40-50.

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
#include <iostream>
#include <string>

using namespace std;



int main()
{
     system("Color A");
    string word = "";
    
do 
{
  cout << " Enter a word that has at least five characters: \n" << endl;
  cin >> word;
} while(word.size() < 5) ;


char searCh = '0';
int counter = '0';

cout << "Enter a character and we'll tell you how many time your character will repeat itself, in the word " << word << "\n" << endl;
cin >> searCh;

for (int i = 0; i < (int)word.size(); i++)
{
 char ch = word.at(i);   
 
 if (searCh == ch)
 {
            counter++;
            }
}

cout << "The Number of " << searCh << "'s in the word, " << word << " is " << 50 - counter << "." << endl;



system("Color D");
  
 
 system("PAUSE");
}
'0', with which you initialized counter, is a value distinct from 0 . '0' is usually equal to 48.
Last edited on
Thanks Helios :)
Topic archived. No new replies allowed.