Program dosnt end
May 27, 2014 at 7:31pm UTC
I have no idea to why the following code never ends
P.S: Ascender are letters that pass the second line in a writing copy, like 'f', 'k'. and descenders are the opposite, they cross the bottom line, ex 'g', 'y'.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
int main()
{
const string ascenders = "tdfhklb" , decenders = "qypgj" ;
ifstream file("input.txt" );
string input, mostAscDec;
while (file >> input) {
unsigned count = 0, lrgstCount;
string::size_type pos = 0;
while (input.find_first_not_of(ascenders, pos) != string::npos && input.find_first_not_of(decenders, pos) != string::npos) {
++count;
if (count >= lrgstCount) {
lrgstCount = count;
mostAscDec = input;
}
}
}
cout << "Word with the least ascenders and decenders: " << mostAscDec << endl;
return 0;
}
May 27, 2014 at 7:39pm UTC
Line 11. If finds something, then you are doing something and then it searches same string for same letters. No wonders it finds something again.
May 27, 2014 at 8:00pm UTC
Waw what a stupid mistake. There were a few other mistakes to so fixed em up. Here is the updated code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
int main()
{
const string ascenders = "tdfhklbTDFHKLB" , decenders = "qypgjQYPGJ" ;
ifstream file("input.txt" );
string input, mostAscDec;
unsigned lrgstCount = 0;
while (file >> input) {
unsigned count = 0;
string::size_type pos = 0;
while (input.find_first_of("weruioaszxcvnm" , pos) != string::npos) {
++count;
if (count >= lrgstCount) {
lrgstCount = count;
mostAscDec = input;
}
++pos;
}
}
cout << "Word with the least ascenders and decenders(" << lrgstCount << "): " << mostAscDec << endl;
return 0;
}
Topic archived. No new replies allowed.