Infinite loop in while function

  • (2 Pages)
  • +
  • 1
  • 2

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

#1 Sirr_Purr_the_Cat  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 33
  • Joined: 14-June 12

Infinite loop in while function

Posted 15 June 2012 - 08:12 PM

#include <iostream>
using namespace std;

void welcomeScreen()
{
	cout << "$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$" << endl;
	cout << "Welcome to MyBank ATM" << endl;
	cout << "Your Friend in the Market" << endl;
	cout << "$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$" << endl;
	cout << "" << endl;
}

int main()
{
	welcomeScreen();
	int xin;
	cout << "How 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;
	system ("CLS");

	while (xin != 3)
	{
		while (xin = 1)
		{
			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;
			system ("CLS");
		}
		
		 if (xin = 2)
		{
			cout << "Checkings balance = " << endl;
			cout << "Savings balance = " << endl;
			cout << "Press 1 for deposit menu" << endl;
			cout << "Press 2 to exit" << endl;
			cin >> xin;
			system ("CLS");
		}

		else 
		{cout << "Invalid Entry" << endl;}

	cout << "===================================================" << endl;
	cout << "How 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;
	}
	

	}



For the above code, every time I enter in an input other than "1" I still get sent to my deposit screen.

I know it is an infinite loop but I have followed all of my teachers notes and can not get out of this one.

Thanks to anyone who can help me.

Is This A Good Question/Topic? 0
  • +

Replies To: Infinite loop in while function

#2 simeesta  Icon User is offline

  • Deadly Ninja


Reputation: 215
  • View blog
  • Posts: 586
  • Joined: 04-August 09

Re: Infinite loop in while function

Posted 15 June 2012 - 08:27 PM

You're using = instead of == in several places. = is assignment. == is testing for equality. eg on line 26 while(x = 1) is always true.It should be while(x==1)
Was This Post Helpful? 3
  • +
  • -

#3 Sirr_Purr_the_Cat  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 33
  • Joined: 14-June 12

Re: Infinite loop in while function

Posted 15 June 2012 - 09:35 PM

wow.....wow....just...wow.

I feel so dumb. Thank you so much for politely pointing out my mistake and not calling me an idiot outright.

I guess this is what I get when its late and its been a long day....
Was This Post Helpful? 0
  • +
  • -

#4 Sirr_Purr_the_Cat  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 33
  • Joined: 14-June 12

Re: Infinite loop in while function

Posted 16 June 2012 - 07:45 PM

Ok, guys, so bear with me. I am a new programmer and I have been posting a lot of things in relation to this assignment I have. I have pored over my instructors notes and put in my my work and effort into it and I finally have it looking like my professor wants it to be. Now I just need it to act that way.

The idea behind this program is pretty simple. It is a basic ATM machine in which you can input deposits and look at your balance.

My problem is, in the code below, whenever I try to exit the loop, like in the deposit menu function, inputting a 3 will bring you back to the main menu like I want but the deposit menu will continue to show up as well completely messing with the functions. Same with the balance section, I want to set it up so you can see your balance and return to the main menu but ALONG with returning to the main menu it will ALSO display the balance menu again.

#include <iostream>
using namespace std;

void mainMenu() //Main menu screen for navigation
{
	cout << "How 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;
}

void depositMenu() //Seperate deposit screen
{			
			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;
			system ("CLS");

	while (xin != 3) 
		{
			 if (xin == 1)
			{
				double cAmt;
				cout << "Enter the amount you would like to deposit" << endl;
				cin >> cAmt;
				cout << "Your total is: " << cAmt << endl;
				cin >> xin;
			 }

			else if (xin == 2)
			{
				double sAmt;
				cout << "Enter the amount you would like to deposit" << endl;
				cin >> sAmt;
				cout << "Your total is: " << sAmt << endl;
				cin >> xin;
			}
			
		}
	system ("CLS");
	mainMenu(); // Returns user to Main Menu screen when 3 is input
	
}


int main()
{	
	cout << "$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$" << endl; //Welcome screen at start of program
	cout << "Welcome to MyBank ATM" << endl;
	cout << "Your Friend in the Market" << endl;
	cout << "$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$\n" << endl;

	int xin;
	mainMenu(); //Calls Main Menu
	cin >> xin;
	system ("CLS");

	while (xin != 3)
	{
		if (xin == 1) //Enters into deposit menu
		{
			depositMenu(); //Calls deposit menu
		}

		 if (xin == 2) //Shows balance and returns user to Main Menu interface
		{
			double cAmt, sAmt;
			cout << "Checkings balance =  " << cAmt << endl;
			cout << "Savings balance = " << sAmt << endl;
			system ("pause");
			system ("CLS");
			mainMenu();
		}

		else 
		{
			cout << "Invalid Entry" << endl;
			mainMenu();
		 }
	
	}

	cout << "Exiting. Good bye!" << endl;
	system ("pause");
	return 0;
}



I thought that setting up my while loops so that
 while {!= 3} 
would get me out of the loop but I was wrong. Can someone explain how to properly exit out of while loops without getting stuck in them infinitely.

The math within the code is not yet complete so do not worry too much about the amounts, the while loops is a much harder struggle I am having. (Although working in the proper math algorithm is proving challenging. To all the mods and admins, I promise I will keep my questions concerning this code within this thread now so I am not continuously making topics.

I really want to get this damn program perfect! I've been spending so much time on it but it's still throwing me for loops.

Thanks in advance to those who help me. I love this place.
Was This Post Helpful? 0
  • +
  • -

#5 mccabec123  Icon User is offline

  • D.I.C Head

Reputation: 18
  • View blog
  • Posts: 225
  • Joined: 03-March 11

Re: Infinite loop in while function

Posted 16 June 2012 - 07:56 PM

Your problem I believe is your while loops, as these loops will continuously repeat until the value 3 has been entered, and this will never be reached if you enter 1 or 2 as there is no cin for the menu choice within the loop, so the xin value for the menu will stay the same throughout the entire loop.

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.

Hope this helps, cheers.

This post has been edited by mccabec123: 16 June 2012 - 07:58 PM

Was This Post Helpful? 2
  • +
  • -

#6 DaneAU  Icon User is offline

  • Great::Southern::Land
  • member icon

Reputation: 284
  • View blog
  • Posts: 1,615
  • Joined: 15-May 08

Re: Infinite loop in while function

Posted 16 June 2012 - 10:48 PM

The problem is that you are calling your mainMenu from a delegated method, this call should be done within the main methods loop. All program flow should be handled here, you are also not going to get the correct values for your sAmt and cAmt because the variables scope's are within the method, moving them to global variables or passing them by reference to the methods will alleviate this issue. You also fail to correctly negotiate your if and if-else statements as mccabec123 discussed. Using xin across all members can make it a little confusing so it is important to take care when using variables with the same name as not to confuse yourself, making a clear and explicit distinction with the naming convention can help with this problem :)

I have ammmended your code and implemented some loops to better negate the issues you are having, i have moved some variables around and placed some comments within the code to help you understand how it all works.

#include <iostream>

using namespace std;

double cAmt = 0; 	// cheque account balance
double sAmt = 0;    // savings account balance

void mainMenu() //Main menu screen for navigation
{
   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;
}

void depositMenu() //Seperate deposit screen
{
   int xin = 0;

   while (xin != 3) {
      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;

      /* within here i removed the cin >> xin; statements, they are not needed
       now that the menu is shown each time the loop executes */
      if (xin == 1) {
         cout << "Enter the amount you would like to deposit" << endl;
         cin >> cAmt;
         cout << "Your total is: " << cAmt << endl;
      } else if (xin == 2) {
         cout << "Enter the amount you would like to deposit" << endl;
         cin >> sAmt;
         cout << "Your total is: " << sAmt << endl;
      } else if (xin == 3) {
         break;
      } else	/* added this just to inform user */
         cout << "Invalid Input!" << endl;
   }
}


int main()
{
   cout << "$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$" << endl; //Welcome screen at start of program
   cout << "Welcome to MyBank ATM" << endl;
   cout << "Your Friend in the Market" << endl;
   cout << "$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$\n" << endl;

   // initialise xin
   int xin = 0;

   while (xin != 3) {
      mainMenu(); //Calls Main Menu
      cin >> xin;

      if (xin == 1) { //Enters into deposit menu
         depositMenu(); //Calls deposit menu
      } else if (xin == 2) { //Shows balance and returns user to Main Menu interface
         // double cAmt, sAmt;	~~ these should be global variables, the reason you get
         // 					   weird output is because they are not initialised at all.
         cout << "Checkings balance =  " << cAmt << endl;
         cout << "Savings balance = " << sAmt << endl;
      } else if (xin == 3) {
         break;
      } else {
         cout << "Invalid Entry" << endl;
      }

   }
   cout << "Exiting. Good bye!" << endl;
   return 0;
}


Always be wary of the scope of variables and ensure you initialise them prior to entering a loop.

eg. Sentinel controlled while
int counter = 0;
while (counter != 3) {
  // operation...
  counter++;
}



not
int counter;
while (counter != 3) {
   counter++;
}


Post back if you have any issues :)

This post has been edited by DaneAU: 16 June 2012 - 10:52 PM

Was This Post Helpful? 2
  • +
  • -

#7 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 5951
  • View blog
  • Posts: 23,210
  • Joined: 23-August 08

Re: Infinite loop in while function

Posted 17 June 2012 - 03:31 AM

Merged duplicate topics. Please do NOT create a new topic when you already have one on the same subject.
Was This Post Helpful? 0
  • +
  • -

#8 Sirr_Purr_the_Cat  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 33
  • Joined: 14-June 12

Re: Infinite loop in while function

Posted 17 June 2012 - 12:06 PM

Wow guys thank you so much, especially DaneAU!

That really cleared some things up, especially with how to use the breaks and why my variables were not working correctly.

Thanks so much, I'm going to study this code and understand it before I turn it in.
Was This Post Helpful? 0
  • +
  • -

#9 Sirr_Purr_the_Cat  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 33
  • Joined: 14-June 12

Re: Infinite loop in while function

Posted 17 June 2012 - 12:11 PM

One quick question though,

when I was originally working on the code for the math where I was failing, I was attempting to make separate functions for the Checkings and Savings which can be continuously added too and then be checked on in the balance section.

Am I on the right path with thinking that if I create a function that has the values, cTotal and sTotal, and then have cAmt and sAmt add into that with the whole function simply showing the Total variables? For example:
double checkingsMoney (double cTotal, double cAmt)
{
    checkingsMoney = cTotal + cAmt;
    return checkingsMoney
}



With the user inputting cAmt, am I thinking the right way for this code if I initialize all these variables globally, and the function globally?
Was This Post Helpful? 0
  • +
  • -

#10 v0rtex  Icon User is offline

  • Caffeine: db "Never Enough!"
  • member icon

Reputation: 223
  • View blog
  • Posts: 773
  • Joined: 02-June 10

Re: Infinite loop in while function

Posted 17 June 2012 - 12:18 PM

double checkingsMoney (double cTotal, double cAmt)
{
    checkingsMoney = cTotal + cAmt;
    return checkingsMoney
}



Will not work as you are trying to return an unknown variable however if you are using a global variable or local variable like so (alternatively if you were trying to return the functionName somehow, then that is not possible):

double checkingsMoney = 0; //global variable

double GetCheckingsMoney (double cTotal, double cAmt)
{
    checkingsMoney = cTotal + cAmt;
    return checkingsMoney
}


OR

double GetCheckingsMoney (double cTotal, double cAmt)
{
   double checkingsMoney = cTotal + cAmt; //local variable
    return checkingsMoney
}



Then the compiler knows what checkingsMoney is, or alternatively you can just return the output as it does not look like you are going to process the addition in any other way like so:

double GetCheckingsMoney (double cTotal, double cAmt)
{
   return (cTotal+cAmt);
}



I think you want to print out both values, you can use a function for GetSavingsMoney and another one for GetCheckingsMoney and then in your main method do the following:

//**Just pseudo-code but it illustrates the idea
cout << GetSavingsMoney(cAmt);
cout << GetSavingsMoney(sAmt);



Hope this helps somewhat, it seems like you are getting there Sirr_Purr_the_Cat :^:

This post has been edited by v0rtex: 17 June 2012 - 12:22 PM

Was This Post Helpful? 2
  • +
  • -

#11 Sirr_Purr_the_Cat  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 33
  • Joined: 14-June 12

Re: Infinite loop in while function

Posted 17 June 2012 - 12:28 PM

View Postv0rtex, on 17 June 2012 - 12:18 PM, said:

double GetCheckingsMoney (double cTotal, double cAmt)
{
   return (cTotal+cAmt);
}



I think you want to print out both values, you can use a function for GetSavingsMoney and another one for GetCheckingsMoney and then in your main method do the following:

//**Just pseudo-code but it illustrates the idea
cout << GetSavingsMoney(cAmt);
cout << GetSavingsMoney(sAmt);



Yea that is exactly what I was aiming for. How come I do not have to call in sTotal or cTotal when you call the function up within the main method? Above you just show the variables cAmt and sAmt.

Haha thanks, programming is a completely new thing to me but I've gotten really into it as I am now aiming to get a computer science B.S. It's going to be a tough road but after these first few initial classes I think I can really sink my teeth into some learning on my own. My code has definitely come a long way since when I started surfing this message board and before all the elbow grease. Thanks a lot!
Was This Post Helpful? 0
  • +
  • -

#12 v0rtex  Icon User is offline

  • Caffeine: db "Never Enough!"
  • member icon

Reputation: 223
  • View blog
  • Posts: 773
  • Joined: 02-June 10

Re: Infinite loop in while function

Posted 17 June 2012 - 12:44 PM

Well, you could make them global variables but that is a bit of a cop-out solution. I was unsure as to what you needed to pass hence I did not pass the correct variables. Sorry for the confusion. You would not need to pass them by value, in this case, passing a reference is adequate:

Here is an example:
#include <iostream>

using namespace std;

double GetCheckingsMoney(double& number1, double& number2) {
	return (number1+number2);
}
int main()
{
	double sAmt = 0;
	double cAmt = 0;

	cout << "Enter data for Savings Money: " << endl;
	cin >> sAmt;

	cout << endl << "Enter data for Checkings Money: ";
	cin >> cAmt;
	
	cout << endl << "Here is your Checking Account: ";
	cout << GetCheckingsMoney(sAmt,cAmt); //pass by reference so that we can actually work with the data itself
	return 0;
}



The advantage to this is that it is a faster solution as we do not need to copy the variable we are working with and is actually appropriate in this case (but is not appropriate in all cases!) however if you do not want to pass a reference then you can just pass by value like you were doing before.

cout << GetCheckingsMoney(sAmt,cAmt);



If you are unsure about references, I have a tutorial on this site otherwise simply google for one!

Hope this helps.

EDIT: You could also use pointers instead of references but that might be a little bit overkill in this context. References might be overkill too. Passing by value should be fine

This post has been edited by v0rtex: 17 June 2012 - 12:59 PM

Was This Post Helpful? 1
  • +
  • -

#13 Sirr_Purr_the_Cat  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 33
  • Joined: 14-June 12

Re: Infinite loop in while function

Posted 17 June 2012 - 04:13 PM

Hmmmm.....references seem to be a bit over my head at the moment but I do see what you are getting at. I don't quite know how I should work them into this code however...

What I was trying to do was something like this. Using the function,

double checkingsMoney (double cAmt = 0, double cAdd = 0) {  //checking function
	return (cAmt + cAdd);
}



I was going to apply it into this situation

      if (xin == 1) {
		 int din;
         cout << "Enter the amount you would like to deposit" << endl;
         cin >> cAmt;
         cout << "Your total is: " << checkingsMoney (cAmt, cAdd) << endl;
		 cout << "Press 1 to deposit more or 2 to return" << endl;
		 cin >> din
		 if (din == 1) {
			 cout <<"Enter the amount you would like to deposit" << endl;
			 cout >> cAdd;
			 cout << "Your total is: " << checkingsMoney (cAdd, cAmt) << endl;
                         cin >> din
		 }
		 else if (din == 2) {
			 depositMenu();
		 }


I was going to try that and see if that could accurately produce the results I want but unfortunately I'm getting all sorts of errors I've never seen before...like this:

error C2676: binary '>>' : 'std::ostream' does not define this operator or a conversion to a type acceptable to the predefined operator

Now I'm totally confused and wondering what I just broke.
Was This Post Helpful? 0
  • +
  • -

#14 snoopy11  Icon User is offline

  • Engineering ● Software
  • member icon

Reputation: 710
  • View blog
  • Posts: 2,033
  • Joined: 20-March 10

Re: Infinite loop in while function

Posted 17 June 2012 - 04:20 PM

Hi

'std::ostream' is the output stream and can't use the operator '>>' it uses the operator '<<'

'std::istream' is the input stream and does use the operator '>>'

hope that clarifies things

look at line 10

Snoopy.

This post has been edited by snoopy11: 17 June 2012 - 04:21 PM

Was This Post Helpful? 0
  • +
  • -

#15 Sirr_Purr_the_Cat  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 33
  • Joined: 14-June 12

Re: Infinite loop in while function

Posted 17 June 2012 - 05:02 PM

Thanks a lot. That's definitely a mistake I'll have to start checking myself for.

In case anyone is wondering here is the final (hopefully) code that I have come up with for this simple program.

#include <iostream>
using namespace std;

double cAmt, cAdd, sAdd, sAmt;

double checkingsMoney (double cAmt = 0, double cAdd = 0) {  //checking function
	return (cAmt + cAdd);
}

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

void mainMenu() //Main menu screen for navigation
{
   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;

}

void depositMenu() //Seperate deposit screen
{
   int xin;
	int din;
	int sin;
   while (xin != 3) {
      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;

      if (xin == 1) {
		 
         cout << "Enter the amount you would like to deposit" << endl;
         cin >> cAmt;
         cout << "Your total is: " << checkingsMoney (cAmt, cAdd) << endl;
		 cout << "Press 1 to deposit more or 2 to return" << endl;
		 cin >> din;	
		 if (din == 1) {
			 cout <<"Enter the amount you would like to deposit" << endl;
			 cin >> cAdd;
			 cout << "Your total is: " << checkingsMoney (cAdd, cAmt) << endl;
			 cout << "Press 1 again for the deposit menu" << endl;
				 cin >> din;
		 }
		 }

       else if (xin == 2) {
         cout << "Enter the amount you would like to deposit" << endl;
         cin >> sAmt;
         cout << "Your total is: " << savingsMoney (sAdd, sAmt) << endl;
		 cout << "Press 1 to deposit more or 2 to return" << endl;
		 cin >> sin;
		 if (sin == 1) {
			 cout << "Enter the amount you would like to deposit" << endl;
			 cin >> sAdd;
			cout << "Your total is: " << savingsMoney (sAdd, sAmt) << endl;
			 cout << "Press 1 again for the deposit menu" << endl;
			 cin >> sin;
		 }
      } else if (xin == 3) {
         break;
      } else	
         cout << "Invalid Input! Please try again" << endl;
   }
}

int main()
{
   cout << "$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$" << endl; //Welcome screen at start of program
   cout << "Welcome to MyBank ATM" << endl;
   cout << "Your Friend in the Market" << endl;
   cout << "$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$\n" << endl;

   int xin = 0;

   while (xin != 3) {
      mainMenu(); //Calls Main Menu
      cin >> xin;

      if (xin == 1) { //Enters into deposit menu
         depositMenu(); //Calls deposit menu
      } else if (xin == 2) { //Shows balance and returns user to Main Menu interface
         cout << "Checkings balance =  " << checkingsMoney (cAdd, cAmt) << endl;
         cout << "Savings balance = " << savingsMoney (sAdd, sAmt) << endl;
      } else if (xin == 3) {
         break;
      } else {
         cout << "Invalid Entry" << endl;
      }

   }
   cout << "Exiting. Good bye!" << endl;
   return 0;
}



I really want to thank everyone who contributed to this topic and my learning. This was great for me and I'm sure there is something I need to fix with the above code :P
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2