Character functions

This is currently an unfinished version of a code i'm working on right now. I am wondering why my code shows me digits after i run my code. How do i make it so that when i run my code, it gives me the correct output instead of giving my some random numbers?

For instance, when i type in q, it gives me that q is a lowercase but, it gives me 81 as the output for the uppercase. I don't understand why it does that.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <cstdlib>

using namespace std;

int main()
{
    cout << "Input a character:";
    char ch;
    cin >> ch;
    
    if (islower(ch))
       cout << ch << " is a lowercase letter and " << toupper(ch) << " is the uppercase of the character." << endl; 
    
    if (isdigit(ch))
       cout << ch << " is a digit." << endl;
    else
        cout << ch << " is not a digit." << endl;
    
    system("pause");
    return 0;
}
toupper(ch) returns an integer. Try ... << (char)toupper(ch) << ... on line 13 instead.
Last edited on
Why did that give me the right output just now? Why did putting (char) in front of toupper(ch) help?
It's all about how the << operator interprets types. When it sees toupper(ch) by itself, it sees an int, because that's the return type of toupper(ch). The behavior for int is to print a number. Putting (char) in the front tells it to take the result of toupper(ch) and interpret the number as a character. The behavior of << in regards to char is to print the letter instead of the ASCII code.
Topic archived. No new replies allowed.