char variables

Hello there, I want to ask about how would I go about not making chars case sensitive like when the user entered 'A' but the char variable is 'a', how would I go about this?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
using namespace std;

int main ()
{
	char a;

	cout << "Please enter letter a: " << endl;
	cin >> a;

	if (a=='a')
	{
		cout << "Congrats you entered a!" << endl;
	}
	system ("pause");
		return 0;
}
Last edited on
char c;

std::cout << "Enter a character: ";
std::cin >> c;

if ( std::isupper( c ) ) c = std::tolower( c );
what's isupper and tolower?
you could also try if (a=='a'||a=='A')
or even

if (tolower(a) == 'a') // or with std:: is you're not using namespace std

Andy

PS I don't usually bother with islower to check a char if all I'm doing is lowering its case. When you pass a lower case char it just gives it back to you; so I would just use

c = tolower(c);
Last edited on
@sanasuke15
What's isupper and tolower?


They are standard C functions declared in header <cctype>.
> I don't usually bother with islower to check a char if all I'm doing is lowering its case.

Yes.

This is just plain stupid: if ( std::isupper( c ) ) c = std::tolower( c );

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
#include <cctype>

char foo( char c )
{
    if( std::isupper(c) ) c = std::tolower(c) ;
    return c ;

    /* g++ -std=c++11 -pedantic-errors -Wall -Wextra -Werror -O3 -c -S
    __Z3fooc:
        pushl	%esi
        pushl	%ebx
        subl	$20, %esp
        movl	32(%esp), %edx
        movsbl	%dl, %esi
        movl	%edx, %ebx
        movl	%esi, (%esp)
        call	*__imp__isupper
        testl	%eax, %eax
        je	L2
        movl	%esi, (%esp)
        call	*__imp__tolower
        movl	%eax, %ebx
    L2:
        addl	$20, %esp
        movl	%ebx, %eax
        popl	%ebx
        popl	%esi
        ret
	*/
}

char bar( char c )
{
    c = std::tolower(c) ;
    return c ;

    /* g++ -std=c++11 -pedantic-errors -Wall -Wextra -Werror -O3 -c -S
        subl	$28, %esp
        movsbl	32(%esp), %eax
        movl	%eax, (%esp)
        call	*__imp__tolower
        addl	$28, %esp
        ret	*/
}
Last edited on
Topic archived. No new replies allowed.