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.
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 :)
//file: Saving account
/*This program calculates the balance of the user's saving account.*/
#include <iostream>
#include <string>
#include <limits>
#include <cmath>
#include <cctype>
usingnamespace std;
staticvoid displayMenu(char &choice);
template <typename T> staticvoid getValue(const string &disp, T &val);
int main ()
{
constchar SENTINEL = 'z';
constchar withdraw = 'w';
constchar 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;
}
elseif (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;
}
staticvoid 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> staticvoid 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;
}