/*
a program that changes uppercase to lowercase and lowercase to uppercase.
*/
#include <iostream>
usingnamespace std;
int main()
{
unsignedchar ch;
for(;;)
{
cout << "enter a letter (enter \".\" to exit): ";
cin >> ch;
if(ch == '.') break;
if((ch < 65 || ch > 90) && (ch < 97 || ch > 122))
{
cout << "ERROR: NEEDS TO BE A LETTER, TRY AGAIN:\n";
}
elseif(ch >= 65 || ch <= 90)
{
ch += 32; //change to lowercase
cout << "new letter: \"" << ch << "\"\n";
}
else
{
ch -= 32; //change to uppercase
cout << "new letter: \"" << ch << "\"\n";
}
}
return 0;
}
when ever I test the program it either doesn't change lowercase to upper case or uppercase to lowercase. depending on whether the part that is supposed to change it to lowercase is addition or subtraction.
Rather than using explicit numbers it's better to use the character itself. So rather than ch < 65 || ch > 90 use ch < 'a' || ch > 'z' and so on. And rather than ch += 32; (which is what is wrong btw) to ch = ch - 'A' + 'a'; to change it to lower case. Similarly for ch -= 32;. Although almost always, the character codes are the one you've used, it's not guaranteed. Plus using the actual characters makes it easier to know exactly what's going on.
@bluezor
well, that line does what its supposed to do, checks to see if its a letter or not. if its not it displays an error message and asks you to try again. the error seems to have something to do with this line:
ch += 32; //change to lowercase
if i make it addition the program will change uppercase to lowercase but not lowercase to uppercase, if i make it subtraction the program will change lowercase to uppercase but not uppercase to lowercase. i cant figure out why its not working
/*
a program that changes uppercase to lowercase and lowercase to uppercase.
*/
#include <iostream>
#include <cctype>
usingnamespace std;
int main()
{
unsignedchar ch;
while (true)
{
cout << "enter a letter (enter \".\" to exit): ";
cin >> ch;
if(ch == '.') break;
if (!isalpha(ch))
{
cout << "ERROR: NEEDS TO BE A LETTER, TRY AGAIN:\n";
continue;
}
ch=islower(ch)?toupper(ch):tolower(ch);
cout << "new letter: \"" << ch << "\"\n";
}
return 0;
}