Need a little help

I would like my if then statements to accept both uppercase letters and lowercase. I know you could just code it in using something like
if ((change=='N') && (change=='n')) but I was wondering if there is an easier way. I know my code is probably pretty rudimentary if only been coding since last week so please go easy on it.

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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
  #include <iostream>

using namespace std;

long int addition (long int a, long int b)
{
    long int c;
    c=a+b;
    return (c);
}

long int subtraction (long int a, long int b)
{
    long int c;
    c=a-b;
    return (c);
}

long int multiplication (long int a, long int b)
{
    long int c;
    c=a*b;
    return (c);
}

long int division (long int a, long int b)
{
    long int c;
    c=a/b;
    return (c);
}
void printmessage ()
{
    cout << "\n" << "INVALID RESPONSE!" << "\n" << "\n";
}

int main ()
{
    long int a, b, c;
    char change;
    char done;

    cout << "Welcome to V.2 of the calculator." << "\n";
    cout << "Please enter your first number: ";
    cin >> a;

        do{
            cout << "Please enter your next number: ";
            cin >> b;

            do{

                cout << "Would you like to add [A], subtract [S], divide [D], or multliply [M]" << ": ";
                cin >> change;

                    if (change=='A')
                    {
                        c = addition (a,b);
                        cout << a << " + " << b << " = " << c;
                    }
                    else if (change=='S')
                    {
                        c = subtraction (a,b);
                        cout << a << " - " << b << " = " << c;
                    }
                    else if (change=='D')
                    {
                        c = division (a,b);
                        cout << a << " / " << b << " = " << c;
                    }
                    else if (change=='M')
                    {
                        c = multiplication (a,b);
                        cout << a << " * " << b << " = " << c;
                    }
                    else
                    {
                        printmessage();
                    }
                }while ((change!='A') && (change!='S') && (change!='M') && (change!='D'));

            cout << "\n";
            cout << "Are you done with: " << c;
            cout << "\n";

            do{

                cout << "Yes [Y], No [N]: ";
                cin >> done;

                while ((done!='N') && (done!='Y'))
                {
                    printmessage();
                    break;
                }

            }while ((done!='Y') && (done!='N'));

        a=c;

        } while (done!='Y');
    return 0;
}
You could explicitly change the case of the letter using cctype functions

http://www.cplusplus.com/reference/cctype/

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>

using namespace std;

int main()
{
    char input;

    while (1)
    {
        cin >> input;
        input = tolower(input);

        if (input == 'a')
        {
            cout << "Success" << endl;
        }
        else
            cout << "Failure" << endl;
    }

return 0;
}


Also if ((change=='N') && (change=='n')) wouldn't work because change cannot be 'N' and 'n' at the same time. I think what you're looking for is ||
Last edited on
Thanks worked perfectly thanks for the help.
Topic archived. No new replies allowed.