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 ?
#include <iostream>
usingnamespace 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.
#include <iostream>
usingnamespace 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
#include <iostream>
usingnamespace 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.
#include <iostream>
usingnamespace 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);
}
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.