Problem w/ the do..when Statement

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:
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include <iostream>
#include <cctype>

using namespace 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!
You're not clear.

Right now, the loop will loop as long as Letter is not alphanumeric and/or Digit isn't a number.

What do you want it to do instead?
@Moschops

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.
That's what || does. It means "or". It means that the loop will continue if either side is true.

letter is a letter (true) and number is a letter (false)

So, TRUE or FALSE, which comes out as TRUE.

Here's the logical OR truth table:

false OR false = false
true OR false = true
false OR true = true
true OR true = true
Last edited on
@Moschops

Oh snap! Thanks man. So it'll keep looping until I get both bool true. I got to fix that. Thanks again.
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 = false
true OR false = true
false OR true = true
true 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.
Last edited on
@Moschops

while ((isalpha (Letter) == false) || (isdigit (Digit) == false));

The loop will keep going if Letter and/or Digit is false. I need them both true to make the loop stop. No?
Last edited on
No. The clue is in the word while.

Do SOMETHING while SOME-CONDITION

As long as SOME-CONDITION is true, SOMETHING will keep happening.

As it is, your code does the correct thing at the moment; it keeps looping until you make Digit a number and Letter something alphanumeric.
Last edited on
@Moschops

That's what I'm saying. If Letter and/or Digit is false, the loop will continue. I need both, Letter and Digit to be true 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.
Last edited on
@Moschops

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>
using namespace 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.

Last edited on
Oh wow. Guess I needed to be more specific. Sorry for wasting your time lol I was like, "WTF". Sorry =p
Topic archived. No new replies allowed.