Characters in ASCII code

Write a C++ program that reads a character and tells the user if it is a letter (‘a’…’z’, ’A’…’Z’), a digit (‘0’…’9’), a punctuation mark (‘.’ … ‘!’), or special character (other than these). The problem is that whatever I type, it prints "This is a puncuation mark!"

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
#include <iostream>
#include<windows.h>
using namespace std;

int main()
{
    char c;
    cout << "Enter a character : ";
    cin>>c;
    if (((int)c>=33)&&((int)c>=47))
    {
        cout<<"This is a puncuation mark!"<<endl;
    }
    else if (((int)c>=65 )&&((int)c<=90)||((int)c>=97)&&((int)c<=122))
    {
        cout<<"This is a letter!"<<endl;
    }
    else if (((int)c>=48)&&((int)c>=57))
    {
        cout<<"This is a digit!"<<endl;
    }
    else
    {
        cout<<"This is a special character!"<<endl;
    }
    system("pause");
    return 0;
}
See the changes(the bolds):

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
#include <iostream>
#include <windows.h>
using namespace std;

int main()
{
    char c;
    cout << "Enter a character : ";
    cin>>c;

    if (((int)c>=33)&&((int)c<=47))
    {
        cout<<"This is a puncuation mark!"<<endl;
    }
    else if ((((int)c>=65 )&&((int)c<=90))||(((int)c>=97)&&((int)c<=122)))
    {
        cout<<"This is a letter!"<<endl;
    }
    else if (((int)c>=48)&&((int)c<=57))
    {
        cout<<"This is a digit!"<<endl;
    }

    else
    {
        cout<<"This is a special character!"<<endl;
    }
    system("pause");
    return 0;
}
Enter a character : a
This is a letter!

Enter a character : 5
This is a digit!

Enter a character : :
This is a special character!
It prints "This is a digit!" no matter which number I enter.
Last edited on
It prints "This is a digit!" no matter which number I enter.

Of course it prints "This is a digit!' when you enter only numbers! :-)

For the rest see the outputs above. Please make the changes as I wrote and all will be ok!

EDIT: use copy/paste!
Last edited on
Oh I'm sorry I thought it only worked with numbers.. I must admit that in the beginning I was confused, but now I looked over ascii codes and I understood. Btw thanks the program is great! :))

OK informaticsgirl! All is well when ends well!

I wish you happy programming and a Merry Christmas!
Topic archived. No new replies allowed.