0

I'm a beginner to c++ and I'm stuck at the very beginning.

The problem is simple: a letter is the input and the output should be "it's uppercase" or "it's lowercase", or simply "error" when it's not a letter.

And that's where I got stuck.

#include <iostream>
#include <cstdlib>
using namespace std;


int main(){
char t;
cin>>t;
if (t==toupper(t))cout<<"UPPER";
else if(t==tolower(t))cout<<"LOWER";
else cout<<"ERROR";
return 0;

}

That's my code. I have never worked with chars before. It seems that the program cannot know whether it's text or a number/special symbol. How do I get it to tell me whether the letter is upper, lower or an error?

T jey
  • 23
  • 1
  • 4
  • 4
    Are you dealing with just ASCII? See [::isupper()](http://en.cppreference.com/w/cpp/string/byte/isupper) and [::islower()](http://en.cppreference.com/w/cpp/string/byte/islower). – Fred Larson Jan 08 '18 at 16:22
  • Please show sample input, what you expect, and what you get. – TripeHound Jan 08 '18 at 16:23
  • `if (t==toupper(t))` -- The book, reference, or tutorial you're using -- does it show that this is the way to use `toupper`, or even if `toupper` is the correct function to determine if a letter is upper-case? – PaulMcKenzie Jan 08 '18 at 16:24
  • *The problem is simple: a letter is the input and the output should be "it's uppercase"* - that's only simple in English, not (usually) in other languages, and it's especially not simple when you have to deal with multiple languages all at once. – Christian Hackl Jan 08 '18 at 16:24
  • 1
    @FredLarson -- `isupper()` and `islower` work for whatever character encoding the system uses, provided that character values fit in a `char`; ASCII is one of those, but not the only one. These functions do **not** depend on the encoding being ASCII. – Pete Becker Jan 08 '18 at 16:37
  • Only ASCII. Sample goes like this: I insert 3 it tells me uppercase. It should say error. The language is not an issue here. – T jey Jan 08 '18 at 16:37
  • @TeodorJovanovski -- forget ASCII. `toupper`, `tolower`, `isupper`, and `islower` work for the character set that the compiler uses; that's usually ASCII, but it doesn't have to be. You can write code for this without assuming that you're dealing with ASCII. – Pete Becker Jan 08 '18 at 16:39
  • @TeodorJovanovski, that makes sense. `3` is neither an uppercase letter nor a lowercase letter. – R Sahu Jan 08 '18 at 16:40
  • @PeteBecker Practically speaking, support for other encodings depends on (1) the right locale for that encoding being installed and (2) the programmer knowing what to do with it. – n. m. could be an AI Jan 08 '18 at 16:41
  • Utilize the [std::isalpha](http://en.cppreference.com/w/cpp/string/byte/isalpha) function followed by a [std::isupper](http://en.cppreference.com/w/cpp/string/byte/isupper) and/or [std::islower](http://en.cppreference.com/w/cpp/string/byte/islower) functions. – Ron Jan 08 '18 at 16:43
  • What do you expect `toupper('3')` to return? If it cannot convert the input character to upper case, it just returns the input unchanged, therefore '3' itself. – SirDarius Jan 08 '18 at 16:44
  • @n.m. -- support for **changing** encodings depends on locales. If you look at the documentation for `toupper`, `tolower`, `isupper`, and `islower`, you won't see any dependency on ASCII; without messing with locales they apply to whatever character set the **compiler** supports natively, be that ASCII, EBCDIC, or whatever. There is **no dependency** on ASCII in the question, and there should be none in the code that answers it. – Pete Becker Jan 08 '18 at 16:44
  • Okay, solved it. Added two booleans and used isupper/islower. Thanks everyone :) – T jey Jan 08 '18 at 16:44
  • @PeteBecker: Ok, I meant as opposed to some form of Unicode that would require a locale. But you're right, they will also work with other single byte encodings like EBCDIC. – Fred Larson Jan 08 '18 at 16:54
  • @PeteBecker you misunderstood. I'm not saying "your compiler will support ASCII by default so go ahead and program for ASCII". I'm saying "your compiler will support ASCII by default **but you will need something else** and you need to know how to set your system up for something else". Prigrammers with EBCDIC machines probably know by now their charset is very different from ASCII so they don't need any of this talk. – n. m. could be an AI Jan 08 '18 at 17:02
  • @n.m. -- the comment that I responded to says: "Are you dealing with ASCII? See `::isupper()` and `::islower()`". The first sentence has **nothing to do with the question nor with the correct solution**. The second sentence is correct regardless of whether the program is "dealing with ASCII". – Pete Becker Jan 08 '18 at 19:00

1 Answers1

-1
int main(){
     char t;
     cin>>t;
     if (t>='A' && t<='Z') cout<<"UPPER";
     else if(t>='a' && t<='z') cout<<"LOWER";
     else cout<<"ERROR";
     return 0;
     }

Even a char is not character, toUpper() will return it as as, this is why your code not working.

m. c.
  • 867
  • 5
  • 12