asciII

I need to write a program that reads 2 characters and chooses the largest and then converts it to upper case. what I wrote is working and it is converting but its displaying it in its asci2 code rather then the letter , so for b its showing '66' instead of 'B', why is this ?

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>

using namespace std;

int main()

{
char key1, key2 , largest;
cout << " Please enter 2 lower case characters. " << endl;
cin >> key1 >> key2 ;
//char (key1 - 32); //dont even need was trying to convert here at first...
//char (key2 - 32); // "" 
largest = key1;

	{
	if (largest < key2)
	largest = key2;
	}

cout << " The largest character is, " << largest - 32 << endl;

return(0);
}
Try like this:
<< (char)(largest - 32) <<
As to why it is like this - there must be some implicit casting to int type involved here, and I am not sure what are the exact rules, so not going to feed you any bsht.
Last edited on
I'm not sure why you are subtracting 32 but there is a built in feature that can upper case a letter call toupper

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 key1, key2 , largest;
cout << " Please enter 2 lower case characters. " << endl;
cin >> key1 >> key2 ;

largest = key1;

	if (largest < key2){
        largest = key2;
    }

cout << " The largest character is, " << largest << endl;
largest = toupper(largest);
cout << "Upper Case is " << largest << endl;

return(0);
}


if you really want to do the arithmetic though it would look like this

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 key1, key2 , largest;
cout << " Please enter 2 lower case characters. " << endl;
cin >> key1 >> key2 ;

largest = key1;

	if (largest < key2){
        largest = key2;
    }

cout << " The largest character is, " << largest << endl;
largest = largest + ('A' - 'a');
cout << "Upper Case is " << largest << endl;

return(0);
}



edit...

I think i read that the ascii table can be different depending on what operating system you use so it's probably not a good idea to use a hard value for an upper casing program.


My value is -32 also. I still don't think i would do it this way though.

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>

using namespace std;

int main()

{
char key1, key2 , largest;
cout << " Please enter 2 lower case characters. " << endl;
cin >> key1 >> key2 ;

largest = key1;

	if (largest < key2){
        largest = key2;
    }

cout << " The largest character is, " << largest << endl;
cout << "A - a is equal to " << ('A' - 'a') << endl;
largest = largest - 32;
cout << "Upper Case is " << largest << endl;

return(0);
}
Last edited on
wow thanks so much for the quick replies. the professor in class had mentioned that if we wanted to covert we simply had to subtract or add 32 depending on what direction we wanted to go.
I used JockX method because it was the one that barley changed what I had already wrote. Does anyone know why though ? id really like to know to help my understanding.
Looking at the ascii table here

http://www.asciitable.com/

We know there are 26 letters in the alphabet.

and it looks like there are 6 characters between upper case and lower case alphabet.

so 26+6 = 32. Guess that's where that comes from.
i meant why did it matter to do it << (char) (largest - 32) << as opposed to << largest - 32 <<
not sure, that last program i put did largest - 32 without char in the subtraction and it worked fine.

I don't know if it makes a difference.
As to why casting to char is neccessary: expression (largest + 32) is not neccessarily of type char. In fact, the + operator causes Integral promotion:
http://en.cppreference.com/w/cpp/language/implicit_cast#Numeric_promotions
thank you
Topic archived. No new replies allowed.