cout and toupper

Say I'm writing a program that takes the input from the user, which is a sentence of some sort, and outputs what he entered with the first letter capitalized.

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
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>

const short MAX_CHARACTERS = 100;

int main()
{
	using namespace std;

	char control = 'y';//this variable is used by user to stay in or exit out of do-while loop
	char sentence[MAX_CHARACTERS];

	do
	{
		cout << "Enter a sentence: ";
		cin.getline(sentence, MAX_CHARACTERS+1);
		bool period_encountered = true;
		short i(0);
		while(sentence[i] != '\0')
		{
			
			if((isalpha(sentence[i])) && period_encountered)
			{
				cout <<  toupper(sentence[i]);
				period_encountered = false;
			}/*
			else if(sentence[i] == '.')
			{
				period_encountered = true;
				cout << sentence[i];
			}*/
			cout << sentence[i];
			i++;
		}

		cout << "\n\nWould you like to run the program again? (Y/N): ";
		cin >> control;
		while ((control != 'y') && (control != 'n'))
		{
			cout << "Incorrect input. Would you like to run the program again? (Y/N): ";
			cin >> control;
			control = tolower(control);
		}
		cin.ignore();
		cin.clear();
	}while(control == 'y');
	
	cout << "Press key to exit: ";
	char exit;
	cin >> exit;

	return 0;
}


So if I type in "tom" the output should be "Tom". However with the above code I get "84tom" instead. I have to replace line 26 with:

1
2
sentence[i] = toupper(sentence[i]);
cout << sentence[i];


Why is this? Why do I have to replace line 26 with the above?
closed account (DSLq5Di1)
toupper returns an integer, you'll need a cast to show its character representation.

cout << static_cast<char>( toupper(sentence[i]) );
Thanks
Topic archived. No new replies allowed.