Reputation: 11
I am working on exercises for school and I'm having a little problem: I don't know how to mark "from - to", like all uppercase letters in ASCII (65-90). I can't put in -
, because it would mean minus. Here is my program so far:
#include <iostream.h>
int main()
{
char letter;
cout<<"Put in letter: ";
cin>>letter;
if (letter == 65)
cout<<"Letter is uppercase";
return 0;
}
Upvotes: 1
Views: 115
Reputation: 211135
(letter >= 65 && letter <= 90)
means gerater or equal 65 and less or equal 90.
#include <iostream>
using namespace std;
int main()
{
char letter;
cout<<"Put in letter: ";
cin>>letter;
if (letter >= 'A' && letter <= 'Z')
cout<<"Letter is upercase";
}
Upvotes: 2
Reputation: 6046
You should definitely use isupper()
because
(letter >= 65 && letter <= 90)
will not work there.This is, for example, the case of EBCDIC code pages used on z/OS mainframes (https://en.wikipedia.org/wiki/EBCDIC#Compatibility_with_ASCII). isupper()
handles those seamlessly.
(I was working on portable code supporting all those systems as well and you wouldn't believe, how many programmers and library writers make the assumption that the basic letters and numbers are always on the same position and in contiguous blocks.)
Upvotes: 6
Reputation: 1670
c and c++ don't have a "from - to" statement or something like that. but you could easily write your own function.
#include <iostream>
using namespace std;
bool between(char,char,char);
int main()
{
char letter;
cout<<"Put in letter: ";
cin>>letter;
if (between(letter, 'A', 'Z')) {
cout<<"Letter is upercase";
}
}
bool between(char n, char low, char high) {
return n >= low && n <= high;
}
Upvotes: 3