Hey guys. It's me again. I'm practicing the contents within the <cctype> library. I made my little program that switches the letter you input into the char to it's opposite. So if you input a lowercase 'a' the program will switch the lowercase 'a' to an uppercase 'A'. Upon achieving this outstanding feet, I started adding numbers to my program. Let me paste my unfinished program here:
#include <iostream>
#include <cctype>
usingnamespace std;
void Intro (void);
int main (void)
{
Intro ();
char Letter, Digit;
bool lentered, dentered;
do
{
cout << "Type a letter: ";
cin.get (Letter);
if (! (Letter == '\n'))
cin.ignore ();
cout << "Type a single digit number: ";
cin.get (Digit);
if (! (Digit == '\n'))
cin.ignore ();
}
while ((isalpha (Letter) == false) || (isdigit (Digit) == false));
cout << endl;
cout << islower (Letter) << " islower";
cout << endl;
cout << isupper (Letter) << " isupper";
cout << endl << endl;
if (islower (Letter) == false)
Letter = tolower (Letter);
else
Letter = toupper (Letter);
cout << "Letter after the 'if' statement in code:" << endl << endl;
cout << islower (Letter) << " islower";
cout << endl;
cout << isupper (Letter) << " isupper";
cout << endl;
cout << "The letter you typed is " << Letter << endl;
return 0;
}
void Intro (void)
{
cout << "This is just a test! This will switch a lowercase letter into an" << endl;
cout << "uppercase letter and vice versa." << endl;
cout << endl;
}
The bool under the char you can ignore. I was going to make this program a little bit more complex later on. Anyways, let me get to the question. In the do..while statement in the first section in the code is what is causing me problems. The do..while statement is suppose to keep going until one of its bool is true right? Upon testing this program, the do..while statement only stops when both of the bool statements are true. while ((isalpha (Letter) == false) || (isdigit (Digit) == false));
I have no idea now if I'm using the or operator (||) correctly now. Any help on making it so I can make only one true and the other false? Thanks!
If I run the program and enter 'a' as the letter and 'b' as the number, the loop will keep... looping even though letter is a letter (true) and number is a letter (false). Its like the || is being ignored.
Oh snap! Thanks man. So it'll keep looping until I get both bool true. I got to fix that. Thanks again.
No. No no no no no no no. No.
It will keep looping until both are false. It will loop until the expression in while(expression) comes out as false. You've got an OR statement in there. Here's the truth table:
1 2 3 4
false OR false = falsetrue OR false = truefalse OR true = truetrue OR true = true
Only one of these comes out as false overall; false OR false = false. You need them both to be false to stop the loop.
Letter is a character. It will only be false with the numerical value of zero. Not the character zero, but the numerical value of zero, and even then you're jamming a meaning into a char that shouldn't really be there.
Digit is a character. It will only be false with the numerical value of zero. Not the character zero, but the numerical value of zero.
Did you perhaps means (isalpha (Letter) and/or isdigit (Digit) is false? There is an enormous difference.
while ((isalpha (Letter) == false) || (isdigit (Digit) == false));
I'm using both isalpha (Letter) and isdigit (Digit). I know Letter is a character and when using isalpha (Letter) it will return 0 or 1 (true or false). The loop will continue if Letter is not a character from the alphabet (isalpha (Letter) == false). The loop will also continue if Digit is not a number character (isdigit (Digit) == false). Have one true and the other false will still make the program loop. I need both, Letter and Digit to be true for the loop to stop. You answered this a long time ago man. What am I not understanding?!
I need both, Letter and Digit to be true for the loop to stop. You answered this a long time ago man. What am I not understanding?!
The difference between saying "Letter is true" and "isalpha (Letter) is true." Your code is correct and I believe you understand it; you just keep saying you need Letter and Digit to be true, which isn't right.
Here, look at this code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
#include <iostream>
#include <cctype>
usingnamespace std;
int main()
{
char Letter = '~';
if (Letter)
{
cout << "Printing this, so Letter must be true!";
}
if (isalpha (Letter))
{
cout << "You don't see this, so isalpha (Letter) must be false!";
}
return 0;
}
Letter can be true all on its own. You don't need Letter to be true; you need isalpha(Letter) to be true. Saying Letter needs to be true, which is what you keep saying, is not the same as saying you isalpha(Letter) needs to be true.