Infinite loop in while function

  • (2 Pages)
  • +
  • 1
  • 2

21 Replies - 1146 Views - Last Post: 25 June 2012 - 03:56 AM Rate Topic: -----

#16 Switters  Icon User is offline

  • D.I.C Head

Reputation: 12
  • View blog
  • Posts: 66
  • Joined: 03-June 12

Re: Infinite loop in while function

Posted 17 June 2012 - 05:53 PM

View Postmccabec123, on 16 June 2012 - 07:56 PM, said:

To exit the loops you can use a 'break;' or a 'return 0;', better to use a break though if you can, unless you do want to return to where the function was called.


Curious as to why you'd recommend using a break to exist a loop at all? Don't mean to second guess you, I just never saw the virtue in doing so...if the logical criteria that determine entry/exit are properly established, a break should never really be necessary. Plus it creates 2 ways to get out of a loop, and thus more potential for bugs. Do you have a different perspective?
Was This Post Helpful? 0
  • +
  • -

#17 DaneAU  Icon User is offline

  • Great::Southern::Land
  • member icon

Reputation: 278
  • View blog
  • Posts: 1,588
  • Joined: 15-May 08

Re: Infinite loop in while function

Posted 18 June 2012 - 12:11 AM

You would use a break in a loop that is infinite or you allow the user to opt out at some point.

int x = -1;
do {
   cout << "Enter Number\n0 to Exit: " << endl;
   cin >> x;

   if(x < 0)
     cout << "Negative Value" << endl;
   else if (x > 0)
     cout << "Positive Value" << endl;
   else   // the only other condition is zero
      break;
} while (true);


Was This Post Helpful? 0
  • +
  • -

#18 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 4892
  • View blog
  • Posts: 11,287
  • Joined: 16-October 07

Re: Infinite loop in while function

Posted 18 June 2012 - 04:48 AM

Globals are bad for many reasons. One is, they often get you thinking in the wrong direction.

Just step back at look at these functions:
double checkingsMoney (double cAmt = 0, double cAdd = 0) {  //checking function
	return (cAmt + cAdd);
}

double savingsMoney (double sAmt, double sAdd) {    // savings function
	return (sAmt + sAdd);
}



They are the same function. double add(double x, double y) { return x + y; }. They don't seem to have much of a reason to exist, really. But the are probably confusing you.

Look at your deposit menu. Options one and two are fundamentally identical. You do too much work to deal with exit. Your menu behavior for the two menus is almost identical; that's where you can rock the copy paste.

e.g.
#include <iostream>

using namespace std;

int mainMenuSelect() {
	int xin;
	cout << "\nHow can I help you today?" << endl;
	cout << "Enter # 1 to make a Deposit" << endl;
	cout << "Enter # 2 to check your Balance" << endl;
	cout << "Enter # 3 to Exit" << endl;
	cin >> xin;
	return xin;
}

int depositMenuSelect() {
	int xin;
	cout << "Welcome to the Deposit section" << endl;
	cout << "Please press 1 for deposit into checkings account" << endl;
	cout << "Please press 2 for deposit into savings account" << endl;
	cout << "Please press 3 for the main menu" << endl;
	cin >> xin;
	return xin;
}

void depositInto(double &acct) {
	/* your code here */
}

void depositMenu(double &cAmt, double &sAmt) {
	int xin = 0;
	while (xin != 3) {
		xin = depositMenuSelect();
		if (xin == 1) {
			depositInto(cAmt);
		} else if (xin == 2) {
			depositInto(sAmt);
		} else if (xin != 3) {
			cout << "Invalid Input! Please try again" << endl;
		}
	}
}

void mainMenu(double &cAmt, double &sAmt) {
	int xin = 0;
	while (xin != 3) {
		xin = mainMenuSelect();
		if (xin == 1) {
			depositMenu(cAmt, sAmt);
		} else if (xin == 2) {
         	cout << "Checkings balance =  " << cAmt << endl;
         	cout << "Savings balance = " << sAmt << endl;
		} else if (xin != 3) {
			cout << "Invalid Input! Please try again" << endl;
		}
	}
}

int main() {
	// your accounts.  They live here.  You pass them
	double cAmt, sAmt;

	cAmt = sAmt = 0;

	cout << "$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$" << endl; //Welcome screen at start of program
	cout << "Welcome to MyBank ATM" << endl;
	cout << "Your Friend in the Market" << endl;
	cout << "$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$\n" << endl;
	mainMenu(cAmt, sAmt);
	cout << "Exiting. Good bye!" << endl;
	
	return 0;
}



Your program as it stands only supports a single deposit. You need to add the value the user enters, not just hang onto it forever.

Hope this helps.
Was This Post Helpful? 1
  • +
  • -

#19 Switters  Icon User is offline

  • D.I.C Head

Reputation: 12
  • View blog
  • Posts: 66
  • Joined: 03-June 12

Re: Infinite loop in while function

Posted 18 June 2012 - 06:02 AM

View PostDaneAU, on 18 June 2012 - 12:11 AM, said:

You would use a break in a loop that is infinite or you allow the user to opt out at some point.

int x = -1;
do {
   cout << "Enter Number\n0 to Exit: " << endl;
   cin >> x;

   if(x < 0)
     cout << "Negative Value" << endl;
   else if (x > 0)
     cout << "Positive Value" << endl;
   else   // the only other condition is zero
      break;
} while (true);




It still seems to me that you are introducing an explicit command when the logic should be doing it's job in setting the flow of control for the loop. I don't doubt that your version "works," my point is that you can always construct the loop conditions in such a way that you don't need to explicitly instruct the program to pass flow of control outside of the loop.


const int undesirable_X = 0;

int x = -1;
while (x != undesirable_X)
  {
   cout << "Enter Number\n0 to Exit: " << endl;
   cin >> x;

   if(x < 0)
     cout << "Negative Value" << endl;
   else if (x > 0)
     cout << "Positive Value" << endl;
  }


Was This Post Helpful? 0
  • +
  • -

#20 DaneAU  Icon User is offline

  • Great::Southern::Land
  • member icon

Reputation: 278
  • View blog
  • Posts: 1,588
  • Joined: 15-May 08

Re: Infinite loop in while function

Posted 18 June 2012 - 06:55 AM

Yes introducing an explicit command, well the thing is especially when using if-else is what actually gets looked at.

Condsider a program with 3 options posted here, it makes logical sense in most aspects to have a menu type else-if block, however what happens should you wish to add more values
int x = -1;

while(true)
{
  if(x == 1) // something
  else if(x == 2) // something
  else // something
}


The way you have it you would need to check for a specific value
x = -1;

while (x != 0) {
  if(x == 1) // soemething
  else if(x == 2) // something
  else if(x == 0) // something
  else // something
}


Using a break statement stops any further comparisons being executed, infact the comparison should be done first and the loop terminated immediately.
x = -1;

while(true) 
{
  if(x == 0) break;
  else if(x == 1) // do something
  else if(x == 2) // do something
  else // do something
}


Of course that assumes zero to be a determinate to exit some loop for which your program performs an operation.

Consider the flip side, if for instance you wish to add more complex comparators, perhaps ones where a lengthy operation is performed to determine some value... And in turn you utilise the loop.

x = 1;

while(true) 
{
  if(x == 0) break;
  else if (doMassiveLengthyAlgorithm(x*100) == 1) // do soemthing else
  else // something else
}


In this case the algorithm may take time to complete, and you may imagine for instance that there would be a heavy overhead if the break was not undertaking on the first comparison to terminate the loop and prevent unwarranted operations occurring.
Was This Post Helpful? 1
  • +
  • -

#21 Switters  Icon User is offline

  • D.I.C Head

Reputation: 12
  • View blog
  • Posts: 66
  • Joined: 03-June 12

Re: Infinite loop in while function

Posted 18 June 2012 - 07:09 AM

Good point, I had not thought if it in those terms. Thanks for taking the time to explain.
Was This Post Helpful? 0
  • +
  • -

#22 busyme12srv  Icon User is offline

  • New D.I.C Head

Reputation: -5
  • View blog
  • Posts: 44
  • Joined: 18-June 12

Re: Infinite loop in while function

Posted 25 June 2012 - 03:56 AM

Instead of while(x=1) it should be while(x==1) // use comparison operator
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2