Infinite loop issue

Hi,
I wrote this code but for some reason when I type any other letter aside for the choices provided it goes into an infinite loop.
I am rather new to C++, any words of advise on this?

Thanks!


//file: Saving account
/*This program calculates the balance of the user's saving account.*/
#include <iostream> // tells the compiler to include this file
#include <string> // tells the compiler to include this file
#include<math.h>// tells the compiler to include this file
#include<limits>// tells the compiler to include this file
using namespace std; // using objects in region std


int displayMenu (char word);
char choice;

int main ()
{
char word; // input
const char SENTINEL = 'Z'; //sentinel value
const char SENTINEL2 = 'z'; //sentinel value
int amount; // input - each transaction amount
int sum; // output - total of transactions
sum = 0; // initialize
int account_NUM; // input - account number
bool cond;

do
{
cout << "Please enter your account number (Please enter digits):";
cin >> account_NUM;
cond = cin.fail();
cin.clear();
cin.ignore(INT_MAX, '\n');

}while(cond);

cout<<endl<<endl;
cout<< "Please enter the initial balance: $";
cin>>sum;
displayMenu(choice);
cout<<endl;
while ((choice != SENTINEL) && (choice != SENTINEL2)){
if ((choice == 'W') || (choice == 'w') || (choice == 'D') || (choice == 'd'))
{
if ((choice == 'W') || (choice == 'w')) {
cout<<endl;
cout<< "Please enter the amount you would like to withdraw: $";
cin>>amount;
sum -= amount;
cout<<endl;
cout<<"your balance is now $"<<sum<<endl; // displayBalance(balance);
cout<<endl;
displayMenu(choice);
}
if ((choice == 'D') || (choice == 'd')){
cout<<endl;
cout<< "Please enter the amount you would like to deposit: $";
cin>>amount;
sum += amount;
cout<<endl;
cout<<"your balance is now $"<<sum<<endl; // displayBalance(balance);
cout<<endl;
displayMenu(choice);
}
}
else
{
cout<< "you have entered an invalid choice. ";
cout<<endl;
}
}

system ("pause");
return 0;
}



int displayMenu(char word)
{
cout<<"Please enter the first letter of your transactions from the following menu: " <<endl;
cout<<"(W)Withdrawal"<<endl;
cout<<"(D)Deposit"<<endl;
cout<<"(Z) to end account transaction"<<endl;
cin>>choice;
}

> when I type any other letter aside for the choices provided it goes into an
> infinite loop.
that's because in that case you don't call `displayMenu()' again.


By the way in `displayMenu()' you don't use the parameter and you forgot to return something.
Here is an option for solve your problem:
1
2
3
4
5
else 
{ 
      cout<< "you have entered an invalid choice. "; 
      break;
}


Use a break, but it terminates the program.
Yeah, the global variable is confusing.

The cin.ignore() line is a nice touch, but you haven't used it everywhere you could.

Here's a possible solution - I've mainly moved things about and added/removed very little. Also, since you didn't format your code, I've taken the liberty of using a very different style :)

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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
//file: Saving account
/*This program calculates the balance of the user's saving account.*/

#include <iostream>
#include <string>
#include <limits>
#include <cmath>
#include <cctype>

using namespace std;

static void displayMenu(char &choice);
template <typename T> static void getValue(const string &disp, T &val);

int main ()
{
	const char SENTINEL = 'z';
	const char withdraw = 'w';
	const char deposit = 'd';
	int sum = 0;
	int account_NUM = 0;
	char choice = 0;

	getValue("Please enter your account number (Please enter digits):", account_NUM);
	getValue("Please enter the initial balance: $", sum);

	do
	{
		displayMenu(choice);
		int amount = 0;

		if (tolower(choice) == withdraw)
		{
			getValue("Please enter the amount you would like to withdraw: $", amount);
			sum -= amount;
			cout << "your balance is now $" << sum << endl;
			cout << endl;
			continue;
		}
		else if (tolower(choice) == deposit)
		{
			getValue("Please enter the amount you would like to deposit: $", amount);
			sum += amount;
			cout << "your balance is now $" << sum << endl;
			continue;
		}
		// maybe add a final else block?
	} while (tolower(choice) != SENTINEL);

	cout << "Enter to continue...";
	cin.ignore(numeric_limits<streamsize>::max(), '\n');

	return 0;
}



static void displayMenu(char &choice)
{
	string menu =
		"Please select an activity by typing a letter as shown in the following menu:\n"
		"(W) to make a withdrawal\n"
		"(D) to deposit funds\n"
		"(Z) to end the session\n";
	getValue(menu, choice);
}

template <typename T> static void getValue(const string &disp, T &val)
{
	bool cond = false;
	do
	{
		cout << endl << disp;
		cin >> val;
		cond = cin.fail();
		cin.clear();
		cin.ignore(numeric_limits<streamsize>::max(), '\n');

	} while(cond);

	cout << endl;
}
Thank you guys so much!
Topic archived. No new replies allowed.