Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing a lowercase character to uppercase in c++

Here is the code that i wrote. When i enter a lowercase character such as 'a', it gives me a blank character but afterwards it works well. Can you tell me what i did wrong? Thanks. :)

#include <iostream>
#include <string>
using namespace std;

int main()
{
    char letter;

    cout << "You will be asked to enter a character.";
    cout << "\nIf it is a lowercase character, it will be converted to uppercase.";
    cout << "\n\nEnter a character. Press . to stop: ";

    cin >> letter;

    if(islower(letter))
    {
        letter = isupper(letter);
        cout << letter;
    }

    while(letter != '.')
    {
        cout << "\n\nEnter a character. Press . to stop: ";
        cin >> letter;

        if(islower(letter))
        {
            letter = toupper(letter);
            cout << letter;
        }
    }

    return 0;
}
like image 782
Manisha Singh Sanoo Avatar asked Mar 15 '14 15:03

Manisha Singh Sanoo


2 Answers

Because you print a bool value (i.e. false, aka, NUL character here) in the first time.

You should change

letter = isupper(letter);

to

letter = toupper(letter);
like image 89
herohuyongtao Avatar answered Sep 28 '22 11:09

herohuyongtao


Look here:

if(islower(letter))
{
    letter = isupper(letter);
    cout << letter;
}

If the character is lower, then you assigned it the return value of isupper. That should be 0. So you print a null character.

Why don't you just call toupper for every character that you enter? If it's lower it will convert it, if it is already upper it won't do anything.

like image 30
Marius Bancila Avatar answered Sep 28 '22 11:09

Marius Bancila