6 Replies - 1617 Views - Last Post: 12 February 2009 - 07:16 PM Rate Topic: -----

#1 c++struggler   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 05-July 08

Unable to get out of loop

Post icon  Posted 12 February 2009 - 08:59 AM

I am having a problem getting out of the loop where I ask the user to type in true or false to whether the account is admin or not. It catches if I don't type in true or false and tells me to try again but if I type in true or false it just keeps asking for admin account.

do
			{
				try
				{
					Console.Write("Admin Account type True or False: ");
					accountStruct.admin = Convert.ToBoolean(Console.ReadLine());

					if (accountStruct.admin != true || accountStruct.admin != false)
					{
						Console.WriteLine("Must type in true or false for admin account.");
						Console.Write("Admin Account: ");
						accountStruct.admin = Convert.ToBoolean(Console.ReadLine());
					}
				}
				catch(FormatException)
				{
					Console.WriteLine("Must type in true or false");
				}
			} while (accountStruct.admin != true || accountStruct.admin != false);



Is This A Good Question/Topic? 0
  • +

Replies To: Unable to get out of loop

#2 eclipsed4utoo   User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1536
  • View blog
  • Posts: 5,972
  • Joined: 21-March 08

Re: Unable to get out of loop

Posted 12 February 2009 - 09:10 AM

since "bool" can only be true or false, you can never get out of your while loop. You need to make the boolean value nullable...

bool? admin = null;
do
{
	try
	{
		Console.Write("Admin Account type True or False: ");
		admin = Convert.ToBoolean(Console.ReadLine());
	}
	catch (FormatException)
	{
		Console.WriteLine("Must type in true or false");
	}
} while (admin == null);



also, you don't need the check inside of the try, since if the user types in anything other than true or false, it will fail the conversion.

This post has been edited by eclipsed4utoo: 12 February 2009 - 09:11 AM

Was This Post Helpful? 0
  • +
  • -

#3 rgfirefly24   User is offline

  • D.I.C Lover
  • member icon


Reputation: 473
  • View blog
  • Posts: 2,221
  • Joined: 07-April 08

Re: Unable to get out of loop

Posted 12 February 2009 - 09:25 AM

Lets look at the way your doing it. Instead of trying to have you do it differently.
while (accountStruct.admin != true || accountStruct.admin != false);


you're while loop asks that while:
the variable accountStruct.admin does not equal true OR the variable accountStruct.admin does not equal false

we continue the loop
So senario 1:  User enters true:

while (accountStruct.admin != true || accountStruct.admin != false);
					  false					  OR			  true							  statement true keep looping

Senario 2:  User enters false:

while (accountStruct.admin != true || accountStruct.admin != false);
					   true					  OR			  false						  statement true keep looping

Senario 3:  User enters anything other than true or false:

System catches it and won't let it through to the while loop


Solution:  Use a && instead

Senario 1:  User enters true:

while (accountStruct.admin != true && accountStruct.admin != false);
						 False																	   statement false loop stops

The while doesn't check any farther than the first condition because it already got its false result.

Senario 2: User enters False

while (accountStruct.admin != true && accountStruct.admin != false);
						   True				 AND		False							   statement false loop stops



Basically using an OR both statements have to equate to false in order to get a compound false. With an AND only one statement has to be false to trigger the compound false.

This post has been edited by rgfirefly24: 12 February 2009 - 09:29 AM

Was This Post Helpful? 0
  • +
  • -

#4 eclipsed4utoo   User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1536
  • View blog
  • Posts: 5,972
  • Joined: 21-March 08

Re: Unable to get out of loop

Posted 12 February 2009 - 09:42 AM

View Postrgfirefly24, on 12 Feb, 2009 - 11:25 AM, said:

Lets look at the way your doing it. Instead of trying to have you do it differently.
while (accountStruct.admin != true || accountStruct.admin != false);


you're while loop asks that while:
the variable accountStruct.admin does not equal true OR the variable accountStruct.admin does not equal false

we continue the loop
So senario 1:  User enters true:

while (accountStruct.admin != true || accountStruct.admin != false);
					  false					  OR			  true							  statement true keep looping

Senario 2:  User enters false:

while (accountStruct.admin != true || accountStruct.admin != false);
					   true					  OR			  false						  statement true keep looping

Senario 3:  User enters anything other than true or false:

System catches it and won't let it through to the while loop


Solution:  Use a && instead

Senario 1:  User enters true:

while (accountStruct.admin != true && accountStruct.admin != false);
						 False																	   statement false loop stops

The while doesn't check any farther than the first condition because it already got its false result.

Senario 2: User enters False

while (accountStruct.admin != true && accountStruct.admin != false);
						   True				 AND		False							   statement false loop stops



Basically using an OR both statements have to equate to false in order to get a compound false. With an AND only one statement has to be false to trigger the compound false.


since you are now using &&, type in "hey", and see what happens. incase you didn't know, a boolean value is defaulted to false. So whether the user puts in a valid response or not, it will be false. Therefore passing the "while" and dropping out of the loop.

This post has been edited by eclipsed4utoo: 12 February 2009 - 09:44 AM

Was This Post Helpful? 0
  • +
  • -

#5 rgfirefly24   User is offline

  • D.I.C Lover
  • member icon


Reputation: 473
  • View blog
  • Posts: 2,221
  • Joined: 07-April 08

Re: Unable to get out of loop

Posted 12 February 2009 - 09:51 AM

I would think that his attempt at converting the input to boolean would catch that.

My assumtion is that inputting "hey" will not convert properly to a bool value


EDIT: Ahh yes i see what you mean ok yea. I never said that his way was "right" i was just wanting him to understand why it kept looping. I guess the words i used at the begginning of my post were a little off kilter.
Was This Post Helpful? 0
  • +
  • -

#6 c++struggler   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 05-July 08

Re: Unable to get out of loop

Posted 12 February 2009 - 05:33 PM

Below is the entire code of my program. This will probably help so you are able to see what I am all trying to do. I changed it now and it gets out of the loop but it doesn't stay in the loop if the user types in something other than "true" or "false." I am new to this so not sure about bool. Thanks everyone for all your help I do appreciate it.

struct account
	{
		public string fname;
		public string lname;
		public string uname;
		public string password;
		public int active;
		public bool admin;
	}
	class Program
	{
		static void getstruct(ref account accountStruct)
		{
			bool haslength = false;
			bool hasDigit = false;
			bool hasUpper = false;
			bool hasLower = false;
			bool valid = false;
			int length = accountStruct.password.Length;
			
			Console.Write("Firstname: ");
			accountStruct.fname = Console.ReadLine();

			Console.Write("Lastname: ");
			accountStruct.lname = Console.ReadLine();

			Console.Write("Username: ");
			accountStruct.uname = Console.ReadLine();

			do
			{
				Console.Write("Password: ");
				accountStruct.password = Console.ReadLine();

				if (accountStruct.password.Length < 6 || accountStruct.password.Length > 12)
				{
					Console.WriteLine("Password is too many or not enough characters. \nYou need to have between 6 and 12 characters.");
					Console.Write("Password: ");
					accountStruct.password = Console.ReadLine();
				}
				if (accountStruct.password.Length >=6 && accountStruct.password.Length <= 12)
				{
					haslength = true;
					for (int i = 0; i < accountStruct.password.Length; i++)
					{
						if (char.IsDigit(accountStruct.password[i]))
							hasDigit = true;
						if (char.IsUpper(accountStruct.password[i]))
							hasUpper = true;
						if (char.IsLower(accountStruct.password[i]))
							hasLower = true;
					}
					if (hasDigit == false || hasLower == false || hasUpper == false)
						Console.WriteLine("There must be a digit, an uppercase letter \nand a lowercase letter in the password.");
				}
				valid = ((haslength) && (hasDigit) && (hasUpper) && (hasLower));
			}
			while (accountStruct.password.Length < 6 || accountStruct.password.Length > 12 || valid == false);

			do
			{
				try
				{
					Console.Write("Days Active: ");
					accountStruct.active = Convert.ToInt32(Console.ReadLine());
					if (accountStruct.active < 1 || accountStruct.active > 1000)
					{
						Console.WriteLine("Days active must be between 1 and 1000.");
						Console.Write("Days Active: ");
						accountStruct.active = Convert.ToInt32(Console.ReadLine());
					}
				}
				catch (FormatException)
				{
					Console.WriteLine("Check your format. \nDays active must be between 1 and 1000.");
				}
			} while (accountStruct.active < 1 || accountStruct.active > 1000);

			do
			{
				try
				{
					Console.Write("Admin Account type True or False: ");
					accountStruct.admin = Convert.ToBoolean(Console.ReadLine());

					if (accountStruct.admin != true && accountStruct.admin != false)
					{
						Console.WriteLine("Must type in true or false for admin account.");
						Console.Write("Admin Account: ");
						accountStruct.admin = Convert.ToBoolean(Console.ReadLine());
					}
				}
				catch(FormatException)
				{
					Console.WriteLine("Must type in true or false");
				}
			} while (accountStruct.admin != true && accountStruct.admin != false);  
		}

		static void add(account[] structArray, account accountStruct, int number)
		{
			structArray[number].lname = accountStruct.lname;
			structArray[number].fname = accountStruct.fname;
			structArray[number].uname = accountStruct.uname;
			structArray[number].password = accountStruct.password;
			structArray[number].active = accountStruct.active;
			structArray[number].admin = accountStruct.admin;
		}

		static void displayStruct(account[] structArray, int number)
		{
			for (int i = 0; i < number; i++)
			{
				Console.WriteLine("{0}\n{1}\n{2}\n{3}\n{4}\n{5}", structArray[i].fname,
					structArray[i].lname, structArray[i].uname, structArray[i].password,
					structArray[i].active, structArray[i].admin);
			}
		}

		static void Main(string[] args)
		{
			account uaccount;
			int numberofaccounts = 0;

			uaccount.fname = "";
			uaccount.lname = "";
			uaccount.uname = "";
			uaccount.password = "";
			uaccount.active = 0;
			uaccount.admin = false;
			account[] accountArray = new account[100];


			for (int i = 0; i < 2; i++)
			{
				getstruct(ref uaccount);
				add(accountArray, uaccount, numberofaccounts);
				numberofaccounts++;
			}

			displayStruct(accountArray, numberofaccounts);

			Console.ReadKey();

		}


Was This Post Helpful? 0
  • +
  • -

#7 eclipsed4utoo   User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1536
  • View blog
  • Posts: 5,972
  • Joined: 21-March 08

Re: Unable to get out of loop

Posted 12 February 2009 - 07:16 PM

View Postc++struggler, on 12 Feb, 2009 - 07:33 PM, said:

Below is the entire code of my program. This will probably help so you are able to see what I am all trying to do. I changed it now and it gets out of the loop but it doesn't stay in the loop if the user types in something other than "true" or "false." I am new to this so not sure about bool. Thanks everyone for all your help I do appreciate it.

struct account
	{
		public string fname;
		public string lname;
		public string uname;
		public string password;
		public int active;
		public bool admin;
	}
	class Program
	{
		static void getstruct(ref account accountStruct)
		{
			bool haslength = false;
			bool hasDigit = false;
			bool hasUpper = false;
			bool hasLower = false;
			bool valid = false;
			int length = accountStruct.password.Length;
			
			Console.Write("Firstname: ");
			accountStruct.fname = Console.ReadLine();

			Console.Write("Lastname: ");
			accountStruct.lname = Console.ReadLine();

			Console.Write("Username: ");
			accountStruct.uname = Console.ReadLine();

			do
			{
				Console.Write("Password: ");
				accountStruct.password = Console.ReadLine();

				if (accountStruct.password.Length < 6 || accountStruct.password.Length > 12)
				{
					Console.WriteLine("Password is too many or not enough characters. \nYou need to have between 6 and 12 characters.");
					Console.Write("Password: ");
					accountStruct.password = Console.ReadLine();
				}
				if (accountStruct.password.Length >=6 && accountStruct.password.Length <= 12)
				{
					haslength = true;
					for (int i = 0; i < accountStruct.password.Length; i++)
					{
						if (char.IsDigit(accountStruct.password[i]))
							hasDigit = true;
						if (char.IsUpper(accountStruct.password[i]))
							hasUpper = true;
						if (char.IsLower(accountStruct.password[i]))
							hasLower = true;
					}
					if (hasDigit == false || hasLower == false || hasUpper == false)
						Console.WriteLine("There must be a digit, an uppercase letter \nand a lowercase letter in the password.");
				}
				valid = ((haslength) && (hasDigit) && (hasUpper) && (hasLower));
			}
			while (accountStruct.password.Length < 6 || accountStruct.password.Length > 12 || valid == false);

			do
			{
				try
				{
					Console.Write("Days Active: ");
					accountStruct.active = Convert.ToInt32(Console.ReadLine());
					if (accountStruct.active < 1 || accountStruct.active > 1000)
					{
						Console.WriteLine("Days active must be between 1 and 1000.");
						Console.Write("Days Active: ");
						accountStruct.active = Convert.ToInt32(Console.ReadLine());
					}
				}
				catch (FormatException)
				{
					Console.WriteLine("Check your format. \nDays active must be between 1 and 1000.");
				}
			} while (accountStruct.active < 1 || accountStruct.active > 1000);

			do
			{
				try
				{
					Console.Write("Admin Account type True or False: ");
					accountStruct.admin = Convert.ToBoolean(Console.ReadLine());

					if (accountStruct.admin != true && accountStruct.admin != false)
					{
						Console.WriteLine("Must type in true or false for admin account.");
						Console.Write("Admin Account: ");
						accountStruct.admin = Convert.ToBoolean(Console.ReadLine());
					}
				}
				catch(FormatException)
				{
					Console.WriteLine("Must type in true or false");
				}
			} while (accountStruct.admin != true && accountStruct.admin != false);  
		}

		static void add(account[] structArray, account accountStruct, int number)
		{
			structArray[number].lname = accountStruct.lname;
			structArray[number].fname = accountStruct.fname;
			structArray[number].uname = accountStruct.uname;
			structArray[number].password = accountStruct.password;
			structArray[number].active = accountStruct.active;
			structArray[number].admin = accountStruct.admin;
		}

		static void displayStruct(account[] structArray, int number)
		{
			for (int i = 0; i < number; i++)
			{
				Console.WriteLine("{0}\n{1}\n{2}\n{3}\n{4}\n{5}", structArray[i].fname,
					structArray[i].lname, structArray[i].uname, structArray[i].password,
					structArray[i].active, structArray[i].admin);
			}
		}

		static void Main(string[] args)
		{
			account uaccount;
			int numberofaccounts = 0;

			uaccount.fname = "";
			uaccount.lname = "";
			uaccount.uname = "";
			uaccount.password = "";
			uaccount.active = 0;
			uaccount.admin = false;
			account[] accountArray = new account[100];


			for (int i = 0; i < 2; i++)
			{
				getstruct(ref uaccount);
				add(accountArray, uaccount, numberofaccounts);
				numberofaccounts++;
			}

			displayStruct(accountArray, numberofaccounts);

			Console.ReadKey();

		}



the code looks exactly the same. take a look at my code. it works.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1