Problem using if statement

I'm writing a program for my beginner level C++ class, and I decided to venture ahead and try out if statements. The program is supposed to convert an uppercase letter to a lowercase letter using the ASCII IDs. I can get it to do that, but I decided to use if to allow me to convert uppercase to lowercase *as well as* lowercase to uppercase. I'm getting an error on the if lines 10 and 11 stating "expected primary-expression before '>=' token." This is probably really simple, but I've not been able to figure it out. I'd appreciate the help. Thanks so much!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
using namespace std;
int main ()
{
    char uppercase;
    int number;
    cout << "Enter an uppercase letter. \n";
    cin >> uppercase;
    number = static_cast<int>(uppercase);
    if (90)>=(number)>=(65); {cout << static_cast<char>(number+32);}
    if (122)>=(number)>=(97); {cout << static_cast<char>(number-32);}
    system("pause");
    return 0;
}
Last edited on
You need not to use a casting because char is an integral type. So this statement

number = static_cast<int>(uppercase);

can be removed

As for other erroneous statements then instead of

if (90)>=(number)>=(65); {cout << static_cast<char>(number+32);}
if (122)>=(number)>=(97); {cout << static_cast<char>(number-32);}

you should write

if ( uppercase <= 90 && uppercase >= 65 ) { cout << static_cast<char>(uppercase + 32 ); }
if ( uppercase <= 122 && uppercase >= 97 ) {cout << static_cast<char>(uppercase -32 ); }


Also instead of magic numbers I would use symbols

if ( 'A' <= uppercase && uppercase <= 'Z' ) { cout << static_cast<char>(uppercase + ' ' ); }
if ( 'a' <= uppercase && uppercase <= 'z' ) {cout << static_cast<char>(uppercase - ' ' ); }
Last edited on
In the syntax of an if statement, the bit that gets run if the condition is true is the bit before the semi-colon, or if there is no semi-colon, the bit between braces.

Let's take a look at your code:
if (90)>=(number)>=(65); {cout << static_cast<char>(number+32);}

Firstly, this: if (90)>=(number)>=(65) is a total mess and is not how conditions work. I expect you meant:
if ((number <= 90) && (number >= 65))

Now, let's look at the bit that gets executed if the condition is true. It's the bit before the semi-colon. What's before the semi-colon? NOthing. So there is nothing to run. I expect you meant this:

if (90)>=(number)>=(65) {cout << static_cast<char>(number+32);}

Now there is no semi-colon after the if condition, so the bit in braces will get run if the condition is true (once you've fixed the condition as I described above).



Thank you both so much. I figured my conditional statements were really effed, but at the beginner level that I'm at some of the directions read like Chinese to me. Yeah, and the semi-colon thing must've been something I misunderstood from the error messages given to me by the compiler. Anyway, it's working now.

Thanks again!
Topic archived. No new replies allowed.