Reputation: 183
Slight issue. (Not using toupper() and tolower() functions) I understand what converting to uppercase and lowercase using numerical values is but following my C++ book, why is the conversion at the end of this statement and not before?:
if (letter >= 'A') //test for 'A' or larger
if (letter <= 'Z') //test for 'Z' or smaller
{
cout << endl
<< "You entered a capital letter."
<< endl;
letter += 'a' - 'A'; //Convert to lowercase
return 0;
}
if (letter >= 'a') //test for 'a' or larger
{
if (letter <= 'z') //test for 'z' or smaller
{
cout << endl
<< "You entered a small letter."
<< endl;
return 0;
}
}
Why would it convert the uppercase to lowercase at this point of code execution since the second if statement deals with lowercase input?
Upvotes: 0
Views: 673
Reputation: 42103
The given snippet could be the body of the function:
int convert(char& letter)
{
if (letter >= 'A' && letter <= 'Z')
{
letter += 'a' - 'A';
return 0; // go out of this function...
}
else if (letter >= 'a' && letter <= 'z')
{
letter += 'A' - 'a';
return 0; // go out of this function...
}
return -1; // it wasn't a letter as we expected
}
Note, that there's a possible path that doesn't match none of these 2 situation. Let's say that letter
is '?'
, since you're returning int
value, there should be an indication that something is wrong (it's up to you how you deal with error handling).
Possible usage of your this function could look like this:
char letter = '!';
if (convert(letter) == 0)
// success ...
else
// error ...
If the question is really about leaving the scope of function, then this question could be helpful too:
How to break out of a function
Concrete example:
void convertLetterAndPrintResult(char& letter)
{
if (convert(letter) == 0)
std::cout << letter << std::endl;
else
std::cout << "ERROR: '" << letter << "' is not valid character!" << std::endl;
}
int main()
{
char letter = '!';
convertLetterAndPrintResult(letter);
letter = 'g';
convertLetterAndPrintResult(letter);
letter = 'L';
convertLetterAndPrintResult(letter);
}
Output:
ERROR: '!' is not valid character!
G
l
Upvotes: 3
Reputation: 2407
Why would it convert the uppercase to lowercase at this point of code execution since the second if statement deals with lowercase input?
That is because
return 0
means that the function is finished. The lines
if (letter >= 'a') //test for 'a' or larger
{
if (letter <= 'z') //test for 'z' or smaller
{
cout << endl
<< "You entered a small letter."
<< endl;
return 0;
}
}
will not be executed if letter was originally an upper case letter. It would print out "You entered a capital letter.", then convert it to lower case, then exit.
why is the conversion at the end of this statement and not before?
It would make no difference if the conversion were before the cout statement.
Upvotes: 1
Reputation:
Because there's a return 0;
statement in the first part. If the original character was uppercase, the control flow doesn't even reach the second nested if () { if () { } }
part.
Upvotes: 2