To upper & to lower problems?

Hi, I have run into an error on an exercise and I can't figure out why.

The goal of the program is to intake characters(letters only) and repeat them with the opposite capitalization, the problem I am running into is that it repeats the characters but in the same capitalization.

Could anyone shed some light on what I am doing incorrectly? (p.s. very jet lagged at the moment, i may fall asleep soon so my reply may be delayed.)

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

using namespace std;

int main()
{
    char ch;
    cout << "Enter characters: ";
    while (ch != 'q')
    {
        cin.get(ch);
        if (isalpha(ch) && islower(ch))
        {
            toupper(ch);
            cout << ch;
        }
        else
        {
            tolower(ch);
            cout << ch;
        }

        cout << endl;
        }
}

You're applying the toupper and tolower operations but never assigning the results to ch. Try this:

1
2
3
4
5
ch = toupper(ch);

//...

ch = tolower(ch);
The reason it shows the same case is you call the function. But functions do not change the value without you telling it to.
Try this (I am assuming your toupper and tolower function return a char value)
cout << toupper (ch)
and
cout << tolower (ch)
Note: This does not change the value but merely displays it
Hope this helps :)
Last edited on
tolower() and toupper() return the modified character.
So lines 15 and 20 should be:
1
2
3
            ch = toupper(ch);

            ch = tolower(ch);


Edit: too slow. Is there a prize for that? :)
Last edited on
Catfish received the "5 minutes behind" title

*Title jingle*
Woot, you guys are awesome! Thanks for the help.

Also solved the digit problem, just added another if statement.

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

using namespace std;

int main()
{
    char ch;
    cout << "Enter characters: ";
    while (ch != 'q')
    {
        cin.get(ch);
        if (isdigit(ch))
            continue;
        if (isalpha(ch) && islower(ch))
        {
            ch = toupper(ch);
            cout << ch;
        }
        else
        {
            ch = tolower(ch);
            cout << ch;
        }

        cout << endl;
        }
        return 0;
}

What about special characters? If inputted, they will fall into the else case.
by special characters do you mean iscntrl & ispunct functions? If so could I just not add to line 13 with the || ?

Also I removed the solved because I have a new question on the next exercise.

When I run the code the first character I entered is not accepted as a possible choice even if it is a valid one, can anyone shed some light?

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

using namespace std;

int main()
{
    cout << "Please enter one of the following choices: \n";
    cout << "c)carnivore  \t\tp)player\n  t)tree  \t\t\tg)game\n";
    char ch;
    cin.get(ch);
    while(cin >> ch)
    {
        switch (ch)
        {
        case 'c':
            cout << "dinosaur\n";
            break;
        case 't':
            cout << "maple\n";
            break;
        case 'p':
            cout << "Shaw\n";
            break;
        case 'g':
            cout << "Counter-strike\n";
            break;
        default:
            cout << "I didn't recognize your input.\nPlease enter a valid choice: ";
            break;
        }
    }
    cin.get();
    return 0;
}
by special characters do you mean iscntrl & ispunct functions?

I meant any character that's not a digit or part of the alphabet. So, in your previous code, characters such as '*', '$', '~', '+', '/' etc. would go into your else case.

If so could I just not add to line 13 with the || ?

With your switch as above, any character entered that's not a lower case c, t, p, or g would go into the default case, so non-digit, non-alphabet characters are already inherently taken care of.

When I run the code the first character I entered is not accepted as a possible choice even if it is a valid one, can anyone shed some light?

The user's first choice is going into cin.get(ch);, then their next choice is going into cin >> ch. You should remove cin.get(ch);
you are awesome, thanks a ton(sorry for the delayed response)
Topic archived. No new replies allowed.