To toupper in C++

Hi,

How can I make the output to always be uppercase? I tried a few thing but I cannot make work.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <string>
#include <cstring>
#include <algorithm>

using namespace std;

int main()
{
    const int SENTENCE = 20;
    char sentence[SENTENCE];

    cout << endl << "Enter a sentence: " << endl;
    cin.getline(sentence, SENTENCE);
    cout << "You entered: ";
    
    for(int i = 0; i < SENTENCE; i++)
    {
        cout << sentence[i];
    }

   //system("pause"); // pause system
   return 0;
}


Thanks a lot for your help
toupper from <cctype> (works with single characters)

http://www.cplusplus.com/reference/clibrary/cctype/toupper/
It works with a single char, but cannot make it work with the array. I cannot figure this out.

cout << toupper(sentence[i]);
I already tried this but it doesn't work.
because it returns an integer that you'll have to cast back to char.

If you want to apply toupper to the whole array you can call std::transform
http://www.cplusplus.com/reference/algorithm/transform/
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
int main (void) {

    const int SENTENCE = 20;
    char sentence[SENTENCE];

    cout << endl << "Enter a sentence: " << endl;
    cin.getline (sentence, 20);
    cout << "You entered: ";

    int I = 0;

    while ( I < SENTENCE && sentence [I]) {
        if ( sentence [I] != ' ')
            sentence [I] &= 0x5f;

            I++;
        }

    cout << sentence << endl;

    return 0;
    }
Thank you all for your help!
Topic archived. No new replies allowed.