Counting Characters

i put the comments:
-first one
-second one


inside my code to point to my questions.
at first and second one I try to compare the char (not int) 1 and the char 0 to the char on a certain place inside the string (sentence).

in this code i'm not using the char 1 yet (because i want to change already count chars to 1 to prevent counting a certain letter twice, like the letter "l" in my console

however the comparing doesn't work at all, and it simply does whatever is inside those if statements.

another problem I have is that I get an error inside the console:
http://i41.tinypic.com/iwt15j.png (can't seem to use pics here, so a link instead)

note: this obviously isn't the optimal way to count characters inside a string, however this is the first "project" I am trying to work on. and haven't had any outside help yet.



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
#include <iostream>
#include <string>
using namespace std;

int charSelecter(char sorter);

int main()
{
    char testing;
    char c = '0';
    char d = '1';
    int appear = 0;
    int a = 0;
    string words;
    string sentence;
    cout << "type in words until you are done, after that press 0" << endl;
    while (words != "0")
    {
        cin >> words;
        sentence.insert(sentence.length(), words);
        sentence.insert(sentence.length(), " ");


    }

   while (a < sentence.length())
   {
        for (int x = 0; x < sentence.length(); x++)
        {
            if (sentence.at(a))
            {
                testing = sentence.at(a);
            }
            if (sentence.at(x) != '1') // first one one
            {
                if (sentence.at(x) != '0') // second one
                {
                    if (testing == sentence.at(x))
                    {
                        appear++;


                    }
                }else{
                    cout << "letter" << testing << " appeared " <<  appear << " times " << endl;
                    a++;
                    appear = 0;

                }
            }
        }
    }
}

Last edited on
To prevent from inserting the string "0" into your string, make the iteration like so:

1
2
3
4
5
cout << "type in words until you are done, after that press 0" << endl;
for (cin >> words; words != "0"; cin >> words)
{
    sentence.insert(sentence.length(), words + " ");
}


Also recommend using vector rather than increasing the size of your string each time the user enters a word

As for the rest of the number counting process, someone here made a recent post that solved this problem:

http://www.cplusplus.com/forum/beginner/121534/#msg662079
Last edited on
i see, i will look into using vectors then.

and i just now noticed that I forgot about the fact that you can count using chars, not that i'd ever come up with what was posted in the topic you linked though.

anyway thanks for your help.
Topic archived. No new replies allowed.