Char to ASCII value program

Why does not the following program work? What am I doing wrong here. Can someone please help me out?
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
// you can use includes, for example:
#include <algorithm>
#include <iostream>
#include <vector>
#include <iterator>
#include <ctype.h>

using namespace std;

// you can write to stdout for debugging purposes, e.g.
// printf("this is a debug message\n");
// you can use includes, for example:
// #include <algorithm>

// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;

int solution(string &S) {
   int sol = -1;
   bool flag = false;
   int count = 0;
   int maxcount = -1;
   for (int i = 0; i< S.length(); i++){
      int letter = S[i];
      cout << "letter is " << letter << endl;
      cout << "count is" << count << endl;
      //if (isdigit(letter)){
      if (48 <=letter <= 57){
          count = 0;
      }
      //else if (isupper(letter)){
      else if (65 <= letter <=90){
          flag = true;
          count++;
       }
       //else if (islower(letter)){
       else if (97 <= letter <= 122){
          count++;
       }
       if (count>maxcount){
            maxcount = count;
       }
   }
   if (flag) sol = maxcount;
   return sol;
}

int main(){
   string temp = "a0Ab";
   int i = solution(temp);
   cout << "The answer is: " << i << endl;
   cin.clear();
   getchar();
   return 0;
}


When I remove the commented lines of code it works. I want the char to integer conversion part to work too.
Last edited on
if (48 <=letter <= 57)
This means something different in English than it does in code. The same is true for all the other times you use that format.
What is int solution(string &S) supposed to do?
Last edited on
I think I figured it out. I should have done if(48<=letter && letter<=57). It is same thing as not having (A == B==C) check in C++. Thanks @admkrk.

@CodeWriter: int solution is a subroutine to return if the input string has a upper case letter or not. It was part of coding challenge I failed to complete online.
Last edited on
Topic archived. No new replies allowed.