so im starting in C++ programming and im working out of a textbook. At the moment im stuck on the question asking me to determine using an input(cin) whether an input is a lowercase or uppercase letter. I am particularly confused on how to determine if an input is upper or lower case. I am able to create the cin statement and the use of if-else. the full question is written below.

Write a C++ program that accepts a character using the cin object and determines whether the character is a lowercase letter. A lowercase letter is any character that is greater than or equal to 'a' and less than or equal to 'z'. If the entered character is a lowercase letter, display the mssage The character just entered is a lowercase letter. If the entered letter is not lowercase, display the message The character just entered is not a lowercase letter.

thanks in advance

Unfortunately, the forum rules require that we obtain from you evidence of your own effort before we can help, and there really isn't any here. Please see this.

Besides, if we don't see what you've done, we have no way of helping you anyway. We're not mind-readers; we assume a lot whether correct or not :P.

What have you tried so far? What does your if statement look like? The assignment's instructions are pretty clear about what you need.

hi !
hope this helps!

pls look into the code here:

# include <iostream>


using namespace std;

void main()
{
	char ch;
	cout << "Please enter a character:";
	cin >> ch;
	int i = int(ch);
	if ( i >= 97 && i <= 122)
		cout << "The character just entered is a lowercase letter." <<endl;
	else
		cout << "The character just entered is not a lowercase letter." <<endl;

}

Thanks,
Arun P
(Be Greateful to the one who created us!)

commented: You've been around long enough, you should know better. -1
commented: read FBody's post. Do not do peoples' work for them. -3

hi !
hope this helps!

pls look into the code here:

# include <iostream>


using namespace std;

void main()
{
char ch;
cout << "Please enter a character:";
cin >> ch;
int i = int(ch);
if ( i >= 97 && i <= 122)
cout << "The character just entered is a lowercase letter." <<endl;
else
cout << "The character just entered is not a lowercase letter." <<endl;

}

Thanks,
Arun P
(Be Greateful to the one who created us!)

You've been around long enough, you should know better. Also, please use code tags.

Additionally, main() should always be an int, not void and You have an extra step in there that isn't necessary. You can compare and manipulate char values directly, like anything else. If your input is a char, you should compare directly to a char, you don't need to cast it to an int:

char input = '\0';
cin.get(input);
if (input == 'a') {
  cout << "You entered 'a'." << endl;
} else {
  cout << "You entered something else." << endl;
}

its not mandatory that it should return int.
i just want to help that person..
also its not necessary to maintain the syntax here..
Its worth if the person who posted this thread is convinced with the replies..
so beter stop leaving these silly comments!

commented: Why would you not "maintain the syntax"? -2

its not mandatory that it should return int.
i just want to help that person..
also its not necessary to maintain the syntax here..
Its worth if the person who posted this thread is convinced with the replies..
so beter stop leaving these silly comments!

Actually it is, some "loose" compilers make it optional by allowing void and may issue a warning about the improper declaration; however, a strictly-compliant compiler will refuse to compile and make it compulsory/mandatory that it be declared int.

Simply giving them code that they can cut/paste and turn in does not help them, they learn nothing from it and it damages the site's reputation.

What's "its not necessary to maintain the syntax" supposed to mean? What are you talking about???

its not mandatory that it should return int.

The specific rules are subtle and varied. It's far easier to return int and guarantee that your code will work everywhere than to be stubborn and risk introducing bugs.

also its not necessary to maintain the syntax here..

Yes, yes it is. Read the rules.

hare..
ok man i will read the rules later.
Anyhow thanks!

Dear banf,
pls go thro the code whatever i have replied u..

thanks!

pls go thro the code whatever i have replied u..

Yes, let's.

>using namespace std;
If you're going to have a using directive, at least refrain from placing it in the global scope.

>void main()
Here are the rules as specified by the C++ standard:

  • On freestanding implementations (eg. embedded programs and operating systems) you can return anything you want
  • On hosted implementations you must return one of the types the implementation supports

Here's the kicker: not all implementations support a void return, but all of them support an int return. The most humorous part is that standard C++ allows omission of the return statement and 0 will be assumed, so here are the two definitions of main side by side:

void main() {}
int main() {}

The only excuse void mainers had was that returning void was more concise due to the return statement. Now that excuse is gone, so why not just return int? I'm genuinely curious.

>cin >> ch;
It's generally a good idea to check input for success and handle failures accordingly.

>int i = int(ch);
This is unnecessary. char is already an integer type.

>if ( i >= 97 && i <= 122)
This is not guaranteed to work. You're assuming a specific implementation of the character set where the alphabet characters are consecutive. This is not always the case (eg. EBCDIC). The isupper function exists for a reason, it's recommended that you use it.

Also, 97 and 122 are pretty much magic numbers unless one has been around the block enough times to recognize that you're comparing the ASCII values of 'a' and 'z'. Note that I said earlier that char is already an integer type, so you can remove those magic numbers with character literals:

if (ch >= 'a' && ch <= 'z')

>cout << "The character just entered is a lowercase letter." <<endl;
endl is a pet peeve of mine because many programmers use it everywhere and it's very rarely necessary in favor of '\n'. The definition of endl is as follows (simplified):

ostream& endl(ostream& out)
{
    out.put('\n');
    out.flush();
    return out;
}

The extra step of flushing the stream explicitly is redundant most of the time. My recommendation is to use '\n' instead of endl except when you can prove that an explicit flush of the output stream is needed.

Here's the same program with my suggested changes:

#include <cctype>
#include <iostream>

int main()
{
    using namespace std;

    char ch;

    cout << "Please enter a character: ";
    
    if (!(cin >> ch))
        cerr<<"Unexpected input\n";
    else {
        cout<<"The character entered is ";

        if (!islower(ch))
            cout<<"not ";

        cout<<"a lowercase letter.\n";
    }
}

He could probably bluff the bad code as his own though.;)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.