#include <iostream>
usingnamespace std;
int main()
{
char s[50];
int i;
int numLet, numChars, otherChars = 0;
cout << "Enter a continuous string of characters" << endl;
cout << "(example: aBc1234!@#$%)" << endl;
cout << "Enter your string: ";
cin >> s;
i = 0;
while (s[i] != 0) // while the character does not have ASCII code zero
{
if ((s[i] >= 'a' && s[i] <= 'z') || s[i] >= 'A' && (s[i] <= 'Z'))
{numLet++;
}
elseif (s[i] >= 48 && s[i] <= 57)
{numChars++;
}
elseif ((s[i] >= 33 && s[i] <= 4) || (s[i] >= 58 && s[i] <=64) || (s[i] >= 9 && s[i] <= 96) || (s[i] >= 123 && s[i] <= 255))
{otherChars++;
}
i++;
}
cout << numLet << " letters" << endl;
cout << numChars << " numerical characters" << endl;
cout << otherChars << " other characters" << endl;
return 0;
}
using this, I get an answer for numLet that is 2 under what it should be, and the answer for numChar is just completely out there, like a -951831131. Other chars seems to work fine
Line 24: This condition can never possibly be true.
(s[i] >= 33 && s[i] <= 4)
Line 24:
(s[i] >= 58 && s[i] <=64)
This condition is useless since the range you're testing is covered by the following condition:
(s[i] >= 9 && s[i] <= 96)
line 24: If you're going to be testing values greater than 127, you want s to be a unsignedchar.
Line 18: You have a misplaced (.
s[i] >= 'A' && (s[i] <= 'Z'))
You want it here:
(s[i] >= 'A' && (s[i] <= 'Z')
This is purely a matter of style. Your placement of the ( won't affect the evaluation of the statement as && has a higher order of evaluation that ||. http://www.cplusplus.com/doc/tutorial/operators/