why won't this code 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
25
26
27
28
29
30
31
32
33
/*
    a program that changes uppercase to lowercase and lowercase to uppercase.
*/

#include <iostream>

using namespace std;

int main()
{
  unsigned char 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";
    }
    else if(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.
Last edited on
Might want to check the logic in this line:

if((ch < 65 || ch > 90) && (ch < 97 || ch > 122))
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.
Btw ch -= 32; should be ch = ch - 'a' + 'A'; for uppercase
@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
Last edited on
try changing that line like mentioned above
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
/*
    a program that changes uppercase to lowercase and lowercase to uppercase.
*/

#include <iostream>

using namespace std;

int main()
{
  unsigned char ch;
  for(;;)
  {
    cout << "enter a letter (enter \".\" to exit): ";
    cin >> ch;
    if(ch == '.') break;
    else if(ch >= 65 && ch <= 90)
    {
      ch +=32; //change to lowercase
      cout << "new letter: \"" << ch << "\"\n";
    }
    else if (ch>=97 && ch<=122)
    {
      ch -= 32; //change to uppercase
      cout << "new letter: \"" << ch << "\"\n";
    }
    else
    {
        cout << "ERROR: NEEDS TO BE A LETTER, TRY AGAIN:\n";
    }
  }
  return 0;
}
ok, that worked, thanks
My turn! :P
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
/*
    a program that changes uppercase to lowercase and lowercase to uppercase.
*/

#include <iostream>

using namespace std;

int main()
{
    unsigned char ch;
    do
    {
        cout << "enter a letter (enter \".\" to exit): ";
        cin >> ch;
        if( 'A' <= ch && 'Z' >= ch )
        {
            //change to lowercase
            ch += 'a' - 'A';
            cout << "new letter: \"" << ch << "\"\n";
        }
        else if( 'a' <= ch && 'z' >= ch )
        {
            //change to uppercase
            ch -= 'a' - 'A';
            cout << "new letter: \"" << ch << "\"\n";
        }
        else
        {
            cout << "ERROR: NEEDS TO BE A LETTER, TRY AGAIN:\n";
        }
  } while( '.' != ch );
  return 0;
}
I won't let you win this! :D

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
/*
    a program that changes uppercase to lowercase and lowercase to uppercase.
*/

#include <iostream>
#include <cctype>
using namespace std;

int main()
{
    unsigned char 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;
}
Last edited on
Topic archived. No new replies allowed.