While loop with if else statements

I am having trouble counting the proper number of letters, numbers and other characters in an array. My code is as follows.

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
  #include <iostream>
using namespace 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++;
  }
else if (s[i] >= '0' && s[i] <= '9')
    {numChars++;
    }
else if ((s[i] >= '33' && s[i] <= '47') || (s[i] >= '58' && s[i] <='64') || (s[i] >= '91' && 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;
}
Line 24: Your decimal values should not be quoted.

 
else if ((s[i] >= 33 && s[i] <= 47) || (s[i] >= 58 && s[i] <=64) || (s[i] >= 91 && s[i] <= 96) || (s[i] >= 123 && s[i] <= 255))

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
#include <iostream>
using namespace 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++;
  }
else if (s[i] >= 48 && s[i] <= 57)
    {numChars++;
    }
else if ((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
Probably because you didn't initialise it, so it had leftover junk when the memory was last used. This means that
1
2
3
4
else if (s[i] >= 48 && s[i] <= 57)
{
     numChars++;
}

is never true.
Last edited on
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 unsigned char.

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/



Last edited on
Topic archived. No new replies allowed.