I'm creating a registration C++ form and everything is working except one thing (at this point anyways).

private: System::Void Register_Click(System::Object *  sender, System::EventArgs *  e)
			 {
				 Username = UsertxtBox->Text;
				 Password = PasstxtBox->Text;
				 if ( Username == "" || Password == "" ) {
					 ErrorTxtBox->Text = "Error in Registration.";
				 }
				 else { RegCompleteBox->Text = "Registration Complete!"; }
			 }

I'm trying to make so you have to type a character in for username and password. If this is the case it goes the the error txt box and says so. However, it always returns false and returns the else statement true.

Any advice what I'm doing wrong?

I'm trying to make so you have to type a character in for username and password. If this is the case it goes the the error txt box and says so. However, it always returns false and returns the else statement true.

Any advice what I'm doing wrong?

#include <stdio.h>

int main(void)
{
   int i, j;
   puts("Simulation of current:");
   for ( i = 0; i < 2; ++i )
   {
      for ( j = 0; j < 2; ++j )
      {
         if ( i || j )
         {
         }
         else
         {
            printf("else: %d, %d\n", i, j);
         }
      }
   }
   puts("Try this:");
   for ( i = 0; i < 2; ++i )
   {
      for ( j = 0; j < 2; ++j )
      {
         if ( i && j )
         {
         }
         else
         {
            printf("else: %d, %d\n", i, j);
         }
      }
   }
   return 0;
}

/* my output
Simulation of current:
else: 0, 0
Try this:
else: 0, 0
else: 0, 1
else: 1, 0
*/

I think I understand what your saying, I just tried this:

Username = UsertxtBox->Text;
				 Password = PasstxtBox->Text;
				 int i, j;
				 for ( i = 0; i < 2; ++i )
				 {
					for ( j = 0; j < 2; ++j )
					{
						if ( Username == "" && Password == "" )
					    {
							  ErrorTxtBox->Text = "Error in Registration.";
					    }
						else
						{
							RegCompleteBox->Text = "Registration Complete!";
						}
					 }
				 }

However I still get the same error. Am I understanding right about what you are hinting?

>Am I understanding right about what you are hinting?

No. The 0's and 1's of i and j are substitutes for the results of the comparisons Username == "" and Password == "".

I was suggesting that you might want to change your || or &&, and leave the rest the same. After all, you only want to continue if both username and password are nonempty.

The i and j stuff was an attempt to show you the truth table.

But now I notice that you've got the if-else worded negatively. Fragments are more difficult to debug than real code, so sorry that I didn't notice. You may need to invert the if clause or the comparison.

Okay, I figured it out. I rewrote the whole thing to this:

Username = textBox1->Text;
				Password = textBox2->Text;
				
	/*	Check if Password and Username Contain a Character	*/
				if (Username->CompareTo(S"") == 0 || (Password->CompareTo(S"") == 0))
					textBox3->Text = "Error in Registration";
	//*** If character returns true ***
				else if (Password->CompareTo(S"") == 1)
					textBox4->Text = "Registration Complete!";

			 }

Now it works :) Thanks for your help, it helped me understand what I did wrong in the if/else statement:

But now I notice that you've got the if-else worded negatively. Fragments are more difficult to debug than real code, so sorry that I didn't notice. You may need to invert the if clause or the comparison.

This article has been dead for over six months. Start a new discussion instead.