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.
#include <iostream>
#include <string>
usingnamespace 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;
}
}
}
}
}
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:
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.